-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.js
97 lines (92 loc) · 3.14 KB
/
inventory.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
// Licensed under the Apache License. See footer for details.
var helper = require("./helper.js");
var winston = require("winston");
var async = require("async");
var _ = require("underscore");
module.exports = function (Inventory) {
helper.simpleCrud(Inventory);
helper.hideRelation(Inventory, "demo");
/**
* Ensures we have inventory data for all distribution centers,
* all retailers, all products within this demo environment.
*/
Inventory.initializeAllInventories = function(demoId, cb) {
winston.info("Initializing all inventory data...");
async.waterfall([
// get all products
function (callback) {
Inventory.app.models.Product.find(function (err, products) {
callback(err, products);
});
},
// get all distribution centers
function (products, callback) {
Inventory.app.models.DistributionCenter.find(function (err, distributionCenters) {
callback(err, products, distributionCenters);
});
},
// get all retailers for this demo
function (products, distributionCenters, callback) {
Inventory.app.models.Retailer.find({
where: {
demoId: demoId
}
}, function (err, retailers) {
callback(err, products, distributionCenters, retailers);
});
},
// get all existing inventory lines
function (products, distributionCenters, retailers, callback) {
Inventory.find({
where: {
demoId: demoId
}
}, function (err, inventories) {
callback(err, products, distributionCenters, retailers, inventories);
});
},
// create all missing inventory lines
function (products, distributionCenters, retailers, inventories, callback) {
var createInventoryLines = [];
products.forEach(function (product) {
distributionCenters.forEach(function (dc) {
if (!_.findWhere(inventories, {
productId: product.id,
locationId: dc.id,
locationType: "DistributionCenter",
demoId: demoId
})) {
createInventoryLines.push({
productId: product.id,
quantity: 100,
locationId: dc.id,
locationType: "DistributionCenter",
demoId: demoId
});
}
});
retailers.forEach(function (retail) {
if (!_.findWhere(inventories, {
productId: product.id,
locationId: retail.id,
locationType: "Retailer",
demoId: demoId
})) {
createInventoryLines.push({
productId: product.id,
quantity: 0,
locationId: retail.id,
locationType: "Retailer",
demoId: demoId
});
}
});
});
winston.info("Creating", createInventoryLines.length, "inventory lines");
helper.bulk(Inventory, createInventoryLines, callback);
}
], function (err, result) {
cb(err);
});
}
};