-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
68 lines (58 loc) · 1.97 KB
/
script.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
require('dotenv').config()
var request = require('sync-request');
var fs = require('fs');
var Handlebars = require('handlebars');
var HandlebarsIntl = require('handlebars-intl');
HandlebarsIntl.registerWith(Handlebars);
Handlebars.registerHelper("math", function(lvalue, operator, rvalue, options) {
lvalue = parseFloat(lvalue);
rvalue = parseFloat(rvalue);
return {
"+": lvalue + rvalue,
"-": lvalue - rvalue,
"*": lvalue * rvalue,
"/": lvalue / rvalue,
"%": lvalue % rvalue
}[operator];
});
Handlebars.registerHelper("combinedOrders", function(order, pretext, options) {
if (order.customerNotes) {
var res = order.customerNotes.match(/[0-9]{9}/g);
if (res) {
var orders = res.filter(function(item, pos) {
return (res.indexOf(item) == pos) && (item != order.orderNumber);
})
return pretext + orders.join(", ");
}
}
return "";
});
Handlebars.registerHelper("groupBuy", function(order, pretext, options) {
if (order.items) {
var res = order.items.filter(function(item) {
return item.sku.match(/GB.*/g) || item.sku == "SQ6503229";
});
if (res.length) {
return pretext;
}
}
return "";
});
const file_template_source = fs.readFileSync('README_template.md', 'utf8');
const file_template = Handlebars.compile(file_template_source);
var orders = [];
for (var page = 1, num_pages = 1; page <= num_pages; page++) {
var res_page = request('GET',
'https://ssapi.shipstation.com/orders?orderStatus=awaiting_shipment&sortBy=orderDate&pageSize=500&page=' + page, {
headers: {
'Authorization': process.env.AUTH_KEY
}}
);
var body = JSON.parse(res_page.getBody());
num_pages = body.pages;
orders = orders.concat(body.orders);
}
fs.writeFile('README.md', file_template({
"orders": orders,
"date": new Date()
}));