forked from pravega-web/pravega
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit.js
171 lines (150 loc) · 4.65 KB
/
credit.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
159
160
161
162
163
164
165
166
167
168
169
170
171
const cors = require('cors');
module.exports = function (app) {
// v1 - Accepting CA code only
app.post("/api/cf/register", cors(), (req, res) => {
// Call an async function so main thread can continue.
creditCa(req.body);
// Does it matter what you send them ?
res.send("OK");
});
// Crediting CA
app.post("/api/ca/credit", (req, res) => {
user.findOne({ refId: req.body.refId }, (err, data) => {
if (err) throw { err };
if (data) {
data.ca.credit += req.body.credit;
user.update({ _id: data._id }, (err1, updata) => {
if (err) throw err;
res.send(data);
});
} else {
res.send("Invalid reference id");
}
});
});
// Vendor
app.post("/api/credit", (req, res) => {
if (mongoose.Types.ObjectId.isValid(req.body._id)) {
user.find({ _id: req.body._id }, (err, data) => {
if (err) throw err;
if (data[0].meta.trId == req.body.trId) {
if (data[0].meta.credit > req.body.credit) {
var upd = data[0];
upd.meta.credit = data[0].meta.credit - req.body.credit;
upd.meta.trId = Math.floor(Math.random() * 10000);
user.findOneAndUpdate(
{ _id: data[0]._id },
upd,
(err1, updated) => {
if (err1) throw err1;
res.send("Success");
}
);
} else {
res.send("Insufficient credits");
}
} else {
res.send("Transaction ID incorrect!");
}
});
} else {
console.log(req.body._id);
res.send("Invalid ID");
}
});
}
// Credit CA
// MOST Important Function as of now
async function creditCa(receipt) {
var oldRegistration = await cfreg.findOne({ ticketId: receipt.ticketId });
if (oldRegistration) {
// Yell at CF
console.log("Dupes Error. TCF get it fixed");
logit("DUPES ERROR", "TCF GET IT FIXED");
} else {
var newCfReg = await cfreg.create({
ticketId: receipt.ticketId,
ticket: receipt
});
// Iterate through the list of tickets
for (const ticket of receipt.ticketItems) {
// Find CA based on refernce code
refId = parseInt(ticket.attendee.customTField0);
logit("INFO", "Incoming Registration : " + receipt.ticketId);
if (typeof refId == "number" && !isNaN(refId)) {
var ca = await user.findOne({ refId: refId });
console.log("CA :", ticket.attendee.customTField0);
// If found ( valid reference ID )
if (ca) {
// Credit
credit = Math.min(
max_val,
Math.max(min_val, Math.round(caCreditMultiplier * ticket.fare))
);
console.log(
"CA: ",
ticket.attendee.customTField0,
" | credited : ",
credit
);
// Math.min(50, Math.round(caCreditMultiplier * ticket.fare))
// Increase the credit and update the CA's account
ca.ca.credit += credit;
ca.meta.credit += credit;
record = {
name: ticket.attendee.name,
event: ticket.programName,
credit: credit,
timeStamp: new Date()
};
if (!ca.meta.refs) {
ca.meta.refs = [];
ca.meta.refs.push(record);
} else {
ca.meta.refs.push(record);
}
var upd = await user.updateOne({ _id: ca._id }, ca);
console.log(upd.nModified);
if (upd.nModified == 1) {
logit(
"INFO",
"CA :" + ticket.attendee.customTField0 + " CREDITED : " + credit
);
} else {
logit(
"ERROR - 2",
"CA :" +
ticket.attendee.customTField0 +
" NOT CREDITED : " +
credit
);
}
// 1 - Success, 0 - Failure
ca = {};
} else {
// Send to server ?
console.log("CA CODE: NoT FounD");
logit(
"INFO",
"CA Code Invalid (Referred and Wrong) :" +
ticket.attendee.customTField0
);
}
} else {
console.log("INVALID CA CODE");
logit(
"INFO",
"CA Code Invalid (Not referred) :" + ticket.attendee.customTField0
);
}
}
// Verify async is actually async
// console.log('Done!')
// var lol = await user.findOne({ 'refId': ticket.attendee.refId });
// lol.ca.credit += caCreditMultiplier * 100
// user.updateOne({_id:lol._id},lol, (err,updRes)=>{
// if(err) throw(err);
// console.log(updRes.nModified)
// })
}
}