-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
783 lines (727 loc) · 21.7 KB
/
app.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
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
const express = require("express");
const mysql = require("mysql");
const app = express();
const ejs = require("ejs");
const session = require("express-session");
const db = require("./config/db.js");
const PORT = process.env.PORT;
const http = require("http");
const bodyParser = require("body-parser");
const checkIfLogged = require("./routes/middleware/checkIfLogged.js");
const checkIfAdmin = require("./routes/middleware/checkIfAdmin.js");
const checkIfdataEntry = require("./routes/middleware/checkIfdataEntry");
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(express.json({ extended: false }));
const server = http.createServer(app);
app.use(
session({
secret: "2C44-4D44-WppQ38S",
resave: true,
saveUninitialized: true,
})
);
//render home.ejs with passing a variable
app.get("/", async (req, res) => {
flag = 0;
if (typeof req.session.success === "undefined" || req.session.success == 0)
flag = 0;
else flag = 1;
req.session.success = 0;
res.render("index", { logged: req.session.admin, flag: flag });
});
// get data-entry
app.get("/data-entry", [checkIfLogged, checkIfdataEntry], async (req, res) => {
res.render("forms/index", {
logged: req.session.admin,
link_freez: (typeof req.session.link_freez=="undefined")?0:req.session.link_freez
});
console.log("inside data-entry4543");
console.log((typeof req.session.link_freez=="undefined")?0:req.session.link_freez);
});
// registration step 1
app.get(
"/registeration-step1.html",
[checkIfLogged, checkIfdataEntry],
async (req, res) => {
var user = req.session.user_exist;
var p = 0;
if (typeof user === "undefined") p = 1;
if(p) {
res.render("forms/registeration-step1", {
logged: req.session.admin,
full_name: "",
email: "",
phone_number: req.session.phone_number,
blood_group: "",
gender: "",
dob: "",
link_freez: 1,
});
delete req.session.phone_number;
}
else {
res.render("forms/registeration-step1", {
logged: req.session.admin,
full_name: user[0].full_name,
email: user[0].email,
phone_number: user[0].phone_number,
blood_group: user[0].blood_group,
gender: user[0].gender,
dob: user[0].DOB,
link_freez: req.session.link_freez,
});
}
}
);
// registration step 2
app.get(
"/pretest-step2.html",
[checkIfLogged, checkIfdataEntry],
async (req, res) => {
var p = " ";
var user = req.session.user_exist;
if (typeof user === "undefined") p = " ";
else p = user[0].blood_group;
res.render("forms/pretest-step2", {
logged: req.session.admin,
full_name: "",
email: "",
phone_number: "",
blood_group: p,
gender: " ",
dob: " ",
haemoglobin: req.session.haemoglobin,
BP: req.session.BP,
temp: req.session.temp,
pulse: req.session.pulse,
link_freez: req.session.link_freez,
});
}
);
// donation step 3
app.get(
"/donation-step3.html",
[checkIfLogged, checkIfdataEntry],
async (req, res) => {
res.render("forms/donation-step3", {
logged: req.session.admin,
full_name: req.session.full_name,
blood_group: req.session.blood_group,
PID: req.session.pid,
link_freez: req.session.link_freez,
});
}
);
// get
app.get("/index.html", async (req, res) => {
flag = 0;
if (typeof req.session.success === "undefined" || req.session.success == 0)
flag = 0;
else flag = 1;
req.session.success = 0;
res.render("index", { logged: req.session.admin, flag: flag });
});
app.get("/about.html", async (req, res) => {
res.render("about", { logged: req.session.admin });
});
app.get("/forms/profile.html", checkIfLogged, async (req, res) => {
console.log(req.session);
await db.query(
"SELECT * FROM request WHERE PID = ?",
req.session.user,
async (error, result, fields) => {
if (error) {
console.log(error);
res.redirect("/");
} else {
var requests = result;
await db.query(
"SELECT * FROM donation_record WHERE PID = ?",
req.session.user,
async (error, result, fields) => {
if (error) {
console.log(error);
res.redirect("/");
} else {
var donation = result;
res.render("forms/profile", {
logged: req.session.admin,
info: req.session,
requests: requests,
donations: donation,
});
}
}
);
}
}
);
});
app.get("/blog.html", async (req, res) => {
res.render("blog", { logged: req.session.admin });
});
app.get("/blog_details.html", async (req, res) => {
res.render("blog_details", { logged: req.session.admin });
});
app.get("/donate_now.html", checkIfLogged, async (req, res) => {
res.render("donate_now", {
logged: req.session.admin,
info: req.session,
});
});
app.get("/services.html", checkIfLogged, async (req, res) => {
res.render("services", { logged: req.session.admin });
});
app.get("/contact.html", async (req, res) => {
res.render("contact", { logged: req.session.admin });
});
app.get("/request_now.html", checkIfLogged, async (req, res) => {
res.render("request_now", { logged: req.session.admin });
});
// long code admin/index
app.get(
"/admin/index_admin.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
"SELECT accepted , COUNT(REID) AS reqs FROM request GROUP BY accepted",
async (error, result, fields) => {
if (error) {
console.log(error);
} else {
req.session.reqcount = result;
await db.query(
"SELECT user_type , COUNT(PID) AS reqs FROM people GROUP BY user_type",
async (errors, results, field) => {
if (errors) {
console.log(error);
} else {
req.session.peoplecount = results;
var datetime = new Date();
var date = datetime.toISOString().slice(0, 10);
await db.query(
"SELECT COUNT(BDCID) AS reqs FROM blood_donation_camp WHERE camp_end < ?",
datetime,
async (error, result, field) => {
if (error) {
console.log(error);
} else {
req.session.ended = result;
await db.query(
"SELECT COUNT(BDCID) AS reqs FROM blood_donation_camp WHERE camp_start > ?",
datetime,
async (error, result, fields) => {
if (error) {
console.log(error);
} else {
req.session.Upcoming = result;
await db.query(
"SELECT COUNT(BDCID) AS reqs FROM blood_donation_camp WHERE camp_start < ? AND camp_end > ?",
[datetime, datetime],
async (error, results, fields) => {
if (error) {
console.log(error);
} else {
req.session.ongoing = results;
console.log(req.session.peoplecount);
res.render("admin/index_admin", {
logged: req.session.admin,
reqcount: req.session.reqcount,
peoplecount: req.session.peoplecount,
ended: req.session.ended,
Upcoming: req.session.Upcoming,
ongoing: req.session.ongoing,
});
}
}
);
}
}
);
}
}
);
}
}
);
}
}
);
}
);
// admin/admin-people.html
app.get(
"/admin/admin-people.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query("SELECT * FROM people", function (error, result, fields) {
if (error) {
console.log(error);
} else {
var peoples = result;
res.render("admin/admin-people", {
logged: req.session.admin,
peoples: result,
});
}
});
}
);
// admin - request
app.get(
"/admin/admin-request.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
`SELECT REID, receiver_name, request.blood_group, quantity, request_date, accepted
FROM people, request WHERE people.PID = request.PID`,
function (error, result, fields) {
if (error) {
console.log(error);
} else {
var requests = result;
res.render("admin/admin-request", {
logged: req.session.admin,
requests: result,
status: "pending",
});
}
}
);
}
);
//person-requests
app.get(
"/person-requests/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
`SELECT REID, receiver_name, request.blood_group, quantity, request_date, accepted
FROM people
INNER JOIN request ON people.PID = request.PID
WHERE people.PID = ?;`,
req.params["id"],
function (error, result, fields) {
if (error) {
console.log(error);
} else {
var requests = result;
res.render("admin/admin-request", {
logged: req.session.admin,
requests: result,
status: "pending",
});
}
}
);
}
);
// app.get("/admin/full-camps/filter", async (req, res) => {
// res.redirect("/admin/admin-camps.html");
// });
// app.post("/admin/full-camps/filter", async (req, res) => {
// var query;
// var datetime = new Date();
// var date = datetime.toISOString().slice(0, 10);
// if (req.body.filter == "Upcoming") {
// query = "SELECT * FROM blood_donation_camp WHERE camp_start > ?";
// } else if (req.body.filter == "ongoing") {
// query =
// "SELECT * FROM blood_donation_camp WHERE camp_start > ? AND camp_end < ?";
// } else {
// query = "SELECT * FROM blood_donation_camp WHERE camp_end < ?";
// }
// if ((req.body.order = "asc")) {
// query = query + " ORDER BY camp_start ASC";
// } else {
// query = query + " ORDER BY camp_start DESC";
// }
// await db.query(query, [date, date], async (error, result, fields) => {
// if (error) {
// console.log(error);
// } else {
// res.render("admin/admin-camps", {
// logged: req.session.admin,
// camps: result,
// });
// }
// });
// });
app.get(
"/admin/admin-camps.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
"SELECT * FROM blood_donation_camp",
function (error, result, fields) {
if (error) {
console.log(error);
} else {
res.render("admin/admin-camps", {
logged: req.session.admin,
camps: result,
});
}
}
);
}
);
app.get(
"/admin/full-camps.html/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
"SELECT * FROM blood_donation_camp WHERE BDCID = ?",
req.params.id,
function (error, result, fields) {
if (error) {
console.log(error);
} else {
res.render("admin/full-camps", { request: result });
}
}
);
}
);
app.get(
"/admin/add-camp.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
res.render("admin/add-camp", { logged: req.session.admin });
}
);
app.get(
"/admin/full-people.html/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
var pid = req.params["id"];
await db.query(
`SELECT people.*, height, weight, next_donation_date, previous_sms_date,
CASE
WHEN donor.PID IS NULL THEN false
ELSE true
END AS isdonor
FROM people
LEFT JOIN donor ON people.PID=donor.PID
WHERE people.PID=?`,
pid,
function (error, result, fields) {
if (error) {
console.log(error);
} else {
var peoples = result;
res.render("admin/full-people", {
logged: req.session.admin,
peoples: result,
});
}
}
);
}
);
app.get(
"/admin/add-people.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
res.render("admin/add-people", { logged: req.session.admin });
}
);
// app.get("/admin/showrequest", async (req, res) => {
// res.redirect("/showrequest/");
// });
app.get("/showrequest/:id", [checkIfLogged, checkIfAdmin], async (req, res) => {
{
await db.query(
"SELECT * FROM request WHERE REID = ?",
req.params.id,
function (error, result, fields) {
if (error) {
console.log(error);
} else {
console.log(result);
res.render("admin/full-request", { request: result });
}
}
);
}
});
app.get(
"/admin/received-record.html/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
"SELECT * FROM received_record WHERE REID=?",
req.params["id"],
async (error, result, fields) => {
if (error) {
console.log(error);
res.redirect("/admin/index_admin.html");
} else {
await db.query(
`SELECT BBID, blood_group FROM blood_bag WHERE status="available";`,
async (error, valid_BBID, fields) => {
if (error) {
console.log(error);
res.redirect("/admin/index_admin.html");
} else {
console.log(valid_BBID);
res.render("admin/received-record", {
logged: req.session.admin,
record: result,
valid_BBID: valid_BBID,
REID:req.params["id"],
});
}
}
);
}
}
);
}
);
app.get(
"/showdonation/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
{
await db.query(
"SELECT * FROM donation_record, people WHERE donation_record.DID = ? AND donation_record.PID = people.PID",
req.params["id"],
function (error, result, fields) {
if (error) {
console.log(error);
} else if(result.length==0) {
res.redirect("/admin/admin-donation.html");
}
else{
console.log(result);
res.render("admin/full-donation", {
logged:req.session.admin,
donation: result,
DID:req.params["id"] ,});
}
}
);
}
}
);
app.get(
"/admin/admin-donation.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
`SELECT DID, don_rec.PID, full_name, don_rec.BDCID, don_rec.BLID, donation_date, blood_group,
CASE
WHEN don_rec.BDCID IS NULL THEN branch_name
ELSE camp_name
END AS branch_camp_name
FROM (SELECT DID, PID, BDCID, BLID, donation_date FROM donation_record LIMIT ?, 50) AS don_rec
INNER JOIN people ON don_rec.PID=people.PID
LEFT JOIN blood_donation_camp ON don_rec.BDCID=blood_donation_camp.BDCID
LEFT JOIN blood_bank ON don_rec.BLID=blood_bank.BLID`,
(0*50),
function (error, result, fields) {
if (error) {
console.log(error);
} else {
res.render("admin/admin-donation", {
logged: req.session.admin,
donations: result,
});
}
}
);
}
);
//"/admin-donation.html/:id" this is for donations related to a camp
app.get(
"/admin-donation.html/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
`SELECT DID, donation_record.PID, donation_record.BDCID, full_name, camp_name AS branch_camp_name, blood_group, donation_date FROM donation_record
INNER JOIN people ON donation_record.PID=people.PID
INNER JOIN blood_donation_camp ON donation_record.BDCID=blood_donation_camp.BDCID
WHERE donation_record.BDCID=? ;`,
req.params["id"],
function (error, result, fields) {
if (error) {
console.log(error);
} else {
res.render("admin/admin-donation", {
logged: req.session.admin,
donations: result,
});
}
}
);
}
);
//"/person-donations/:id" this is for donations done by a particular person
app.get(
"/person-donations/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
`SELECT DID, don_rec.PID, full_name, don_rec.BDCID, don_rec.BLID, donation_date, blood_group,
CASE
WHEN don_rec.BDCID IS NULL THEN branch_name
ELSE camp_name
END AS branch_camp_name
FROM (SELECT DID, PID, BDCID, BLID, donation_date FROM donation_record WHERE PID=?) AS don_rec
INNER JOIN people ON don_rec.PID=people.PID
LEFT JOIN blood_donation_camp ON don_rec.BDCID=blood_donation_camp.BDCID
LEFT JOIN blood_bank ON don_rec.BLID=blood_bank.BLID`,
req.params["id"],
function (error, result, fields) {
if (error) {
console.log(error);
} else {
res.render("admin/admin-donation", {
logged: req.session.admin,
donations: result,
});
}
}
);
}
);
app.get(
"/admin/admin-bloodbank.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
"SELECT * FROM blood_bank",
async (error, result, fields) => {
if (error) {
console.log(error);
res.redirect("/");
} else {
res.render("admin/admin-bloodbank", {
logged: req.session.admin,
banks: result,
});
}
}
);
}
);
app.get(
"/admin/add-bloodbank.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
res.render("admin/add-bloodbank", { logged: req.session.admin });
}
);
app.get(
"/admin/admin-bloodbag.html",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
res.render("admin/admin-bloodbag", { logged: req.session.admin });
}
);
app.get(
"/admin/full-bloodbag.html/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
`SELECT blood_bag.*, donation_date, full_name, people.PID
FROM blood_bag
INNER JOIN donation_record ON donation_record.BBID=blood_bag.BBID
INNER JOIN people ON people.PID=donation_record.PID
WHERE blood_bag.BBID=?;`,
req.params["id"],
async (error, result, fields) => {
if (error) {
console.log(error);
res.redirect("/");
} else {
console.log(result);
if(JSON.parse(JSON.stringify(result[0])).status == "donated"){
await db.query(
`SELECT received_date, receiver_name, PID FROM received_record
INNER JOIN request ON request.REID=received_record.REID
WHERE BBID=?;`,
req.params["id"],
async (error, rec_result, fields) => {
if (error) {
console.log(error);
res.redirect("/");
} else {
console.log(rec_result);
res.render("admin/full-bloodbag", {
logged: req.session.admin,
blood_bag: result,
receiver : rec_result,
});
}
}
);
} else {
res.render("admin/full-bloodbag", {
logged: req.session.admin,
blood_bag: result,
});
}
}
}
);
}
);
app.get(
"/admin/admin-bloodbag.html/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
"SELECT * FROM blood_bag,donation_record WHERE blood_bag.BLID=? AND blood_bag.BBID=donation_record.BBID",
req.params["id"],
async (error, result, fields) => {
if (error) {
console.log(error);
res.redirect("/");
} else {
res.render("admin/admin-bloodbag", {
logged: req.session.admin,
blood_bag: result,
});
}
}
);
}
);
app.get(
"/admin/camp-bloodbag/:id",
[checkIfLogged, checkIfAdmin],
async (req, res) => {
await db.query(
`SELECT blood_bag.*, donation_date FROM blood_bag
INNER JOIN donation_record ON blood_bag.BBID=donation_record.BBID
WHERE donation_record.BDCID=? ;`,
req.params["id"],
async (error, result, fields) => {
if (error) {
console.log(error);
res.redirect("/");
} else {
res.render("admin/admin-bloodbag", {
logged: req.session.admin,
blood_bag: result,
});
}
}
);
}
);
/* all links redirected from filter */
/* end --------------------- */
app.use("/user", require("./routes/user"));
app.use("/request", require("./routes/request"));
app.use("/donate", require("./routes/donate"));
app.use("/admin", require("./routes/admin"));
app.use("/edit", require("./routes/edit"));
server.listen(3000 || PORT, function (req, res) {
console.log("Running on Server");
});
//git push -u -f origin master