This repository has been archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
472 lines (374 loc) · 16 KB
/
server.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
const config = require('./config');
const express = require("express");
const http = require("http");
const https = require("https");
const bodyParser = require('body-parser');
const fs = require('fs');
const pug = require('pug');
const path = require('path');
const uuidv1 = require('uuid/v1');
const port = process.env.PORT || config.port;
const app = express();
app.use(express.static('public'));
app.use(bodyParser.json());
app.set('view engine', 'pug');
//Splash page
app.get("/", (req, res) => {
res.render('splash');
});
app.get("/ping", (req, res) => {
res.status(200).send();
});
//Provider Reservations endpoint - simulates start of Provider Reservations journey
app.get("/:providerId/reservations", (req, res) => {
let providerId = req.params.providerId;
console.log("Reservation selection for Provider " + providerId);
let viewmodel = {
providerId : providerId,
reservations : [
{
reservationTitle: "Rapid Logistics (non levy)",
reservationSubtitle: "Reservation with for Geospatial Survey Technician (244) June 2019",
accountLegalEntityId: 'X9JE72',
reservationUrl: String.Format("{0}/{1}/unapproved/add-apprentice?reservationId={2}&employerAccountLegalEntityPublicHashedId={3}&courseCode={4}&startMonthYear={5}",
config.providerCommitmentsBaseUrl,
providerId,
uuidv1(),
'X9JE72',
"244",
"062019")
}
]
};
res.render('use-reservation', viewmodel);
});
//Provider Reservation selection page, used in "Add Another Apprentice" journey
//If transfer sender id is included, the endpoint assumes that the sender is a levy payer so the user gets an "auto" reservation
app.get("/:providerId/reservations/:employerId/select", (req, res) => {
let providerId = req.params.providerId;
let employerId = req.params.employerId;
let cohortRef = req.getFromQueryString("cohortReference");
let transferSenderId = req.getFromQueryString("transferSenderId");
let journeyData = req.getFromQueryString("journeydata");
console.log(String.Format("Reservation selection for Provider: {0}, Employer: {1}", providerId, employerId));
if(transferSenderId !== undefined)
{
console.log(String.Format("Transfer Sender {0} indicated", transferSenderId));
}
//simulate levy-payer auto create and redirect
if(config.hashedlevyaccountlegalentities.includes(employerId) || (transferSenderId !== undefined))
{
console.log("Simulating greenlight for levy payer - auto redirecting to add apprentice");
let redirectUrl = String.Format("{0}/{1}/unapproved/{2}apprentices/add?reservationId={3}&employerAccountLegalEntityPublicHashedId={4}{5}{6}&autocreated=true",
config.providerCommitmentsBaseUrl,
providerId,
cohortRef === undefined ? "" : cohortRef + "/",
uuidv1(),
employerId,
transferSenderId === undefined ? "" : "&transferSenderId=" + transferSenderId,
journeyData === undefined ? "" : "&journeydata=" + journeyData
);
res.redirect(redirectUrl);
return;
}
//non-levy payer must select a reservation
let viewmodel = {
providerId : providerId,
cohortRef: cohortRef,
reservations : [
{
reservationTitle: "Geospatial Survey Technician (244) June 2019",
reservationSubtitle: "",
accountLegalEntityId: employerId,
reservationDescription: "",
reservationUrl: String.Format("{0}/{1}/unapproved/{2}apprentices/add?reservationId={3}&employerAccountLegalEntityPublicHashedId={4}&courseCode={5}&startMonthYear={6}{7}",
config.providerCommitmentsBaseUrl,
providerId,
cohortRef === undefined ? "" : cohortRef + "/",
uuidv1(),
employerId,
"244",
"062019",
journeyData === undefined ? "" : "&journeydata=" + journeyData
)
}
]
};
res.render('select-reservation', viewmodel);
});
/* new endpoints to go here */
app.get('/api/accounts/:accountId/status',(req, res) => {
let accountId = req.params.accountId;
let transferSenderId = req.getFromQueryString("transferSenderId");
if(config.levyaccounts.includes(accountId) || config.levyaccounts.includes(transferSenderId))
{
sendFile(res, '/api/status-auto.json');
return;
}
sendFile(res, '/api/status-manual.json');
});
app.post('/api/reservations/accounts/:accountLegalEntityId/bulk-create', (req, res) => {
let accountLegalEntityId = req.params.accountLegalEntityId;
let requestedCount = req.getFromBody("count");
let transferSenderId = String(req.getFromBody("transferSenderId"));
if(config.levyaccountlegalentities.includes(accountLegalEntityId) || config.levyaccounts.includes(transferSenderId))
{
let result = {
reservationIds: []
};
for(let i=0; i<requestedCount;i++)
{
result.reservationIds[i] = uuidv1()
}
res.status(200).send(result);
return;
}
res.status(400).send('Non-Levy accounts cannot bulk create reservations');
});
//Reservations API Change of Party endpoint
//TODO
app.post('/api/reservations/:reservationId/change', (req, res) => {
let reservationId = req.params.reservationId;
console.log(String.Format("Change-of-Party request for {0}", reservationId));
let newReservationId = uuidv1();
let result = String.Format("{ reservationId: \"{0}\" }", newReservationId);
res.status(200).send(result);
});
//Reservations API validation endpoint - new!!
app.get('/api/reservations/validate/:reservationId*',(req, res) => {
let course = req.getFromQueryString("courseCode");
let startDate = new Date(req.getFromQueryString("startDate"));
console.log(String.Format("Validation request v2 for Course: {0}, StartDate: {1}", course, startDate));
//we can't do this green-light simulation because accountId is no longer included
//Levy payer gets a green light
/*
if(config.levyaccounts.includes(accountId))
{
console.log("Reservation validation request from levy payer - simulates auto-create-reservation");
sendFile(res, '/api/okResponse.json');
return;
}
*/
//Non-levy payer - baked in failures for Funeral Director Course and Jan 19 Start Dates
if(course === "411")
{
console.log("Course 411 - simulates course error (v2)");
sendFile(res, '/api/courseErrorResponse.json');
return;
}
if(dates.compare(startDate, new Date("2019-01-01")) === 0)
{
console.log("01 Jan 2019 - simulates start date error (v2)");
sendFile(res, '/api/startDateErrorResponse.json');
return;
}
//default to ok for happy testing
sendFile(res, '/api/okResponse.json');
});
/* employer routes */
//Employer Reservations endpoint - simulates start of Employer Reservations journey
app.get("/accounts/:accountId/reservations", (req, res) => {
let accountId = req.params.accountId;
console.log("Reservation selection for Employer Account: " + accountId);
if(config.hashedLevyAccounts.includes(accountId))
{
res.status(400).send('This endpoint is not appropriate for levy paying employers, who have no reservations UI');
return;
}
let reservations = config.employerReservations.filter(function(item){ return item.accountId === accountId });
let viewmodel = {
reservations: []
};
reservations.forEach(reservation => {
viewmodel.reservations.push({
reservationTitle: reservation.title,
reservationSubtitle: reservation.subtitle,
accountLegalEntityId: reservation.accountLegalEntityId,
reservationUrl: String.Format("{0}/{1}/unapproved/add?reservationId={2}&accountLegalEntityHashedId={3}&courseCode={4}&startMonthYear={5}",
config.employerCommitmentsBaseUrl,
accountId,
uuidv1(),
reservation.accountLegalEntityId,
reservation.courseCode,
reservation.startMonthYear
)
});
});
res.render('employer-use-reservation', viewmodel);
});
//Employer Reservation selection page, used in "Add Another Apprentice" journey
//If transfer sender id is included, the endpoint assumes that the sender is a levy payer so the user gets an "auto" reservation
//Provider Select Reservation endpoint
app.get("/accounts/:accountId/reservations/:legalEntityId/select", (req, res) => {
let employerId = req.params.accountId;
let legalEntityId = req.params.legalEntityId;
let cohortRef = req.getFromQueryString("cohortReference");
let transferSenderId = req.getFromQueryString("transferSenderId");
if(transferSenderId === "" ) {transferSenderId = undefined; }
let providerId = req.getFromQueryString("providerId");
let journeyData = req.getFromQueryString("journeydata");
let encodedPledgeApplicationId = req.getFromQueryString("encodedPledgeApplicationId");
if(encodedPledgeApplicationId === "") { encodedPledgeApplicationId = undefined; }
console.log(String.Format("Reservation selection for Employer: {0}, Ale: {1}", employerId, legalEntityId));
if(transferSenderId !== undefined)
{
console.log(String.Format("Transfer Sender {0} indicated", transferSenderId));
if(encodedPledgeApplicationId !== undefined)
{
console.log(String.Format("Pledge Application Id {0} indicated", encodedPledgeApplicationId));
}
}
//simulate levy-payer auto create and redirect (includes transfer receiver)
if(config.hashedLevyAccounts.includes(employerId) || (transferSenderId !== undefined))
{
console.log("Simulating greenlight for levy payer - auto redirecting to add apprentice");
let redirectUrl = String.Format("{0}/{1}/unapproved/{2}?reservationId={3}&accountLegalEntityHashedId={4}{5}{6}{7}{8}&autocreated=true",
config.employerCommitmentsBaseUrl,
employerId,
cohortRef === undefined ? "add/apprentice" : cohortRef + "/apprentices/add",
uuidv1(),
legalEntityId,
transferSenderId === undefined ? "" : "&transferSenderId=" + transferSenderId,
encodedPledgeApplicationId === undefined ? "" : "&encodedPledgeApplicationId=" + encodedPledgeApplicationId,
providerId === undefined ? "" : "&providerId=" + providerId,
journeyData === undefined ? "" : "&journeydata=" + journeyData
);
res.redirect(redirectUrl);
return;
}
let backUrl = "";
if(cohortRef === undefined)
{
backUrl = "https://localhost:44376/" + employerId + "/unapproved/add/assign?ProviderId=" + providerId + "&AccountLegalEntityHashedId=" + legalEntityId;
}
else
{
backUrl = "https://localhost:44376/" + employerId + "/unapproved/" + cohortRef;
}
//non-levy payer must select a reservation
let viewmodel = {
cohortRef: cohortRef,
backUrl: backUrl,
reservations : [
{
reservationTitle: "Geospatial Survey Technician (244) June 2019",
reservationSubtitle: "",
accountLegalEntityId: employerId,
reservationDescription: "",
reservationUrl: String.Format("{0}/{1}/unapproved/{2}?reservationId={3}&accountLegalEntityHashedId={4}&courseCode={5}&startMonthYear={6}{7}{8}",
config.employerCommitmentsBaseUrl,
employerId,
cohortRef === undefined ? "add/apprentice" : cohortRef + "/apprentices/add",
uuidv1(),
legalEntityId,
"244",
"062019",
providerId === undefined ? "" : "&providerId=" + providerId,
journeyData === undefined ? "" : "&journeydata=" + journeyData
)
}
]
};
res.render('select-reservation', viewmodel);
});
/* serve up api requests */
app.get('/api/*',(req, res) => {
sendFile(res, req.url, req.method);
});
app.listen(port, () => {
console.log("Server listening on port " + port);
});
String.Format = function (b) {
var a = arguments;
return b.replace(/(\{\{\d\}\}|\{\d\})/g, function (b) {
if (b.substring(0, 2) == "{{") return b;
var c = parseInt(b.match(/\d/)[0]);
return a[c + 1]
})
};
sendFile = function(res, url, method) {
const filename = ("responses" + url.replace(/\/$/, '') + '_' + method + '.json').toLowerCase();
console.log("Processing request for " + path.join(__dirname, filename));
if(!fs.existsSync(filename))
{
res.status(404).send('No such file: ' + filename);
return;
}
res.header("Content-Type",'application/json');
res.sendFile(path.join(__dirname, filename));
};
sendFile = function(res, filename) {
const fullFileName = "responses/" + filename;
res.header("Content-Type",'application/json');
res.sendFile(path.join(__dirname, fullFileName));
};
express.request.getFromQueryString = function(parameterName) {
for (var key in this.query)
{
if(key.toLowerCase() === parameterName.toLowerCase())
{
return this.query[key];
}
}
};
express.request.getFromBody = function(propertyName) {
for (var key in this.body)
{
if(key.toLowerCase() === propertyName.toLowerCase())
{
return this.body[key];
}
}
};
// Source: http://stackoverflow.com/questions/497790
let dates = {
convert:function(d) {
// Converts the date in d to a date-object. The input can be:
// a date object: returned without modification
// an array : Interpreted as [year,month,day]. NOTE: month is 0-11.
// a number : Interpreted as number of milliseconds
// since 1 Jan 1970 (a timestamp)
// a string : Any format supported by the javascript engine, like
// "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
// an object : Interpreted as an object with year, month and date
// attributes. **NOTE** month is 0-11.
return (
d.constructor === Date ? d :
d.constructor === Array ? new Date(d[0],d[1],d[2]) :
d.constructor === Number ? new Date(d) :
d.constructor === String ? new Date(d) :
typeof d === "object" ? new Date(d.year,d.month,d.date) :
NaN
);
},
compare:function(a,b) {
// Compare two dates (could be of any type supported by the convert
// function above) and returns:
// -1 : if a < b
// 0 : if a = b
// 1 : if a > b
// NaN : if a or b is an illegal date
// NOTE: The code inside isFinite does an assignment (=).
return (
isFinite(a=this.convert(a).valueOf()) &&
isFinite(b=this.convert(b).valueOf()) ?
(a>b)-(a<b) :
NaN
);
},
inRange:function(d,start,end) {
// Checks if date in d is between dates in start and end.
// Returns a boolean or NaN:
// true : if d is between start and end (inclusive)
// false : if d is before start or after end
// NaN : if one or more of the dates is illegal.
// NOTE: The code inside isFinite does an assignment (=).
return (
isFinite(d=this.convert(d).valueOf()) &&
isFinite(start=this.convert(start).valueOf()) &&
isFinite(end=this.convert(end).valueOf()) ?
start <= d && d <= end :
NaN
);
}
}