forked from laaksoharju/burger-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataHandler.js
116 lines (98 loc) · 3.22 KB
/
dataHandler.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
'use strict';
let csv = require("csvtojson");
let ingredientsDataName = "ingredients";
let transactionsDataName = "transactions";
let defaultLanguage = "en";
// Store data in an object to keep the global namespace clean
function Data() {
this.data = {};
this.orders = {};
this.currentOrderNumber = 0;
}
Data.prototype.getUILabels = function (lang) {
var ui = require("./data/ui_" + (lang || defaultLanguage) + ".json");
return ui;
};
/*
Returns a JSON object array of ingredients with the fields from
the CSV file, plus a calculated amount in stock, based on
transactions.
*/
Data.prototype.getIngredients = function () {
var d = this.data;
return d[ingredientsDataName].map(function (obj) {
obj.stock = d[transactionsDataName].reduce(function (sum, trans) {
if (trans.ingredient_id === obj.ingredient_id) {
return sum + trans.change;
} else {
return sum;
}
}, 0);
return obj;
});
};
/*
Function to load initial data from CSV files into the object
*/
Data.prototype.initializeTable = function (table) {
csv({checkType: true})
.fromFile("./data/" + table + ".csv")
.then((jsonObj) => {
//console.log("JSON object", jsonObj, "done");
this.data[table] = jsonObj;
});
};
Data.prototype.initializeData = function() {
console.log("Starting to build data tables");
// Load initial ingredients. If you want to add columns, do it in the CSV file.
this.initializeTable(ingredientsDataName);
// Load initial stock. Make alterations in the CSV file.
this.initializeTable(transactionsDataName);
}
/*
Adds an order to to the queue and makes an withdrawal from the
stock. If you have time, you should think a bit about whether
this is the right moment to do this.
*/
Data.prototype.getOrderNumber = function () {
this.currentOrderNumber += 1;
return this.currentOrderNumber;
}
Data.prototype.addOrder = function (order) {
var orderId = this.getOrderNumber();
this.orders[orderId] = order.order;
this.orders[orderId].orderId = orderId;
this.orders[orderId].status = "not-started";
var transactions = this.data[transactionsDataName],
//find out the currently highest transaction id
transId = transactions[transactions.length - 1].transaction_id,
i = order.order.ingredients,
k;
for (k = 0; k < i.length; k += 1) {
transId += 1;
transactions.push({transaction_id: transId,
ingredient_id: i[k].ingredient_id,
change: - 2});
}
return orderId;
};
Data.prototype.changeStock = function (item, saldo) {
var transactions = this.data[transactionsDataName]
var transId = transactions[transactions.length - 1].transaction_id
transactions.push({transaction_id: transId,
ingredient_id: item.ingredient.ingredient_id,
change: saldo - item.ingredient.stock});
};
Data.prototype.getAllOrders = function () {
return this.orders;
};
Data.prototype.markOrderDone = function (orderId) {
this.orders[orderId].status = "done";
};
Data.prototype.markOrderStarted = function (orderId) {
this.orders[orderId].status = "started";
};
Data.prototype.markOrderNotStarted = function (orderId) {
this.orders[orderId].status = "not-started";
};
module.exports = Data;