-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusertask-event-listener.js
81 lines (68 loc) · 3.08 KB
/
usertask-event-listener.js
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
module.exports = function (RED) {
function UserTaskEventListener(config) {
RED.nodes.createNode(this, config);
var node = this;
node.engine = RED.nodes.getNode(config.engine);
let subscription;
const register = async () => {
const client = node.engine.engineClient;
if (!client) {
node.error('No engine configured.', {});
return;
}
const query = RED.util.evaluateNodeProperty(config.query, config.query_type, node);
function userTaskCallback() {
return async (userTaskNotification) => {
if (config.usertask != '' && config.usertask != userTaskNotification.flowNodeId) return;
const newQuery = {
flowNodeInstanceId: userTaskNotification.flowNodeInstanceId,
...query,
};
try {
const matchingFlowNodes = await client.userTasks.query(newQuery);
if (matchingFlowNodes.userTasks && matchingFlowNodes.userTasks.length == 1) {
const userTask = matchingFlowNodes.userTasks[0];
node.send({
payload: {
flowNodeInstanceId: userTaskNotification.flowNodeInstanceId,
userTaskEvent: userTaskNotification,
userTask: userTask,
action: config.eventtype,
type: 'usertask',
},
});
}
} catch (error) {
node.error(error, {});
}
};
}
async function subscribe() {
switch (config.eventtype) {
case 'new':
return await client.userTasks.onUserTaskWaiting(userTaskCallback());
case 'finished':
return await client.userTasks.onUserTaskFinished(userTaskCallback());
case 'reserved':
return await client.userTasks.onUserTaskReserved(userTaskCallback());
case 'reservation-canceled':
return await client.userTasks.onUserTaskReservationCanceled(userTaskCallback());
default:
node.error('no such event: ' + config.eventtype, {});
}
}
subscription = subscribe();
node.on('close', async () => {
if (node.engine && node.engine.engineClient && client) {
client.userTasks.removeSubscription(subscription);
}
});
};
if (node.engine) {
register().catch((error) => {
node.error(error, {});
});
}
}
RED.nodes.registerType('usertask-event-listener', UserTaskEventListener);
};