-
Notifications
You must be signed in to change notification settings - Fork 70
/
bpmn.provider.ts
120 lines (117 loc) · 3.69 KB
/
bpmn.provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2023 Sourcefuse Technologies
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import {bind, BindingScope, Provider, service} from '@loopback/core';
import {AnyObject} from '@loopback/repository';
import {HttpErrors} from '@loopback/rest';
import {
WorflowManager,
Workflow,
WorkflowDto,
WorkflowVersion,
} from '@sourceloop/bpmn-service';
import {CamundaService} from '../services';
@bind({scope: BindingScope.TRANSIENT})
export class BpmnProvider implements Provider<WorflowManager> {
constructor(
@service(CamundaService)
private readonly camunda: CamundaService,
) {}
value(): WorflowManager {
return {
getWorkflowById: async workflow => {
const response = await this.camunda.get<AnyObject>(
workflow.externalIdentifier,
);
return new Workflow({
provider: 'camunda',
id: workflow.id,
workflowVersion: workflow.workflowVersion,
externalIdentifier: response.id,
name: response.name,
inputSchema: workflow.inputSchema,
});
},
startWorkflow: async (input, workflow, version) => {
let response;
if (version) {
response = await this.camunda.execute<AnyObject>(
version.externalWorkflowId,
input,
);
} else {
response = await this.camunda.execute<AnyObject>(
workflow.externalIdentifier,
input,
);
}
return response;
},
createWorkflow: async (workflowDto: WorkflowDto) => {
const response = await this.camunda.create<AnyObject>(
workflowDto.name,
Buffer.from(workflowDto.bpmnFile, 'utf-8'),
);
let version = 1;
let id;
if (response.deployedProcessDefinitions) {
const processDefinition = Object.values(
//NOSONAR
response.deployedProcessDefinitions, //NOSONAR
)[0] as AnyObject; //NOSONAR
version = processDefinition.version;
id = processDefinition.id;
} else {
throw new HttpErrors.BadRequest(
'Workflow with same name and definition already exists',
);
}
return {
version: version,
provider: 'camunda',
processId: id,
externalId: id,
fileRef: workflowDto.bpmnFile,
};
},
updateWorkflow: async (workflowDto: WorkflowDto) => {
const response = await this.camunda.create<AnyObject>(
workflowDto.name,
Buffer.from(workflowDto.bpmnFile, 'utf-8'),
);
let version;
let id;
if (response.deployedProcessDefinitions) {
const processDefinition = Object.values(
//NOSONAR
response.deployedProcessDefinitions, //NOSONAR
)[0] as AnyObject; //NOSONAR
version = processDefinition.version;
id = processDefinition.id;
} else {
throw new Error('Workflow not changed or Worflow does not exist!');
}
return {
version: version,
provider: 'camunda',
processId: id,
externalId: id,
fileRef: workflowDto.bpmnFile,
};
},
deleteWorkflowById: async (workflow: Workflow) => {
await this.camunda.delete(
workflow.workflowVersions.map(
(version: WorkflowVersion) => version.externalWorkflowId as string,
),
);
return workflow;
},
deleteWorkflowVersionById: async (version: WorkflowVersion) => {
await this.camunda.deleteVersion(version.externalWorkflowId);
return version;
},
};
}
}