-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
158 lines (138 loc) · 4.61 KB
/
app.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
'use strict';
// constants
const successMessage = 'SUCCESS';
const outOfStockMessage = 'OUT OF STOCK';
// create hashtable to contain item names and current quantity in inventory
let inventory = {
skis: 0,
shovel: 0,
sled: 0,
snowblower: 0,
tires: 0
};
// create array of objects containing order data - orders data includes: "order_id", "customer_id", "order_date", "item_ordered", "item_quantity", "item_price"
let orderData = {};
$.ajax({
url: './data/orders.json',
async: false,
dataType: 'json',
success: function(data) {
orderData = data;
}
});
// create array of objects containing restock data - restocks data includes: "restock_date", "item_stocked", "item_quantity", "manufacturer", "wholesale_price"
let restockData = {};
$.ajax({
url: './data/restocks.json',
async: false,
dataType: 'json',
success: function(data) {
restockData = data;
}
});
// create array of objects to create restock data that will trigger Out of Stock
let testRestockData = {};
$.ajax({
url: './data/restocks.failtest.json',
async: false,
dataType: 'json',
success: function(data) {
testRestockData = data;
}
});
// combine data sets into single array and sort by date
function combineEvents(orderEvents, restockEvents){
let combinedEvents = orderEvents.concat(restockEvents);
combinedEvents.forEach(el => el.date = el.order_date || el.restock_date);
combinedEvents.sort(compareOrderDates);
return combinedEvents;
}
// helper function to sort order data according to order date
function compareOrderDates(a, b){
if ( a.date < b.date){
return -1;
}
if ( a.date > b.date){
return 1;
}
return 0;
}
// process events to update inventory table and respond to either out of stock or algorithm success
function processEvents (events){
for(let i=0; i<events.length; i++){
let item;
if(events[i].hasOwnProperty('restock_date')){
item = events[i].item_stocked;
inventory[item]+= events[i].item_quantity;
} else {
item = events[i].item_ordered;
let date = events[i].date;
inventory[item] -= events[i].item_quantity;
if(inventory[item]<0){
displayFailure();
displayFailureEvent(item, date);
return;
}
}
}
displaySuccess();
displaySurplus();
}
// method to populate status message and make visible if successful
function displaySuccess() {
let status = document.getElementById('result');
let statusMessage = document.getElementById('result-text');
status.classList.remove('hidden');
statusMessage.innerHTML = successMessage;
}
// method to populate surplus inventory table if successful
function displaySurplus() {
let inventoryValues = Object.values(inventory);
let inventoryKeys = Object.keys(inventory);
let surplusContainer = document.getElementById('surplus-container');
surplusContainer.classList.remove('hidden');
let surplusTable = document.getElementById('surplus-table');
for(let i=0; i<inventoryValues.length; i++){
if(inventoryValues[i]>0){
let newItem = document.createElement('td');
let newQuant = document.createElement('td');
let newRow = document.createElement('tr');
newItem.textContent = inventoryKeys[i];
newRow.appendChild(newItem);
newQuant.textContent = inventoryValues[i];
newRow.appendChild(newQuant);
surplusTable.appendChild(newRow);
}
}
}
// method to populate status message and make visibile if failure
function displayFailure() {
let status = document.getElementById('result');
let statusMessage = document.getElementById('result-text');
status.classList.remove('hidden');
statusMessage.innerHTML = outOfStockMessage;
}
// method to populate Out of Stock table if failure
function displayFailureEvent(item, date) {
let failureContainer = document.getElementById('out-container');
failureContainer.classList.remove('hidden');
let failureTable = document.getElementById('out-table');
let newItem = document.createElement('td');
let newDate = document.createElement('td');
let newRow = document.createElement('tr');
newItem.textContent = item;
newRow.appendChild(newItem);
newDate.textContent = date;
newRow.appendChild(newDate);
failureTable.appendChild(newRow);
}
// production creation of allEvents
let allEvents = combineEvents(orderData, restockData);
// test creation of allEvents from restocks.failtest.json - uncomment following line and comment above line to run test of out of stock functionality
// let allEvents = combineEvents(orderData, testRestockData);
// main function call
processEvents(allEvents);
// export methods for testing and authorization
module.exports = {
compareOrderDates: compareOrderDates
}