This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewards.js
87 lines (79 loc) · 2.63 KB
/
rewards.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
const db = require('./db')
function getOffer(value, quantity, check_item_guid_map, redemptions_id_quantity_map) {
var reward = db.find('rewards', {id: value});
if (reward == null) throw "ERROR_REWARD_DOES_NOT_EXIST";
return toOffer(reward, quantity, check_item_guid_map, redemptions_id_quantity_map);
}
function toOffer(reward, quantity, check_item_guid_map, redemptions_id_quantity_map) {
var check = checkApplicable(reward, quantity, check_item_guid_map, redemptions_id_quantity_map);
var offer = {};
var amount = 0.01;
if (reward.type != "PERCENT") {
amount = reward.amount;
}
if (reward.type == "RANDOM") {
amount = Math.floor(Math.random() * amount * 100)/100;
}
offer.identifier = reward.id;
offer.name = reward.name;
offer.applicable = check.applicable;
offer.selectionType = reward.scope;
offer.amount = amount
offer.expiration = [];
if (reward.expiryDate != null) {
expirationDate = {
date: reward.expiryDate,
quantity: quantity
}
offer.expiration.push(expirationDate);
}
if ((offer.selectionType == "ITEM" || offer.selectionType == "MULTI_ITEM") && check.item_id != null && check.applicable) {
offer.itemApplication = [];
offer.amount = 0
offer.quantity = 1
check.item_id.forEach(function(application) {
var itemInfo = {};
itemInfo.selectionIdentifier = application.selectionGUID
itemInfo.amount = application.amount
offer.amount += Number(application.amount)
offer.itemApplication.push(itemInfo);
})
}
offer.quantity = quantity > 0 ? quantity : 0;
return offer;
}
// Simple Rules:
// 1. reward available
// 2. each reward can only be used once per check
// 3. the item is available in the check
// 4. the reward is always apply to the first item available in the list and the check
function checkApplicable(reward, quantity, check_item_guid_map, redemptions_id_quantity_map) {
var result = {}
result.item_id = []
var itemsApplied = reward.item_id;
if (quantity <= 0) {
result.applicable = false;
return result;
}
if (itemsApplied.length == 0) {
result.applicable = true;
return result;
}
if (reward.scope == "MULTI_ITEM" || reward.type == "ITEM") {
result.item_id = []
for ( i in itemsApplied) {
var item = itemsApplied[i]
var id = item.menuItemGuid
if (!check_item_guid_map[id]) {
result.applicable = false;
return result;
}
result.item_id.push({"selectionGUID":check_item_guid_map[id][0], "amount":item.amount})
}
result.applicable = true;
return result;
}
result.applicable = false;
return result;
}
module.exports = {getOffer}