This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
orderstatus.js
85 lines (71 loc) · 3.29 KB
/
orderstatus.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
'use strict';
module.exports = function (RED) {
var YaaS = require('yaas.js');
var yaas = new YaaS();
function ReadOrderstatus(config, orderId) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCustomerCredentials = RED.nodes.getNode(config.yaasCustomerCredentials);
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.status({ fill: 'yellow', shape: 'dot', text: 'idle' });
node.tenant_id = node.yaasCredentials.application_id.split('.')[0];
node.on('input', function (msg) {
yaas.init(node.yaasCredentials.client_id,
node.yaasCredentials.client_secret,
'hybris.order_read',
node.tenant_id)
.then(function () {
var orderId = msg.payload.orderId || msg.payload.orderid || msg.payload;
yaas.order.getSalesorderDetails(orderId)
.then(result => {
//console.log('result:', result);
var status = result.body.status;
node.status({ fill: 'green', shape: 'dot', text: orderId + ' ' + status });
node.send({ payload: result.body.status, body: result.body });
}).catch(error => {
_error(node, error);
});
})
.catch(error => {
_error(node, error);
});
});
}
function ChangeOrderstatus(config, orderId, status) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCustomerCredentials = RED.nodes.getNode(config.yaasCustomerCredentials);
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.status({ fill: 'yellow', shape: 'dot', text: 'idle' });
node.tenant_id = node.yaasCredentials.application_id.split('.')[0];
node.on('input', function (msg) {
yaas.init(node.yaasCredentials.client_id,
node.yaasCredentials.client_secret,
'hybris.order_update',
node.tenant_id)
.then(function () {
var orderId = msg.payload.orderId || msg.payload.orderid;
var status = msg.payload.status;
yaas.order.transitionSalesorder(orderId, status)
.then(result => {
//console.log('result:', result);
node.status({ fill: 'green', shape: 'dot', text: orderId + ' ' + status });
node.send({ payload: result.body });
}).catch(error => {
_error(node, error);
});
})
.catch(error => {
_error(node, error);
});
});
}
function _error(node, error) {
var message = error.body.message || 'error order status';
node.status({ fill: 'red', shape: 'dot', text: message });
node.error(error);
console.error(JSON.stringify(error));
}
RED.nodes.registerType('readorderstatus', ReadOrderstatus);
RED.nodes.registerType('changeorderstatus', ChangeOrderstatus);
};