-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessService.ts
More file actions
172 lines (138 loc) · 5.68 KB
/
processService.ts
File metadata and controls
172 lines (138 loc) · 5.68 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import cds from '@sap/cds';
import { PROCESS_LOGGER_PREFIX, PROCESS_PREFIX, PROCESS_SERVICE } from '../constants';
import { emitProcessEvent, ProcessLifecyclePayload, ProcessStartPayload } from './utils';
import { WorkflowStatus } from '../api';
const LOG = cds.log(PROCESS_LOGGER_PREFIX);
export function registerProcessServiceHandlers(service: cds.Service): void {
const definitionId = service.definition?.[PROCESS_PREFIX] as string | undefined;
if (!definitionId) {
LOG.warn(
`No definitionID found for service ${service.name}. Process service handlers will not be registered.`,
);
return;
}
LOG.debug(`Registering handlers for process service: ${service.name}`);
LOG.debug(` ${PROCESS_PREFIX}: ${definitionId}`);
registerStartHandler(service, definitionId);
registerSuspendHandler(service, definitionId);
registerResumeHandler(service, definitionId);
registerCancelHandler(service, definitionId);
registerGetInstancesByBusinessKeyHandler(service, definitionId);
registerGetAttributesHandler(service, definitionId);
registerGetOutputsHandler(service, definitionId);
}
function registerStartHandler(service: cds.Service, definitionId: string): void {
service.on('start', async (req) => {
LOG.debug(`Starting process: ${definitionId}`);
const { inputs } = req.data;
if (!inputs) {
return req.reject({ status: 400, message: 'Missing required parameter: inputs' });
}
const payload: ProcessStartPayload = { definitionId, context: inputs };
await emitProcessEvent('start', req, payload, `Failed to start workflow: ${definitionId}`);
});
}
function registerSuspendHandler(service: cds.Service, definitionId: string): void {
service.on('suspend', async (req) => {
LOG.debug(`Suspending process: ${definitionId}`);
const { businessKey, cascade } = req.data;
if (!businessKey) {
return req.reject({ status: 400, message: 'Missing required parameter: businessKey' });
}
const payload: ProcessLifecyclePayload = { businessKey, cascade: cascade ?? false };
await emitProcessEvent(
'suspend',
req,
payload,
`Failed to suspend process with business key: ${businessKey}`,
);
LOG.debug(`Process suspended: businessKey=${businessKey}`);
});
}
function registerResumeHandler(service: cds.Service, definitionId: string): void {
service.on('resume', async (req) => {
LOG.debug(`Resuming process: ${definitionId}`);
const { businessKey, cascade } = req.data;
if (!businessKey) {
return req.reject({ status: 400, message: 'Missing required parameter: businessKey' });
}
const payload: ProcessLifecyclePayload = { businessKey, cascade: cascade ?? false };
await emitProcessEvent(
'resume',
req,
payload,
`Failed to resume process with business key: ${businessKey}`,
);
LOG.debug(`Process resumed: businessKey=${businessKey}`);
});
}
function registerCancelHandler(service: cds.Service, definitionId: string): void {
service.on('cancel', async (req) => {
LOG.debug(`Cancelling process: ${definitionId}`);
const { businessKey, cascade } = req.data;
if (!businessKey) {
return req.reject({ status: 400, message: 'Missing required parameter: businessKey' });
}
const payload: ProcessLifecyclePayload = { businessKey, cascade: cascade ?? false };
await emitProcessEvent(
'cancel',
req,
payload,
`Failed to cancel process with business key: ${businessKey}`,
);
LOG.debug(`Process cancelled: businessKey=${businessKey}`);
});
}
function registerGetInstancesByBusinessKeyHandler(
service: cds.Service,
definitionId: string,
): void {
service.on('getInstancesByBusinessKey', async (req) => {
LOG.debug(`Getting instances by businessKey for process: ${definitionId}`);
const { businessKey, status } = req.data;
if (!businessKey) {
return req.reject({ status: 400, message: 'Missing required parameter: businessKey' });
}
if (status) {
const validStatuses = Object.values(WorkflowStatus);
const statuses = Array.isArray(status) ? status : [status];
const invalidStatuses = statuses.filter((s) => !validStatuses.includes(s));
if (invalidStatuses.length > 0) {
return req.reject({
status: 400,
message: `Invalid status value(s): ${invalidStatuses.join(', ')}. Valid values are: ${validStatuses.join(', ')}`,
});
}
}
const processService = await cds.connect.to(PROCESS_SERVICE);
const result = await processService.send('getInstancesByBusinessKey', {
businessKey,
status,
});
return result;
});
}
function registerGetAttributesHandler(service: cds.Service, definitionId: string): void {
service.on('getAttributes', async (req) => {
LOG.debug(`Getting attributes for process: ${definitionId}`);
const { processInstanceId } = req.data;
if (!processInstanceId) {
return req.reject({ status: 400, message: 'Missing required parameter: processInstanceId' });
}
const processService = await cds.connect.to(PROCESS_SERVICE);
const result = await processService.send('getAttributes', { processInstanceId });
return result;
});
}
function registerGetOutputsHandler(service: cds.Service, definitionId: string): void {
service.on('getOutputs', async (req) => {
LOG.debug(`Getting outputs for process: ${definitionId}`);
const { processInstanceId } = req.data;
if (!processInstanceId) {
return req.reject({ status: 400, message: 'Missing required parameter: processInstanceId' });
}
const processService = await cds.connect.to(PROCESS_SERVICE);
const result = await processService.send('getOutputs', { processInstanceId });
return result;
});
}