-
Notifications
You must be signed in to change notification settings - Fork 2
/
konecty-queue.js
77 lines (71 loc) · 2.01 KB
/
konecty-queue.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
const api = require('./api');
module.exports = function(RED) {
function KonectyQueueNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.server = RED.nodes.getNode(config.server);
node.on('input', function(msg) {
const { host, key } = node.server;
const { queueType, queue, queueData } = config;
let token = key;
if (config.token && config.tokenType) {
const userToken = RED.util.evaluateNodeProperty(config.token, config.tokenType, this, msg);
if (/[^ ]+/.test(userToken)) {
token = userToken;
}
}
const apiInstance = api({ host, key: token });
let id;
if (queueType === 'form') {
const { i } = JSON.parse(queueData);
id = i;
} else {
id = RED.util.evaluateNodeProperty(queue, queueType, this, msg);
}
if (id == null) {
node.warn(RED._('konecty-queue.errors.invalid-data'));
node.status({ fill: 'red', shape: 'ring', text: 'konecty-queue.queue.invalid-data' });
return;
}
node.status({ fill: 'blue', shape: 'ring', text: RED._('konecty-queue.label.running') });
apiInstance
.getNextOnQueue(id)
.then(({ success, user }) => {
if (success) {
node.send([
{
...msg,
payload: user
}
]);
node.status({});
} else {
node.send([
null,
{
...msg,
payload: errors
}
]);
const errMessages = errors.map(({ message }) => message).join('\n');
node.error(RED._('konecty-queue.errors.error-processing', { message: errMessages }));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('konecty-queue.errors.error-processing', { message: errMessages })
});
}
})
.catch(error => {
node.error(RED._('konecty-queue.errors.error-processing', error));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('konecty-queue.errors.error-processing', error)
});
});
node.status({});
});
}
RED.nodes.registerType('konecty-queue', KonectyQueueNode);
};