-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkonecty-opportunity.js
111 lines (97 loc) · 3.27 KB
/
konecty-opportunity.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
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
const api = require('./api');
module.exports = function(RED) {
function KonectyOpportunityNode(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 { extraFields, extraFieldsType } = config;
// Prepare payload
let opportunityData = {};
const product = RED.util.evaluateNodeProperty(config.product, config.productType, this, msg);
if (/^[0-9]+$/.test(product)) {
opportunityData.product = { code: product };
} else if (/$[^\b]+$/.test(product)) {
opportunityData.product = { _id: product };
}
const setFormData = field => {
if (config[`${field}Type`] === 'form') {
try {
const { i = '' } = JSON.parse(config[`${field}Data`]);
if (/[^\b]+/.test(i)) {
opportunityData[field] = { _id: i };
}
} catch (_) {}
} else {
const i = RED.util.evaluateNodeProperty(config[field], config[`${field}Type`], this, msg) || '';
if (/[^\b]+/.test(i)) {
opportunityData[field] = { _id: i };
}
}
};
['contact', 'user', 'campaign'].forEach(setFormData);
const extraFieldsData = RED.util.evaluateNodeProperty(extraFields, extraFieldsType, this, msg) || {};
if (Object.keys(extraFieldsData).length > 0) {
opportunityData = {
...opportunityData,
...extraFieldsData
};
}
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 });
if (Object.keys(opportunityData).length === 0) {
node.warn(RED._('konecty-opportunity.errors.invalid-data'));
node.status({ fill: 'red', shape: 'ring', text: RED._('konecty-opportunity.errors.invalid-data') });
node.send([null, { ...msg, payload: [RED._('konecty-opportunity.errors.invalid-data')] }]);
return;
}
node.status({ fill: 'blue', shape: 'ring', text: RED._('konecty-opportunity.label.running') });
apiInstance
.createOpportunity(opportunityData)
.then(({ success, processData: { opportunity } = {}, errors }) => {
if (success) {
node.send([
{
...msg,
payload: opportunity
}
]);
node.status({});
} else {
node.send([
null,
{
...msg,
payload: errors
}
]);
const errMessages = errors.map(({ message }) => message).join('\n');
node.error(RED._('konecty-opportunity.errors.error-processing', { message: errMessages }));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('konecty-opportunity.errors.error-processing', { message: errMessages })
});
}
})
.catch(error => {
node.send([null, { ...msg, payload: [RED._('konecty-opportunity.errors.error-processing')] }]);
node.error(RED._('konecty-opportunity.errors.error-processing', error));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('konecty-opportunity.errors.error-processing', error)
});
});
node.status({});
});
}
RED.nodes.registerType('konecty-opportunity', KonectyOpportunityNode);
};