-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.stock.order.js
355 lines (323 loc) · 16.6 KB
/
generate.stock.order.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
var SUCCESS = 0;
var FAILURE = 1;
var REPORT_EMPTY = 'report_empty';
var MANAGER_NEW_ORDERS = 'manager_new_orders';
var MANAGER_IN_PROCESS = 'manager_in_process';
var WAREHOUSE_FULFILL = 'warehouse_fulfill';
var MANAGER_RECEIVE = 'manager_receive';
var REPORT_COMPLETE = 'report_complete';
var BOXED = 'boxed';
try {
var fs = require('fs');
var utils = require('./jobs/utils/utils.js');
var path = require('path');
var Promise = require('bluebird');
var _ = require('underscore');
// Global variable for logging
var commandName = path.basename(__filename, '.js'); // gives the filename without the .js extension
var params = null;
var task_id = null;
var config = null;
console.log(commandName, process.argv);
process.argv.forEach(function (val, index, array) {
if (val == '-payload') {
params = JSON.parse(fs.readFileSync(process.argv[index + 1], 'utf8'));
}
if (val === '-config') {
config = JSON.parse(fs.readFileSync(process.argv[index + 1], 'utf8'));
}
if (val === '-id') {
task_id = process.argv[index + 1];
}
});
console.log(commandName, 'params:', params);
console.log(commandName, 'config:', config);
console.log(commandName, 'task_id:', task_id);
try {
process.env['User-Agent'] = task_id + ':' + commandName + ':' + params.domainPrefix;
return utils.savePayloadConfigToFiles(params)
.then(function () {
try {
var nconf = require('nconf');
nconf.file('client', { file: 'config/client.json' })
.file('oauth', { file: 'config/oauth.json' });
console.log(commandName, 'nconf:', nconf.get());
// HACK starts: dynamically set remote datasource URL
var datasourcesFile = path.join(__dirname, 'client', 'datasources.json');
console.log(commandName, 'datasourcesFile: ' + datasourcesFile);
fs.writeFileSync(datasourcesFile,
JSON.stringify({
"db": {
"name": "db",
"connector": "memory"
},
"remoteDS": {
"url": params.loopbackServerUrl + '/api',
"name": "remoteDS",
"connector": "remote"
}
}, null, 2));
var datasourcesContent = require(datasourcesFile);
console.log(commandName, 'datasourcesContent: ' + JSON.stringify(datasourcesContent, null, 2));
// HACK ends
var client = require('./client/loopback.js');
// the remote datasource
var remoteDS = client.dataSources.remoteDS;
/*console.log('before', remoteDS);
console.log('before', remoteDS.url);
remoteDS.url = params.loopbackServerUrl;
console.log('after', remoteDS.url);*/
// the strong-remoting RemoteObjects instance
var remotes = remoteDS.connector.remotes;
// TODO: (2) figure out the total # of pages we will be dealing with
// ex: 42 pages total
// TODO: (3) run the report for totalPages/5 pages
// ex: page 1-5
// TODO: (4) queue the next job to work on the res of the pages
// ex: start at page 6/42, work on pages 6-10
// TODO: (5) last job to run should change the state from empty to new_orders
// ex: whomever process pages 40-42
return Promise.resolve(params)
.then(function (params) { // (1) create a report if params.reportId is empty
console.log(commandName, 'WTF');
if (params.reportId === undefined || params.reportId === null) {
console.log(commandName, 'need to create a new report');
return client.models.UserModel.loginAsync(params.credentials) // get an access token
.then(function(token) {
console.log('Logged in as', params.credentials.email);
params.loopbackAccessToken = token;
// set the access token to be used for all future invocations
console.log(commandName, 'params.loopbackAccessToken.id', params.loopbackAccessToken.id);
console.log(commandName, 'params.loopbackAccessToken.userId', params.loopbackAccessToken.userId);
remotes.auth = {
bearer: (new Buffer(params.loopbackAccessToken.id)).toString('base64'),
sendImmediately: true
};
console.log(commandName, 'the access token to be used for all future invocations has been set');
return Promise.resolve();
})
.then(function(){
return client.models.ReportModel.createAsync({
userModelToReportModelId: params.loopbackAccessToken.userId, // explicitly setup the foreignKeys for related models
state: REPORT_EMPTY,
outlet: {
id: params.outletId,
name: params.outletName // TODO: fetch via an api call instead?
},
supplier: {
id: params.supplierId,
name: params.supplierName // TODO: fetch via an api call instead?
}
});
})
.then(function (reportModelInstance) {
console.log('new reportModelInstance:', reportModelInstance);
params.reportId = reportModelInstance.id; // save the new report id into the params
return Promise.resolve();
});
}
else {
console.log(commandName, 'report already exists');
// set the access token to be used for all future invocations
console.log(commandName, 'params.loopbackAccessToken.id', params.loopbackAccessToken.id);
console.log(commandName, 'params.loopbackAccessToken.userId', params.loopbackAccessToken.userId);
remotes.auth = {
bearer: (new Buffer(params.loopbackAccessToken.id)).toString('base64'),
sendImmediately: true
};
console.log(commandName, 'the access token to be used for all future invocations has been set');
return Promise.resolve();
}
})
.then(function decideOp () {
// NOTE: no time to investigate why we end up accidently nuking our foreign-keys
// later on somwhere in code ... when we use this shortcut to avoid one extra server call
//var reportModelInstance = new client.models.ReportModel({id: params.reportId});
return client.models.ReportModel.findByIdAsync(params.reportId)
.then(function(reportModelInstance){
var stockOrderLineitemModels = Promise.promisifyAll(
reportModelInstance.stockOrderLineitemModels,
{
filter: function(name, func, target){
return !( name == 'validate');
}
}
);
if (params.op) {
console.log('Will run the OP for: ' + params.op);
return Promise.resolve(params.op);
}
else {
return stockOrderLineitemModels.countAsync()
.then(function (count) {
console.log('inside decideOp(), count:', count);
if (count > 0) { // if rows already exist, it means the raw data was imported already
console.log('Will run the OP for: importStockOrder');
return Promise.resolve('importStockOrder');
} else { // if rows do NOT exist, means we have to get the raw material ourselves
console.log('Will run the OP for: generateStockOrder');
return Promise.resolve('generateStockOrder');
}
});
}
});
})
.tap(function removeUnfulfilledProducts (methodName) {
if(methodName !== 'removeUnfulfilledProducts') {
console.log('will skip removeUnfulfilledProducts');
return Promise.resolve();
}
else {
// NOTE: no time to investigate why we end up accidently nuking our foreign-keys
// later on somwhere in code ... when we use this shortcut to avoid one extra server call
//var reportModelInstance = new client.models.ReportModel({id: params.reportId});
return client.models.ReportModel.findByIdAsync(params.reportId)
.then(function(reportModelInstance){
var stockOrderLineitemModels = Promise.promisifyAll(
reportModelInstance.stockOrderLineitemModels,
{
filter: function(name, func, target){
return !( name == 'validate');
}
}
);
var where = { // only work with rows that need to be deleted
//reportId: params.reportId, // don't need this, lineitem object is a relational instance already
or: [
{fulfilledQuantity: null},
{fulfilledQuantity: {exists: false}},
{fulfilledQuantity: {lte: 0}}
],
vendConsignmentProductId: {ne: null}
};
return stockOrderLineitemModels.countAsync(where)
.then(function (count) {
var pageSize = 200;
var totalPages = Math.ceil(count / pageSize);
console.log('Will traverse %d rows by fetching %d page(s) of size <= %d', count, totalPages, pageSize);
var pseudoArrayToIterateOverPagesSerially = new Array(totalPages);
for (var i=0; i<totalPages; i++) {
pseudoArrayToIterateOverPagesSerially[i] = i+1;
}
// constraint Promise.map with concurrency of 1 around pseudoArrayIterateAllPages
return Promise.map(
pseudoArrayToIterateOverPagesSerially,
function (pageNumber) {
return client.models.ReportModel.getRowsAsync(params.reportId, pageSize, pageNumber, where)
.then(function (lineitems) {
console.log('total lineitems retrieved for page #%d: %d', pageNumber, lineitems.length);
return Promise.map(
lineitems,
function (lineitem) {
var deleteStockOrderRow = require('./jobs/delete-stock-order-row.js');
return deleteStockOrderRow.run(lineitem.vendConsignmentProductId)
.catch(function(error) {
console.error('removeUnfulfilledProducts dot-catch block');
console.log(commandName, 'ERROR', error);
return Promise.resolve(); // ignore the error to keep going
});
},
{concurrency: 1}
)
.then(function () {
console.log('done deleting ALL lineitems for page #%d', pageNumber);
return Promise.resolve();
});
});
},
{concurrency: 1}
)
.then(function () {
console.log('all pages done');
console.log('done with removeUnfulfilledProducts op');
return Promise.resolve();
});
});
});
}
})
.tap(function generateStockOrder (methodName) {
if(methodName !== 'generateStockOrder') {
console.log('will skip generateStockOrder');
return Promise.resolve();
}
else {
var generateStockOrder = require('./jobs/generate-stock-order.js');
return generateStockOrder.run(params.reportId, params.outletId, params.supplierId, params.loopbackAccessToken.userId)
.then(function (rows) {
console.log(commandName, 'rows.length', rows.length);
// split rows to be saved in chunks of 500
var i, rowChunks=[], chunkSize = 500;
for (i=0; i<Math.ceil(rows.length/chunkSize); i+=1) {
console.log(commandName, 'slice from index', i*chunkSize, 'up to but not including', i*chunkSize+chunkSize);
rowChunks.push(rows.slice(i*chunkSize,i*chunkSize+chunkSize));
}
console.log(commandName, 'rowChunks.length', rowChunks.length);
return Promise.map(
rowChunks,
function (aChunkOfRows) {
console.log(commandName, 'Will create a chunk of lineitems with length:', aChunkOfRows.length);
return client.models.StockOrderLineitemModel.createAsync(aChunkOfRows)
.tap(function (stockOrderLineitemModelInstances) {
// TODO: file a bug w/ strongloop support, the data that comes back
// does not represent the newly created rows in size accurately
console.log(commandName, 'Created a chunk of lineitems with length:', _.keys(stockOrderLineitemModelInstances).length);
});
},
{concurrency: 1}
)
.then(function () {
// if the lineitems saved properly then move the STATE to the next stage
return client.models.ReportModel.findByIdAsync(params.reportId)
.then(function (reportModelInstance) {
console.log(commandName, 'Found the ReportModel...');
console.log(commandName, reportModelInstance);
reportModelInstance.state = MANAGER_NEW_ORDERS;
reportModelInstance.totalRows = rows.length;
return client.models.ReportModel.updateAllAsync(
{id: params.reportId},
reportModelInstance
)
.tap(function (info) {
console.log(commandName, 'Updated the ReportModel...');
console.log(commandName, info);
});
});
})
.catch(function (error) {
console.error('3rd last dot-catch block');
console.log(commandName, 'ERROR', error);
return Promise.reject(error);
});
});
}
})
.catch(function (error) {
console.error('2nd last dot-catch block');
console.log(commandName, 'ERROR', error);
return Promise.reject(error);
});
}
catch (e) {
console.error('3rd last catch block');
console.error(commandName, e);
// TODO: throw or float up promise chain or just exit the worker process here?
}
})
.catch(function (error) {
console.error('last dot-catch block');
console.log(commandName, 'ERROR', error);
process.exit(FAILURE); // this is the last one so exit the worker process
});
}
catch (e) {
console.error('2nd last catch block');
console.error(commandName, e);
// TODO: throw or float up promise chain or just exit the worker process here?
}
}
catch (e) {
console.error('last catch block');
console.error(e);
process.exit(FAILURE);
}