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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
var express = require('express');
var router = express.Router();
var debug = require('debug')('debug')
const { v4: uuidv4 } = require('uuid');
router.get('/', function(req, res, next) {
res.send('list of referrals...');
});
// Grant & Retrieve Referral tokens for existing user
// usage: GET '/grant?sponsor_id=N&expiry=2021-09-01'
//
// Required:
// sponsor_id (= user.id)
// Changes to DB:
// adds record to referrals table:
// { token: <uuid>, sponsor_id: <user.id>, status: "available" }
/**
* @api {post} /grant Grant Referral code
* @apiName Grant
* @apiGroup Referrals
*
*
* @apiPermission member
*
* @apiParam {sponsor_id} user_id of member generating referral code.
* @apiParam {expiry} optional expiry date for token.
*
* @apiSuccess {token: referral_code, expiry: expiry} Token and expiry if applicable.
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "token": "*********",
* "status": "available",
* "expiry": null
* }
*/
router.post(
"/grant",
auth,
ah(async (req, res) => {
const {sponsor_id, expiry} = req.body
debug('grant token..')
var token = uuidv4()
debug('generated token: ' + token + ' sponsored by ' + sponsor_id)
var ref = await db.Referral.create({
sponsor_id: sponsor_id,
token: token,
status: 'available'
})
debug('generated referral: ' + JSON.stringify(ref))
return res.send({token: token, status: 'available', expiry: expiry})
})
);
/**
* @api {get} /grant Grant Referral code (variation using get)
* @apiName Grant
* @apiGroup Referrals
*
* @apiPermission member
*
* @apiPermission member
* @apiParam {sponsor_id} user_id of member generating referral code.
* @apiParam {expiry} optional expiry date for token.
*
* @apiSuccess {token: referral_code, status: status, expiry: expiry} Token and expiry if applicable.
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "token": "*********",
* "status": "available",
* "expiry": null
* }
*/
router.get(
"/grant",
auth,
ah(async (req, res) => {
const {sponsor_id, expiry} = req.query
debug('grant token..')
var token = uuidv4()
debug('generated token: ' + token + ' sponsored by ' + sponsor_id)
var ref = await db.Referral.create({
sponsor_id: sponsor_id,
token: token,
status: 'available'
})
debug('generated referral: ' + JSON.stringify(ref))
return res.send({token: token, status: 'available', expiry: expiry})
})
);
/**
* @api {get} /checkTokens/:sponsor_id Retrieve list of referral tokens generated by this user
*
* @apiName checkTokens
* @apiGroup Referrals
*
*
* @apiPermission member
*
* @apiParam {sponsor_id} user_id of member generating referral code.
* @apiParam {status} optional token status.
*
* @apiSuccess tokens array of token objects
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "tokens": [
* {"token": "*********", "status": "available", "expiry": null}
* ]
* }
*/
// TO FIX - change sponsor_id so that it is retrieved from current payload (NOT FROM URL)
router.get(
"/checkTokens/:sponsor_id",
auth,
ah(async (req, res) => {
const {sponsor_id} = req.params
const {status} = req.query
var tokens = knex
.select(
'token',
'created_at',
knex.raw('LEFT(created_at,10) as created'),
'username',
'status'
)
.from('referrals')
.leftJoin('users', 'users.id', 'referrals.user_id')
.where('sponsor_id', 'like', sponsor_id)
if (status && status !== 'all') tokens = tokens.where('status', 'like', status)
console.log(sponsor_id + ' : ' + status)
const found = await tokens
debug('my tokens: ' + JSON.stringify(found))
return res.send({tokens: found})
})
);
/**
* @api {get} /verify/:user_id Verify token and apply to existing user
*
* @apiName verify
* @apiGroup Referrals
*
* @apiPermission member
*
* @apiParam {user_id} Current user
* @apiParam {token} Token to validate
*
* @apiSuccess verified = true
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "verified": true
* }
*
* @apiDescription referral token is updated from 'available' to 'used'
* @apiDescription referral token is updated with existing user_id
*/
router.get(
"/verify/:user_id/:token",
auth,
ah(async (req, res) => {
const { user_id, token } = req.params;
debug('verify token: ' + token)
const found = await db.Referral.findAll({
attributes: ['status'],
where: {
token: token,
user_id: null
}
})
if (found && found.length) {
debug('found referral: ' + JSON.stringify(found))
if (found[0].status === 'available') {
await db.Referral.update(
{
status: 'used',
user_id: user_id,
updated_at: new Date().toISOString().substring(0,10)
},
{
where: { token: token }
}
)
return res.send({ verified: true, sponsor_id: found.sponsor_id, updated: found.updated_at});
} else {
res.status(500).send({ verified: false, message: 'Referral already ' + found[0].status })
}
} else {
res.status(500).send('Invalid referral token')
}
})
);
/**
* @api {post} /joinQueue Join waiting list (track email & phone)
* @apiName joinQueue
* @apiGroup Referrals
*
* @apiParam {email}
* @apiParam {phone}
*
* @apiSuccess verified = true
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "success": true
* "message": 'Added to waiting list'
* }
*
* @apiDescription referral token is updated from 'available' to 'used'
* @apiDescription referral token is updated with existing user_id
*/
router.post(
"/joinQueue",
ah(async (req, res) => {
const { email, phone } = req.body;
debug('email: ' + email)
debug('phone: ' + phone)
console.log(email + ' BODY: ' + JSON.stringify(req.body))
await db.WaitingList.create({
email: email,
phone: phone
})
res.send({success: true, message: 'Added ' + email + ' to waiting list ' + phone})
})
);
router.get(
"/joinQueue",
ah(async (req, res) => {
const {email, phone} = req.query;
debug('email: ' + email)
debug('phone: ' + phone)
console.log(email + ' BODY: ' + JSON.stringify(req.body))
await db.WaitingList.create({
email: email,
phone: phone
})
res.send({success: true, message: 'Added ' + email + ' to waiting list ' + phone})
})
);
/**
* @api {get} /isReferred/:user_id Check if user is referred
* @apiName isReferred
* @apiGroup Referrals
*
* @apiParam {user_id}
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* true
* }
*
* @apiDescription returns boolean indicating if user is referred or not
*/
router.get(
"/isReferred/:user_id",
ah(async (req, res) => {
const { user_id } = req.params;
var referred = await db.Referral.findOne({
where: {
user_id: user_id,
status: 'used'
}
})
if (referred) {
res.send(true)
} else {
res.send(false)
}
})
);
module.exports = router;
|