-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreserveService.js
59 lines (50 loc) · 1.56 KB
/
reserveService.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
// reserveService.js
// item reserve service
// ==================
// maximal number of reserving items
const MAX_ITEMS = process.env.RESERVE_ITEMS_MAXCOUNT || 5;
class ReserveService {
/**
* Constructor for initializing Service
* @constructor
* @param {array} itemIds - list of item ids
*/
constructor(itemIds) {
console.log('Reserving: ' + itemIds);
this.itemIds = itemIds;
this.usedList = [];
this.orderList = [];
}
reset() {
this.usedList = [];
this.orderList = [];
}
readAllOrders() {
return this.orderList;
}
pushOrder(order) {
this.orderList.push(order);
}
checkReserve(item) {
if (!this.itemIds.includes(`${item.item.item_id}`)) {
// ignore this item
console.log('Ignore: ' + item.item.item_id);
return -1;
}
// check whether item was already reserved
if (!this.usedList.includes(`${item.item.item_id}`)) {
console.log(`${new Date().toLocaleString("de-DE")}| ${item.item.item_id}: Reserving ${item.display_name}.`);
this.usedList.push(`${item.item.item_id}`);
if (item.items_available > MAX_ITEMS) {
return MAX_ITEMS;
} else {
return item.items_available;
}
} else {
// already reserved
console.log(`${new Date().toLocaleString("de-DE")}| ${item.item.item_id}: Already reserved today.`);
return -1;
}
}
}
module.exports = ReserveService;