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
/
Copy pathcustomer.js
140 lines (121 loc) · 5.77 KB
/
customer.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
module.exports = function(RED) {
var YaaS = require('yaas.js');
function missesRequirements(obj, req){
var missing = [];
for(var r of req){
if(!obj[r]) {
missing.push(r);
}
}
return missing.length ? missing : false;
}
function YaasCustomerSignupNode(config) {
RED.nodes.createNode(this, config);
this.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
this.tenant_id = config.tenantId;
this.status({fill: 'yellow', shape: 'ring', text: 'idle'});
var yaas = new YaaS();
yaas.init(this.yaasCredentials.client_id, this.yaasCredentials.client_secret, '', this.tenant_id)
.then(() => {
this.status({fill: 'yellow', shape: 'dot', text: 'Token received'});
this.on('input', msg => {
var missingRequirements = missesRequirements(msg.payload, ['email', 'password']);
if(missingRequirements){
this.error('Missing required fields: ' + JSON.stringify(missingRequirements));
this.status({fill: 'red', shape: 'dot', text: 'Missing fields (' +missingRequirements.length + ')'});
}
else if(msg.payload.password.length < 6) {
this.error('Password too short (at least 6 characters)');
this.status({fill: 'red', shape: 'dot', text: 'Password too short'});
} else {
yaas.customer.signup(msg.payload)
.then(response => {
this.status({fill: 'green', shape: 'dot', text: 'Signed up as: ' + response.body.id});
msg.payload = response.body.id;
this.send(msg);
})
.catch(error => {
this.status({fill: 'red', shape: 'dot', text: 'error during signup'});
this.error('error during singup');
console.error(JSON.stringify(error));
});
}
});
});
}
RED.nodes.registerType('customer signup', YaasCustomerSignupNode);
function YaasUpdateCustomerNode(config) {
RED.nodes.createNode(this, config);
this.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
this.tenant_id = config.tenantId;
this.status({fill: 'yellow', shape: 'ring', text: 'idle'});
var yaas = new YaaS();
yaas.init(this.yaasCredentials.client_id, this.yaasCredentials.client_secret,
'hybris.customer_update', this.tenant_id)
.then(() => {
this.status({fill: 'yellow', shape: 'dot', text: 'Token received'});
this.on('input', msg => {
var missingRequirements = missesRequirements(msg.payload, ['firstName', 'lastName']);
if(missingRequirements){
this.error('Missing required fields: ' + JSON.stringify(missingRequirements));
this.status({fill: 'red', shape: 'dot', text: 'Missing fields (' +missingRequirements.length + ')'});
}else {
msg.payload.preferredSite = msg.payload.preferredSite || 'main';
var customerNumber = msg.payload.customerNumber;
delete msg.payload.customerNumber;
yaas.customer.updateCustomer(customerNumber, msg.payload)
.then(response => {
this.status({fill: 'green', shape: 'dot', text: 'Customer ' + customerNumber + ' updated'});
msg.payload = customerNumber;
this.send(msg);
})
.catch(error => {
this.error('error during customer update');
this.status({fill: 'red', shape: 'dot', text: 'error during customer update'});
console.error(JSON.stringify(error));
});
}
});
});
}
RED.nodes.registerType('update customer', YaasUpdateCustomerNode);
function YaasCustomerAddressNode(config) {
RED.nodes.createNode(this, config);
this.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
this.tenant_id = config.tenantId;
this.status({fill: 'yellow', shape: 'ring', text: 'idle'});
var yaas = new YaaS();
yaas.init(this.yaasCredentials.client_id,
this.yaasCredentials.client_secret,
'hybris.customer_update',
this.tenant_id)
.then(() => {
this.status({fill: 'yellow', shape: 'dot', text: 'Token received'});
this.on('input', msg => {
var missingRequirements = missesRequirements(msg.payload,
['street', 'streetNumber', 'zipCode', 'city', 'country']);
if(missingRequirements){
this.error('Missing required fields: ' + JSON.stringify(missingRequirements));
this.status({fill: 'red', shape: 'dot', text: 'Missing fields (' +missingRequirements.length + ')'});
} else {
var customerNumber = msg.payload.customerNumber;
delete msg.payload.customerNumber;
yaas.customer.createCustomerAddress(customerNumber, msg.payload)
.then(response => {
var statusText = 'New address for customer ' + customerNumber + ' created';
this.status({fill: 'green', shape: 'dot', text: statusText});
msg.payload = customerNumber;
this.send(msg);
})
.catch(error => {
this.error('error during customer address creation');
this.status({fill: 'red', shape: 'dot', text: 'error during customer address creation'});
console.error(JSON.stringify(error));
});
}
});
});
}
RED.nodes.registerType('customer address', YaasCustomerAddressNode);
};