-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
2182 lines (1835 loc) · 77.5 KB
/
app.js
File metadata and controls
2182 lines (1835 loc) · 77.5 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// for GET & POST methods over HTTPS
const express = require('express');
const app = express();
// for session variables
const session = require('express-session');
const crypto = require('crypto');
// with random secret
app.use(session({
secret: crypto.randomBytes(64).toString('hex'),
resave: false,
saveUninitialized: true
}));
// for Google sign in
const login = require('./login');
// for database
const util = require('util');
const mysql = require('mysql');
// for client code
app.use(express.static('client'));
// for POST methods
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
// helper functions
// configure the database
function makeDatabase() {
// connection details
const connection = mysql.createConnection({
host: 'database-1.cvjxib5qdvbb.eu-west-2.rds.amazonaws.com',
user: 'admin',
password: 'zaRhur-fawbi3-ryxkax',
port: '3306',
database: 'tcr_hub'
});
return {
query(sql, args) {
// execute query
return util.promisify(connection.query).call(connection, sql, args);
},
close() {
// disconnect database
return util.promisify(connection.end).call(connection);
}
};
}
// perform a database query
async function performQuery(query) {
// configure database
const db = makeDatabase();
try {
// execute query
return result = await db.query(query);
// if query fails
} catch (err) {
// return error
return '0database';
} finally {
// disconnect databse
await db.close();
}
}
// process result of query
function processQueryResult(result, response) {
// if database error
if (result == '0database') {
// send response
response.status(500).send('0database');
// return false
return false;
}
// else return true
return true;
}
// validate session
function validateSession(type, req, resp) {
// if session active & of correct type
if (req.session.active && req.session.type == type) {
// valid session
return true;
}
// activity error
resp.status(403).send('0inactive');
return false;
}
// validate session (type irrelevant)
function validateGeneralSession(req, resp) {
// if session active
if (req.session.active) {
// valid session
return true;
}
// activity error
resp.status(403).send('0inactive');
return false;
}
// add parameter to search clause
function addToSearchClause(parameter, field, whereClause) {
// if parameter defined
if (parameter) {
// if necessary
if (whereClause.length > 0) {
// add 'AND' to clause
whereClause += ' AND ';
}
// add parameter
whereClause += field + ' LIKE "%' + parameter + '%"';
}
// return search clause
return whereClause;
}
// add parameter to values statement for database update
function addToValuesStatement(parameter, field, valuesStatement) {
// if parameter defined
if (parameter) {
// if other parameters already in statement
if (valuesStatement.length > 0) {
// add comma
valuesStatement += ', ';
}
// add parameter to statement
valuesStatement += field + ' = ' + parameter;
}
// return statement
return valuesStatement;
}
// root
app.get('/', async function(req, resp) {
// redirect to customer sign in page
resp.writeHead(302, {
'Location': 'customersignin.html'
});
resp.end();
});
// rooms
// get rooms
app.get('/rooms', async function(req, resp) {
// if valid session
if (validateGeneralSession(req, resp)) {
// room types
const types = req.query.types;
// whether rooms must be bookable
const bookable = req.query.bookable;
// if types specified
if (types) {
// if types valid
if (['community', 'hostel', 'both'].includes(types)) {
// dictionary of rooms
let rooms = {};
// if community rooms requested
if (types != 'hostel') {
// get rooms
const community = await performQuery('SELECT * FROM communityRooms');
// providing no error occurred
if (processQueryResult(community, resp)) {
// add rooms to dictionary
rooms['community'] = community;
}
}
// if hostel rooms requested
if (types != 'community') {
// get rooms
const hostel = await performQuery('SELECT * FROM hostelRooms');
// providing no error occurred
if (processQueryResult(hostel, resp)) {
// add rooms to dictionary
rooms['hostel'] = hostel;
}
}
// return rooms
resp.status(200).send(JSON.stringify(rooms));
} else {
// types error
resp.status(400).send('0types');
}
} else {
// types error
resp.status(400).send('0types');
}
}
});
// get room availability
app.get('/roomavailability', async function(req, resp) {
// if valid session
if (validateGeneralSession(req, resp)) {
// room type
const type = req.query.type;
// room ID
const id = req.query.id;
// if ID specified
if (id) {
// check room type
switch (type) {
// if community room
case 'community':
// get room details
const communityRoom = await performQuery('SELECT days, start, end, maxLength FROM communityRooms WHERE id = ' + id);
// if no database error
if (processQueryResult(communityRoom, resp)) {
// if room exists
if (communityRoom.length == 1) {
// room details
response = communityRoom[0]
// room ID
response['id'] = id;
// room availability
const availability = await performQuery('SELECT start, end FROM communityBookings WHERE UNIX_TIMESTAMP(start) >= ' + ((new Date).getTime()) / 1000 + ' AND roomId = ' + id);
// if no database error
if (processQueryResult(availability, resp)) {
// times at which room is busy
response['busy'] = availability;
// send response
resp.status(200).send(JSON.stringify(response));
}
// room does not exist
} else {
// ID error
resp.status(400).send('0id');
}
}
break;
// if hostel room
case 'hostel':
// get room details
const hostelRoom = await performQuery('SELECT nights FROM hostelRooms WHERE id = ' + id);
// if no database error
if (processQueryResult(hostelRoom, resp)) {
// if room exists
if (hostelRoom.length == 1) {
// room details
response = hostelRoom[0]
// room ID
response['id'] = id;
// room availability
const availability = await performQuery('SELECT startDate, endDate FROM hostelBookings WHERE UNIX_TIMESTAMP(startDate) >= ' + ((new Date).getTime()) / 1000 + ' AND roomId = ' + id);
// if no database error
if (processQueryResult(availability, resp)) {
// times at which room is busy
response['busy'] = availability;
// send response
resp.status(200).send(JSON.stringify(response));
}
// room does not exist
} else {
// ID error
resp.status(400).send('0id');
}
}
break;
// if room type invalid
default:
//type error
resp.status(400).send('0type');
}
} else {
// ID error
resp.status(400).send('0id');
}
}
});
// get rooms large enough to house number of guests
app.get('/roomslargeenough', async function(req, resp) {
// if valid session
if (validateGeneralSession(req, resp)) {
// number of guests
const guestNum = req.query.guestnum;
// if number of guests specified
if (guestNum) {
// get matching hostel rooms
const hostelRooms = await performQuery('SELECT * FROM hostelRooms WHERE noOfPeople >= '+ guestNum);
// if no database error
if (processQueryResult(hostelRooms, resp)) {
// if matching rooms found
if (hostelRooms.length > 0) {
// send list of rooms
resp.status(200).send(JSON.stringify(hostelRooms));
} else {
// no matches
resp.status(200).send('0matches');
}
}
} else {
// parameter error
resp.status(400).send('0parameter');
}
}
});
// customers
// check customer in database
async function checkCustomerExists(customerID, resp) {
// get customer's details
const customer = await performQuery('SELECT id, fName, lName, email, phone FROM customers WHERE id = ' + customerID);
// if no database error
if (processQueryResult(customer, resp)) {
// if customer in database
if (customer.length == 1) {
return true;
}
return false;
}
}
// get all customers
app.get('/customers', async function(req,resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// fetch customers
const customers = await performQuery('SELECT id, fName, lName, email, phone FROM customers');
// if no database error
if (processQueryResult(customers, resp)) {
// if matching customers found
if (customers.length > 0) {
// send list of customers
resp.status(200).send(JSON.stringify(customers));
} else {
// no matches
resp.status(200).send('0matches');
}
}
}
});
// customer search
app.get('/customersearch', async function(req, resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// search parameters
const id = req.query.id;
const fname = req.query.fname;
const sname = req.query.sname;
const email = req.query.email;
const phone = req.query.phone;
// where clause
let where = '';
// build search clause
where = addToSearchClause(id, 'id', where);
where = addToSearchClause(fname, 'fName', where);
where = addToSearchClause(sname, 'lName', where);
where = addToSearchClause(email, 'email', where);
where = addToSearchClause(phone, 'phone', where);
// if no parameters specified
if (where.length == 0) {
// parameter error
resp.status(400).send('0parameters');
} else {
// get matching customers
const customers = await performQuery('SELECT id, fName, lName, email, phone FROM customers WHERE ' + where + ' ORDER BY lName, fName');
// if no database error
if (processQueryResult(customers, resp)) {
// if matching customers found
if (customers.length > 0) {
// send list of customers
resp.status(200).send(JSON.stringify(customers));
} else {
// no matches
resp.status(200).send('0matches');
}
}
}
}
});
// checks customer exists
app.get('/customerexists', async function(req, resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// customer ID
const customerID = req.query.id;
// if customer ID specified
if (customerID) {
// find customer
const customer = await performQuery('SELECT id, fName, lName, email, phone FROM customers WHERE id = ' + customerID);
// if no database error
if (processQueryResult(customer, resp)) {
// if customer exists
if (customer.length > 0) {
// send customer's details
resp.status(200).send(JSON.stringify(customer));
} else {
// no matches
resp.status(200).send('0matches');
}
}
} else {
// customer ID error
resp.status(400).send('0customerID');
}
}
});
// activities
// get all activities
app.get('/activities', async function(req, resp) {
// if valid session
if (validateGeneralSession(req, resp)) {
// fetch activities
const activities = await performQuery('SELECT * FROM activities');
// if no database error
if (processQueryResult(activities, resp)) {
// send activities
resp.status(200).send(JSON.stringify(activities));
}
}
});
// bookings
// get all bookings
app.get('/bookings', async function(req, resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// dictionary of bookings where key is booking type
let bookings = {};
// activity bookings
const activity = await performQuery('SELECT * FROM activityBookings');
// community room bookings
const community = await performQuery('SELECT * FROM communityBookings');
// hostel room bookings
const hostel = await performQuery('SELECT * FROM hostelBookings');
// if no database errors
if (processQueryResult(activity, resp) && processQueryResult(community, resp) && processQueryResult(hostel, resp)) {
// put bookings in dictionary
bookings['activity'] = activity;
bookings['community'] = community;
bookings['hostel'] = hostel;
// send bookings
resp.status(200).send(JSON.stringify(bookings));
}
}
});
// get customer bookings
async function bookings(customerID, resp) {
// dictionary of bookings where key is booking type
let bookings = {};
// activity bookings
const activity = await performQuery('SELECT b.id AS bookingID, b.dateTime, b.numberOfPeople, b.price, b.paid, a.id AS activityID, a.name, a.description FROM activityBookings b INNER JOIN activities a ON b.activityId = a.id WHERE b.userId = ' + customerID + ' ORDER BY b.dateTime');
// community room bookings
const community = await performQuery('SELECT b.id AS bookingID, b.start, b.end, b.priceOfBooking, b.paid, r.id AS roomID, r.name, r.description FROM communityBookings b INNER JOIN communityRooms r ON b.roomId = r.id WHERE b.userId = ' + customerID + ' ORDER BY b.start');
// hostel room bookings
const hostel = await performQuery('SELECT b.id AS bookingID, b.startDate, b.endDate, b.price, b.paid, r.id AS roomID, r.noOfPeople, r.pricePerNight FROM hostelBookings b INNER JOIN hostelRooms r ON b.roomId = r.id WHERE b.userId = ' + customerID + ' ORDER BY b.startDate');
// if no database errors
if (processQueryResult(activity, resp) && processQueryResult(community, resp) && processQueryResult(hostel, resp)) {
// if activity bookings exist
if (activity.length > 0) {
// add to dictionary
bookings['activity'] = activity;
}
// if community room bookings exist
if (community.length > 0) {
// add to dictionary
bookings['community'] = community;
}
// if hostel room bookings exist
if (hostel.length > 0) {
// add to dictionary
bookings['hostel'] = hostel;
}
// if customer has made no bookings
if (Object.entries(bookings).length === 0) {
// bookings error
return '0bookings';
}
// return bookings
return JSON.stringify(bookings);
}
}
// get customer booking requests
async function bookingRequests(customerID, resp) {
// dictionary of booking requests where key is booking type
let bookings = {};
// activity booking requests
const activity = await performQuery('SELECT b.id AS requestID, b.dateTime, b.numberOfPeople, b.price, a.id AS activityID, a.name, a.description FROM activityRequests b INNER JOIN activities a ON b.activityId = a.id WHERE b.userId = ' + customerID + ' ORDER BY b.dateTime');
// community room booking requests
const community = await performQuery('SELECT b.id AS requestID, b.start, b.end, b.priceOfBooking, r.id AS roomID, r.name, r.description FROM communityRequests b INNER JOIN communityRooms r ON b.roomId = r.id WHERE b.userId = ' + customerID + ' ORDER BY b.start');
// hostel room booking requests
const hostel = await performQuery('SELECT b.id AS requestID, b.startDate, b.endDate, b.price, r.id AS roomID, r.noOfPeople, r.pricePerNight FROM hostelRequests b INNER JOIN hostelRooms r ON b.roomId = r.id WHERE b.userId = ' + customerID + ' ORDER BY b.startDate');
// if no database errors
if (processQueryResult(activity, resp) && processQueryResult(community, resp) && processQueryResult(hostel, resp)) {
// if activity booking requests exist
if (activity.length > 0) {
// add to dictionary
bookings['activity'] = activity;
}
// if community room booking requests exist
if (community.length > 0) {
// add to dictionary
bookings['community'] = community;
}
// if hostel room booking requests exist
if (hostel.length > 0) {
// add to dictionary
bookings['hostel'] = hostel;
}
// if customer has made no booking requests
if (Object.entries(bookings).length === 0) {
// bookings error
return '0bookings';
}
// return booking requests
return JSON.stringify(bookings);
}
}
// get bookings for specified customer
app.get('/customerbookings', async function(req, resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// customer ID
const customerID = req.query.id;
// if ID specified
if (customerID) {
// check customer exists
if (await checkCustomerExists(customerID, resp)) {
// get dictionary of bookings
const bookingsToReturn = await bookings(customerID, resp);
// send bookings
resp.status(200).send(bookingsToReturn);
} else {
// ID error
resp.status(400).send('0customerID');
}
} else {
// ID error
resp.status(400).send('0customerID');
}
}
});
// get bookings using customer session
app.get('/mybookings', async function(req, resp) {
// if valid customer session
if (validateSession('customer', req, resp)) {
// get dictionary of bookings
const bookingsToReturn = await bookings(req.session.userID, resp);
// send bookings
resp.status(200).send(bookingsToReturn);
}
});
// get booking requests using customer session
app.get('/mybookingrequests', async function(req, resp) {
// if valid customer session
if (validateSession('customer', req, resp)) {
// get dictionary of booking requests
const bookingsToReturn = await bookingRequests(req.session.userID, resp);
// send booking requests
resp.status(200).send(bookingsToReturn);
}
});
// make activity booking on behalf of customer
app.post('/staffactivitybooking', async function(req, resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// customer ID
const customerID = req.body.customerid;
// if customer ID specified
if (customerID) {
// check customer exists
if (checkCustomerExists(customerID, resp)) {
// booking parameters
const activityID = req.body.activityid;
const dateTime = req.body.datetime;
const numberOfPeople = req.body.numberofpeople;
const price = req.body.price;
const paid = req.body.paid;
// if all parameters specified
if (activityID && dateTime && numberOfPeople && price && paid) {
// insert row
const result = await performQuery('INSERT INTO activityBookings (dateTime, activityId, userId, numberOfPeople, price, paid) VALUES (FROM_UNIXTIME(' + dateTime + '), ' + activityID + ', ' + customerID + ', ' + numberOfPeople + ', ' + price + ', ' + paid + ')');
// if no database error
if (processQueryResult(result, resp)) {
// if correct number of rows inserted
if (result['affectedRows'] == 1) {
// booking successful
resp.status(200).send('1success');
} else {
// database error
resp.status(500).send('0database');
}
}
} else {
// parameter error
resp.status(400).send('0parameters');
}
}
} else {
// customer ID error
resp.status(400).send('0customerID');
}
}
});
// make activity booking using customer session
app.post('/customeractivitybooking', async function(req, resp) {
// if valid customer session
if (validateSession('customer', req, resp)) {
// booking parameters
const activityID = req.body.activityid;
const dateTime = req.body.datetime;
const numberOfPeople = req.body.numberofpeople;
const price = req.body.price;
// if all parameters specified
if (activityID && dateTime && numberOfPeople && price) {
// insert row
const result = await performQuery('INSERT INTO activityRequests (dateTime, activityId, userId, numberOfPeople, price) VALUES (FROM_UNIXTIME(' + dateTime + '), ' + activityID + ', ' + req.session.userID + ', ' + numberOfPeople + ', ' + price + ')');
// if no database error
if (processQueryResult(result, resp)) {
// if correct number of rows inserted
if (result['affectedRows'] == 1) {
// booking successful
resp.status(200).send('1success');
} else {
// database error
resp.status(500).send('0database');
}
}
} else {
// parameter error
resp.status(400).send('0parameters');
}
}
});
// make community room booking on behalf of customer
app.post('/staffcommunitybooking', async function(req, resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// customer ID
const customerID = req.body.customerid;
// if customer ID specified
if (customerID) {
// check customer exists
if (checkCustomerExists(customerID, resp)) {
// booking parameters
const roomID = req.body.roomid;
const start = req.body.start;
const end = req.body.end;
const price = req.body.price;
const paid = req.body.paid;
// if all parameters specified
if (roomID && start && end && price && paid) {
// check for clashing bookings
const clashes = await performQuery('SELECT * FROM communityBookings WHERE start < FROM_UNIXTIME(' + end + ') AND end > FROM_UNIXTIME(' + start + ') AND roomId = ' + roomID);
// if no database error
if (processQueryResult(clashes, resp)) {
// if no clashes
if (clashes.length == 0) {
// insert row
const result = await performQuery('INSERT INTO communityBookings (start, end, priceOfBooking, paid, roomId, userId) VALUES (FROM_UNIXTIME(' + start + '), FROM_UNIXTIME(' + end + '), ' + price + ', ' + paid + ', ' + roomID + ', ' + customerID + ')');
// if no database error
if (processQueryResult(result, resp)) {
// if correct number of rows inserted
if (result['affectedRows'] == 1) {
// booking successful
resp.status(200).send('1success');
} else {
// database error
resp.status(500).send('0database');
}
}
} else {
// clashing bookings error
resp.status(400).send('0clashes');
}
}
} else {
// parameter error
resp.status(400).send('0parameters');
}
}
} else {
// customer ID error
resp.status(400).send('0customerID');
}
}
});
// make community room booking using customer session
app.post('/customercommunitybooking', async function(req, resp) {
// if active customer session
if (validateSession('customer', req, resp)) {
// booking parameters
const roomID = req.body.roomid;
const start = req.body.start;
const end = req.body.end;
const price = req.body.price;
// if all parameters specified
if (roomID && start && end && price) {
// check for clashing bookings
const clashes = await performQuery('(SELECT start, end FROM communityBookings WHERE start < FROM_UNIXTIME(' + end + ') AND end > FROM_UNIXTIME(' + start + ') AND roomId = ' + roomID + ') UNION (SELECT start, end FROM communityRequests WHERE start < FROM_UNIXTIME(' + end + ') AND end > FROM_UNIXTIME(' + start + ') AND roomId = ' + roomID + ')');
// if no database error
if (processQueryResult(clashes, resp)) {
// if no clashes
if (clashes.length == 0) {
// insert row
const result = await performQuery('INSERT INTO communityRequests (start, end, priceOfBooking, roomId, userId) VALUES (FROM_UNIXTIME(' + start + '), FROM_UNIXTIME(' + end + '), ' + price + ', ' + roomID + ', ' + req.session.userID + ')');
// if no database error
if (processQueryResult(result, resp)) {
// if correct number of rows inserted
if (result['affectedRows'] == 1) {
// booking successful
resp.status(200).send('1success');
} else {
// database error
resp.status(500).send('0database');
}
}
} else {
// clashing bookings error
resp.status(400).send('0clashes');
}
}
} else {
// parameter error
resp.status(400).send('0parameters');
}
}
});
// make hostel room booking on behalf of customer
app.post('/staffhostelbooking', async function(req, resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// customer ID
const customerID = req.body.customerid;
// if customer ID specified
if (customerID) {
// check customer exists
if (checkCustomerExists(customerID, resp)) {
// booking parameters
const roomID = req.body.roomid;
const start = req.body.start;
const end = req.body.end;
const price = req.body.price;
const paid = req.body.paid;
const numberOfPeople = req.body.numberofpeople;
// if all parameters specified
if (roomID && start && end && price && paid && numberOfPeople) {
// check for clashing bookings
const clashes = await performQuery('SELECT * FROM hostelBookings WHERE startDate < FROM_UNIXTIME(' + end + ') AND endDate > FROM_UNIXTIME(' + start + ') AND roomId = ' + roomID);
// if no database error
if (processQueryResult(clashes, resp)) {
// if no clashes
if (clashes.length == 0) {
// insert row
const result = await performQuery('INSERT INTO hostelBookings (roomId, startDate, endDate, userId, price, paid, noOfPeople) VALUES ('+ roomID + ', FROM_UNIXTIME(' + start + '), FROM_UNIXTIME(' + end + '),' + customerID + ', ' + price + ',' + paid + ', ' + numberOfPeople + ')');
// if no database error
if (processQueryResult(result, resp)) {
// if correct number of rows inserted
if (result['affectedRows'] == 1) {
// booking successful
resp.status(200).send('1success');
} else {
// database error
resp.status(500).send('0database');
}
}
} else {
// clashing bookings error
resp.status(400).send('0clashes');
}
}
} else {
// parameter error
resp.status(400).send('0parameters');
}
}
} else {
// customer ID error
resp.status(400).send('0customerID');
}
}
});
// make community room booking using customer session
app.post('/customerhostelbooking', async function(req, resp) {
// if valid staff session
if (validateSession('customer', req, resp)) {
// booking parameters
const roomID = req.body.roomid;
const start = req.body.start;
const end = req.body.end;
const price = req.body.price;
const numberOfPeople = req.body.numberofpeople;
// if all parameters specified
if (roomID && start && end && price && numberOfPeople) {
// check for clashing bookings
const clashes = await performQuery('(SELECT startDate, endDate FROM hostelBookings WHERE startDate < FROM_UNIXTIME(' + end + ') AND endDate > FROM_UNIXTIME(' + start + ') AND roomId = ' + roomID + ') UNION (SELECT startDate, endDate FROM hostelRequests WHERE startDate < FROM_UNIXTIME(' + end + ') AND endDate > FROM_UNIXTIME(' + start + ') AND roomId = ' + roomID + ')');
// if no database error
if (processQueryResult(clashes, resp)) {
// if no clashes
if (clashes.length == 0) {
// insert row
const result = await performQuery('INSERT INTO hostelRequests (roomId, startDate, endDate, userId, price, noOfPeople) VALUES ('+ roomID + ', FROM_UNIXTIME(' + start + '), FROM_UNIXTIME(' + end + '),' + req.session.userID + ', ' + price + ', ' + numberOfPeople + ')');
// if no database error
if (processQueryResult(result, resp)) {
// if correct number of rows inserted
if (result['affectedRows'] == 1) {
// booking successful
resp.status(200).send('1success');
} else {
// database error
resp.status(500).send('0database');
}
}
} else {
// clashing bookings error
resp.status(400).send('0clashes');
}
}
} else {
// parameter error
resp.status(400).send('0parameters');
}
}
});
// update activity booking (request)
app.post('/updateactivitybooking', async function(req, resp) {
// if valid staff session
if (validateSession('staff', req, resp)) {
// booking ID
const bookingID = req.body.bookingid;
// if booking ID specified
if (bookingID) {
// type
const type = req.body.type;
// if valid type
if (['booking', 'request'].includes(type)) {
// parameters
const numberOfPeople = req.body.numberofpeople;
const price = req.body.price;
// values statment for SQL
let values = '';
// add parameters to values statement
values = addToValuesStatement(numberOfPeople, 'numberOfPeople', values);
values = addToValuesStatement(price, 'price', values);
// if at least one parameter defined
if (values.length > 0) {
// database table names
const tableNames = {'booking': 'activityBookings', 'request': 'activityRequests'};
// update appropriate table
const result = await performQuery('UPDATE ' + tableNames[type] + ' SET ' + values + ' WHERE id = ' + bookingID);
// if no database error
if (processQueryResult(result, resp)) {
// if correct number of rows updated
if (result['affectedRows'] == 1) {
// success response
resp.status(200).send('1success');
// booking does not exist
} else {
// booking error
resp.status(400).send('0booking');
}
}
} else {
// parameters error
resp.status(400).send('0parameters');
}
} else {
// type error
resp.status(400).send('0type');