forked from jeffdonthemic/node-red-contrib-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 17
/
obm.js
65 lines (63 loc) · 2.38 KB
/
obm.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
module.exports = function(RED) {
var parser = require('xml2js').parseString;
function ParseObm(config) {
RED.nodes.createNode(this, config);
const node = this;
this.on('input', (msg) => {
parser(msg.payload, (err, result) => {
if (err) {
node.error(err, msg);
node.status({ fill: 'red', shape: 'dot', text: 'Error parsing XML.' });
} else {
// TODO: debulkify incoming messages ??
const returnMsg = Object.assign({}, msg);
const httpMsg = Object.assign({}, msg);
try {
// get rid of the stuff we don't need
const root = result['soapenv:Envelope']['soapenv:Body'][0].notifications[0];
const data = root.Notification[0].sObject[0];
// start building payload
returnMsg.payload = {
organizationId: root.OrganizationId[0],
actionId: root.ActionId[0],
type: data.$['xsi:type'].split(':')[1],
sobject: {}
};
// check for a sessionId
if (Array.isArray(root.SessionId)) {
returnMsg.payload.sessionId = root.SessionId[0];
}
// look at each node and see if it contains an array with data
for (let [key, val] of Object.entries(data)) {
if (Array.isArray(val)) {
returnMsg.payload.sobject[key.split(':')[1].toLowerCase()] = val[0];
}
}
// Prepare the return message for http
if (httpMsg.headers) {
httpMsg.headers['Content-Type'] = 'application/xml';
} else {
httpMsg.headers = {
'Content-Type': 'application/xml'
};
}
httpMsg.payload = `<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<notificationsResponse xmlns="http://soap.sforce.com/2005/09/outbound">
<Ack>true</Ack>
</notificationsResponse>
</soapenv:Body>
</soapenv:Envelope>`;
node.send([returnMsg, httpMsg]);
node.status({});
} catch (err) {
node.status({ fill: 'red', shape: 'dot', text: 'Error!' });
node.error(err, msg);
}
}
});
});
}
RED.nodes.registerType('obm', ParseObm);
};