forked from tessel/tessel.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-selection.js
159 lines (148 loc) · 4.36 KB
/
module-selection.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
var Celery = require("try-celery");
var async = require('async')
var stubs = require('./stubs.json');
var numeral = require('numeral');
numeral.defaultFormat('$0,0.00');
function getExistingOrderDetails(orderID, callback) {
// Create a Celery connection to the Tessel 2 Celery account
if(process.env.NODE_ENV === 'dev'){
callback(null, stubs.orderDetails.data);
return;
}
var celery = new Celery({
"key" : process.env.CELERY_TOKEN_T2,
"version" : 2
});
console.log('fetching!', "orders/" + orderID.toString())
// Ping Celery for orders for a specific id
celery.request("orders/" + orderID.toString(), function(err, body) {
// If an error happened, log it just in case
if (err) {
console.log(err);
// Pass the error back to the caller
if (typeof callback === 'function') {
callback(err);
}
return;
}
// If there was no error
else {
// Just call the callback
if (typeof callback === 'function') {
callback(null, body.data);
}
return;
}
});
}
function getAvailableModules(callback) {
if(process.env.NODE_ENV === 'dev'){
stubs.products.map(function(item){
item.price_formatted = numeral(item.price/100).format();
item.product_id = item._id;
item.id = item.slug;
return item;
});
callback(null, stubs.products);
return;
}
// Fetch modules from the celery account
var celery = new Celery({
"key" : process.env.CELERY_TOKEN_T2,
"version" : 2
});
// Ping Celery for orders for a specific id
celery.request("products/", function(err, body) {
if (err) {
// If an error happened, log it just in case
console.log(err);
// Pass the error back to the caller
if (typeof callback === 'function') {
callback(err);
}
return;
}
// If there was no error
else {
// Create a filter on all the products returned
async.filter(body.data, function iter(item, callback) {
// If it doesn't have a name
if (!item.name) {
// Give it the axe
callback(false);
return;
}
// It's part of a Tessel + a module
else if (item.name.indexOf('Tessel') !== -1) {
// Axe it
callback(false);
return;
}
else {
item.product_id = item._id;
item.price_formatted = numeral(item.price / 100).format();
item.id = item.slug;
// Otherwise, this is a legit module
callback(true);
}
}, function done(results) {
results.sort(function(a, b) {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1;
}
// a must be equal to b
return 0;
});
results=results.concat(results.splice(results.indexOf('DIY'), 3))
// The filtering is complete
if (typeof callback === 'function') {
// Return the valid modules
callback(null, results);
}
return;
});
}
});
}
function updateOrder(orderID, modules, address, callback) {
// Create a Celery connection to the Tessel 2 Celery account
var celery = new Celery({
"key" : process.env.CELERY_TOKEN_T2,
"version" : 2
});
var orderDetails;
//Get origonal order body
celery.request("/orders/"+ orderID, function(err, body){
if(err || (body.meta.code && body.meta.code !== 200)){
callback(err || body.meta.error.message);
}
orderDetails = body.data;
// Add the modules to the order
if (modules.length) {
orderDetails.line_items = orderDetails.line_items.concat(modules);
}
// update address
orderDetails.shipping_address = address;
// Make a request to update the order
celery.request({
'method' : 'PUT',
'url': 'orders/' + orderID.toString(),
'body': orderDetails},
function(err, body) {
if (typeof callback === 'function') {
if (body.meta.code !== 200) {
callback(new Error("Uh oh! Something went wrong:" + body.meta.error.message));
} else {
// Call the callback
callback(err);
}
}
});
});
}
module.exports.getExistingOrderDetails = getExistingOrderDetails;
module.exports.getAvailableModules = getAvailableModules;
module.exports.updateOrder = updateOrder;