-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagento.js
271 lines (261 loc) · 7.88 KB
/
magento.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var Client = require('node-rest-client').Client;
var client = new Client();
var emitter = require('../core-integration-server-v2/javascripts/emitter');;
var url, userName, password, token;
var errMsg = '"Connection timeout error" in Magento';
var page = 1, count = 0, finalDataArr = [];
function run(node) {
try {
var type = node.option.toLowerCase();
var newUrl = url + "/index.php/rest/V1/integration/admin/token";
var resObj = node.reqData;
var args = {
data : {
username : userName,
password : password
},
headers : {
"Content-Type" : 'application/json'
}
};
client.post(newUrl, args, function(data, res) {
try {
var status = parseInt(res.statusCode/100);
if (status == 2) {
token = data;
getOrders(node);
} else {
if(status == 5) {
emitter.emit('error', 'Server Error', data, '', node);
} else {
errMsg = data.message;
if(errMsg.includes('%resources')) {
errMsg.slice('%resources');
}
emitter.emit('error', errMsg, '', newUrl, node);
}
}
} catch(e) {
emitter.emit('error', e.message, e.stack, newUrl, node);
}
}).on('error', function(err) {
emitter.emit('error', errMsg, '', newUrl, node);
});
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function getOrders(node) {
try {
var basicKey = token.replace("\"", "");
var newUrl = url + "/index.php/rest/V1/orders?searchCriteria[pageSize]=10&searchCriteria[currentPage]=" + page;
var args = {
headers : {
Authorization : "Bearer " + basicKey,
Accept : "application/json"
}
};
client.get(newUrl, args, function(data, res) {
try {
var status = parseInt(res.statusCode/100);
if(status == 2) {
count = data.total_count;
setOrders(data.items, node);
} else {
if(status == 5) {
emitter.emit('error', 'Server Error in Magento', data.toString(), newUrl, node);
} else {
errMsg = data.message;
if(errMsg.includes('%resources')) {
errMsg.slice('%resources');
}
emitter.emit('error', errMsg, data, newUrl, node);
}
}
} catch(e) {
emitter.emit('error', e.message, e.stack, newUrl, node);
}
}).on('error', function(err) {
emitter.emit('error', errMsg, '', newUrl, node);
});
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function setOrders(ordersArr, node) {
try {
var resArr = [];
var obj, resObj;
var actionName = node.connection.actionName.toLowerCase();
var msgPrefix = 'No ';
if(node.optionType.toLowerCase() == 'new') {
msgPrefix = 'No new ';
}
var length = ordersArr.length;
var pathStartTime = node.connection.startedAt;
var arr = pathStartTime.split('/');
var formattedDateStr = arr[1] + '/' + arr[0] + '/' + arr[2];
var startDate = new Date(formattedDateStr);
for(var i = 0; i < ordersArr.length; i++) {
resObj = {};
obj = ordersArr[i];
if(node.optionType.toLowerCase() == 'new') {
var cDate = new Date(obj.created_at);
var cUTCDate = new Date(cDate);
if(cUTCDate.getTime() < startDate.getTime()) {
continue;
}
}
resObj.id = obj.increment_id;
resObj.email = obj.customer_email;
resObj.price = obj.base_subtotal;
resObj.quantity = obj.total_qty_ordered;
resObj.shippingAmount = obj.base_shipping_amount;
var objStatus = obj.status;
if(objStatus.toLowerCase() == 'canceled') {
objStatus = 'cancelled';
}
resObj.status = objStatus;
resObj.name = obj.increment_id;
resObj.createdAt = obj.created_at;
resObj.updatedAt = obj.updated_at;
var billingAddress = {};
billingAddress.name = obj.billing_address.firstname + ' ' + obj.billing_address.lastname;
billingAddress.firstName = obj.billing_address.firstname;
billingAddress.lastName = obj.billing_address.lastname;
billingAddress.company = obj.billing_address.company;
billingAddress.street = obj.billing_address.street[0];
billingAddress.city = obj.billing_address.city;
billingAddress.country = obj.billing_address.country_id;
billingAddress.zip = obj.billing_address.postcode;
billingAddress.phone = obj.billing_address.telephone;
billingAddress.state = obj.billing_address.region_code;
billingAddress.countryCode = obj.billing_address.country_id;
resObj.billingAddress = billingAddress;
var shippingAddress = {};
shippingAddress.name = obj.billing_address.firstname + ' ' + obj.billing_address.lastname;
shippingAddress.firstName = obj.billing_address.firstname;
shippingAddress.lastName = obj.billing_address.lastname;
shippingAddress.company = obj.billing_address.company;
shippingAddress.street = obj.billing_address.street[0];
shippingAddress.city = obj.billing_address.city;
shippingAddress.country = obj.billing_address.country_id;
shippingAddress.zip = obj.billing_address.postcode;
shippingAddress.phone = obj.billing_address.telephone;
shippingAddress.state = obj.billing_address.region_code;
shippingAddress.country = obj.billing_address.country_id;
resObj.shippingAddress = shippingAddress;
resObj.shippingMethod = obj.payment.method;
resObj.paymentMethod = obj.payment.method;
resObj.customerId = obj.customer_id;
var itemArr = obj.items;
var itemsArr = [];
var itemObj, item;
var quantity = 0;
for(var j = 0; j < itemArr.length; j++) {
item = {};
itemObj = itemArr[j];
item.id = itemObj.product_id;
item.name = itemObj.name;
item.price = itemObj.base_price;
item.quantity = itemObj.qty_ordered;
item.variant = itemObj.product_type;
item.sku = itemObj.sku;
itemsArr[j] = item;
}
resObj.items = itemsArr;
resObj.isLast = false;
resObj.slackFlag = false;
var length = finalDataArr.length + i;
if(length == count-1) {
resObj.isLast = true;
if(actionName == 'slack') {
resObj.slackFlag = true;
}
}
var l = resArr.length;
resArr[l] = resObj;
}
if(resArr.length == 0) {
emitter.emit("error", msgPrefix + 'orders found in Magento', "", "", node);
return;
}
post(resArr, node, "");
finalDataArr = finalDataArr.concat(resArr);
if(finalDataArr.length != count) {
page++;
getOrders(node);
}
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function post(response, node, message) {
node.resData = response;
emitter.emit('success', node, message);
}
function testApp(callback) {
try {
var newUrl = url + "/index.php/rest/V1/integration/admin/token";
var args = {
data : {
username : userName,
password : password
},
headers : {
"Content-Type" : 'application/json'
}
};
var result;
client.post(newUrl, args, function(data, res) {
try {
var status = parseInt(res.statusCode/100);
if(status == 2) {
result = {
status : 'success',
response : data
};
} else {
result = {
status : 'error',
response : data
};
}
callback(result);
} catch(e) {
callback({status : 'error', response : e.stack});
}
}).on('error', function(err) {
callback({status:"error", response:err});
});
} catch(e) {
callback({status:"error", response:e.stack});
}
}
function test(request, callback) {
try {
var credentials = request.credentials;
url = credentials.url;
userName = credentials.userName;
password = credentials.password;
testApp(callback);
} catch(e) {
callback({status:'error', response:e.stack});
}
}
function init(node) {
try {
var credentials = node.credentials;
url = credentials.url;
userName = credentials.userName;
password = credentials.password;
run(node);
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
var Magento = {
init : init,
test : test
};
module.exports = Magento;