-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
1240 lines (1185 loc) · 34.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
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
const express = require('express');
/*
include all required modules
*/
const session = require('express-session');
const path = require('path');
const sanitize = require('mongo-sanitize');
const moment = require('moment');
/*
require database schemas
*/
require('./db');
const mongoose = require('mongoose');
//const Visitor = mongoose.model('Visitor');
const Patient = mongoose.model('Patient');
const Doctor = mongoose.model('Doctor');
const Appointment = mongoose.model('Appointment');
const Message = mongoose.model('Message');
const Chat = mongoose.model('Chat');
const Post = mongoose.model('Post');
const Comment = mongoose.model('Comment');
const MedicalProfile = mongoose.model('MedicalProfile');
const Id = mongoose.model('Id');
const app = express();
app.set('view engine', 'hbs');
const publicPath = path.join(__dirname);
app.use(express.static(publicPath));
app.use(express.urlencoded({extended: false}));
/*
* session setting
* session will be used to store the login state of users
*/
const sessionOptions = {
secret: 'secret for signing session id',
saveUninitialized: false,
resave: false
};
app.use(session(sessionOptions));
/*
*use the middleware to store the session information of users
*/
app.use(function(req, res, next){
/*
use 'local' one the session information
*/
res.locals.is_doctor = req.session.is_doctor;
res.locals.type = req.session.type;
res.locals.user = req.session.name;
res.locals._id = req.session._id;
res.locals.userslug = req.session.slug;
next();
});
// get the homepage
app.get('/', (req, res) => {
/*
* Randomly generate 10 posts from the postes with top 20 hits
* send 10 posts to front-end for display
*/
let twentyPosts;
let selectedPosts;
Post.find(function(err,posts){
posts.sort((a, b) => (a.hit < b.hit) ? 1:-1);
twentyPosts = posts.slice(0,20);
/*
* generate the posts with the help of the getRandom function
*/
selectedPosts = getRandom(twentyPosts, Math.min(10,twentyPosts.length))
.map(function(postObj) {
postObj.content = postObj.content.slice(0, 300);
return postObj;
});
if (req.session.name===undefined) {
res.render('HomePage',{NotLogin: true,posts:selectedPosts});
}
else {
res.render('HomePage',{posts:selectedPosts});
}
});
});
//load the login page
app.get("/login", function(req, res) {
res.render('login', {layout: false});
});
//load the register page
app.get("/signup", function(req, res) {
res.render('signup', {layout: false});
});
//check whether the login is valid with email and password
app.get("/loginCheck", function(req,res){
/*
check whether the user is login as a doctor
*/
if (req.query.usertype==="Doctor") {
/*
* check the if the email and password match any registered doctors
*/
Doctor.findOne({email:req.query.Email, password: req.query.Password}, function(error, data){
if (data) {
/*
* if login is valid, store the session info in the server
* session including the usertype, username, userid, and userslug
*/
req.session.is_doctor = true;
req.session.type = "Doctor";
req.session.name = data.name;
req.session._id = data.id;
req.session.slug = data.slug;
res.redirect("/");
} else {
const errormessage="Sorry! Incorrect username or password. Please try again.";
res.render("error", {"error": errormessage});
}
});
} else if (req.query.usertype==="Patient"){
/*
check whether the user is login as a patient
*/
Patient.findOne({email:req.query.Email, password: req.query.Password}, function(error, data){
/*
check the if the email and password match any registered doctors
*/
if (data) {
/*
* if login is valid, store the session info in the server
* session including the usertype, username, userid, and userslug
*/
req.session.is_doctor = false;
req.session.type = "Patient";
req.session.name = data.name;
req.session._id = data.id;
req.session.slug = data.slug;
res.redirect("/");
}
else {
const errormessage="Sorry! Incorrect username or password. Please try again.";
res.render("error", {"error": errormessage});
}
});
}
});
//get the register form for doctor
app.get('/registerDoctor', function(req, res){
res.render("registerDoctor",{layout: false});
});
//get the register form for patient
app.get('/registerPatient', function(req, res){
res.render("registerPatient",{layout: false});
});
//store the register information of new patient into database
app.post('/registerPatient', function(req, res){
/*
* generate id for a new patient
* check the previous ids to decide the next aviliable id
* the first registered user will get a default id
*/
let id = '1000000000';
Id.findOne({type:'init'},function(error, data){
if (data) {
id = data.id;
}
});
/*
* create a new patient
* store the personal information provided during the registration
* save the new patient into database
*/
new Patient({
name: sanitize(req.body.Name),
password: sanitize(req.body.Password),
gender: sanitize(req.body.Gender),
id: id.toString(),
date_of_birth: sanitize(req.body.DateOfBirth),
phone: sanitize(req.body.Phone),
address: sanitize(req.body.Address),
email: sanitize(req.body.Email),
}).save(function(error,newPatient){
if (error) {
console.log(error);
const errormessage = "Invalid register information!";
res.render('errorPage', {"error": errormessage});
} else {
patient = newPatient;
const NewId = id+1;
/*
* update the aviliable id
*/
Id.findOneAndUpdate({type:'init'},{id:NewId},function(error){
if (error) {
const errormessage = "Invalid register information!";
res.render('errorPage', {"error": errormessage});
console.log(error);
} else {
/*
* create an empty medical profile for the new patient
*/
new MedicalProfile({
patient_id:id
}).save(function(error,data){
if (data) {
res.redirect('/');
}
});
}
});
}
});
});
//store the register information of new doctor into database
app.post('/registerDoctor', function(req, res){
/*
* generate id for a new doctor
* check the previous ids to decide the next aviliable id
* the first registered user will get a default id
*/
let id = '1000000000';
Id.findOne({type:'init'},function(error, data){
if (data) {
id = data.id;
}
});
/*
* create a new doctor
* store the personal information provided during the registration
* save the new doctor into database
*/
new Doctor({
name: sanitize(req.body.Name),
password: sanitize(req.body.Password),
gender: sanitize(req.body.Gender),
id: id.toString(),
date_of_birth: sanitize(req.body.DateOfBirth),
phone: sanitize(req.body.Phone),
address: sanitize(req.body.Address),
email: sanitize(req.body.Email),
resume: sanitize(req.body.Resume),
department: sanitize(req.body.Department),
hospital: sanitize(req.body.Hospital),
position: sanitize(req.body.Position),
rating: 0
}).save(function(error){
if (error) {
console.log(error);
res.render('errorPage', {"error": "Invalid register information!"});
} else {
/*
* update the aviliable id
*/
const NewId = id+1;
Id.findOneAndUpdate({type:'init'},{id:NewId},function(error){
if (error) {
res.render('errorPage', {"error": "Server Error!"});
} else {
res.redirect('/');
}
});
}
});
});
//get the result of doctor search from database and sent to front-end
app.get('/search-result', (req, res) => {
/*
* search a doctor based on the input sequence
*/
Doctor.find(function(err, doctors) {
/*
* show all doctors if the input is empty
*/
if (req.query.option === "") {
/*
* doctors are sorted by their ratings.
*/
doctors.sort((a, b) => (a.rating < b.rating) ? 1:-1);
res.render('SearchResults', {doctors: doctors});
} else {
/*
* search doctors based on name/hospital/department/position
*/
const option = sanitize(req.query.option);
const filter = sanitize(req.query.filter);
const filteredDoctors = doctors.filter(function(doctorObj) {
return doctorObj[filter] === option;
});
filteredDoctors.sort((a, b) => (a.rating < b.rating) ? 1:-1);
res.render('SearchResults', {doctors: filteredDoctors});
}
});
});
//get the detailed doctor profile
app.get('/doctors/:slug', (req, res) => {
const slug = sanitize(req.params.slug);
/*
* search for doctor by slug and get the profile
* provide detailed doctor info after searching
*/
Doctor.findOne({slug: slug}, function(err, doctor) {
if (err || doctor === null) {
res.render('DoctorDetail', {error: true});
} else {
res.render('DoctorDetail', {doctor: doctor});
}
});
});
//enter the mian form page
app.get('/main-forum', (req, res) => {
/*
* search for all posts and send to the front-end
*/
Post.find(function(err, posts) {
if (req.query.option === "") {
/*
* list of posts is sorted by the number of their hits
*/
posts.sort((a, b) => (a.hit < b.hit) ? 1:-1);
posts.map(function(postObj) {
postObj.content = postObj.content.slice(0, 320);
return postObj;
});
res.render('forumPosts', {posts: posts});
} else {
/*
* filter the posts if searching with specific title/author
*/
const option = sanitize(req.query.option);
const filter = sanitize(req.query.filter);
const filteredPosts = posts.filter(function(postObj) {
return postObj[filter] === option;
}).map(function(postObj) {
postObj.content = postObj.content.slice(0, 300);
return postObj;
});
filteredPosts.sort((a, b) => (a.hit < b.hit) ? 1:-1);
res.render('forumPosts', {posts: filteredPosts});
}
});
});
//get the page of creating a new post
app.get('/posts-new', (req, res) => {
if (req.session.name !== undefined){
res.render('addPost');
} else {
res.redirect('/login');
}
});
//add the new post into database and go back to the main form
app.post('/posts-new', (req, res) => {
/*
* get the title, content, time, author of the new post
*/
const rawContent = JSON.parse(sanitize(req.body.content));
const title = sanitize(req.body.title);
const content = rawContent["ops"][0]["insert"].trim();
const myDate = new Date();
const time = myDate.getTime();
const stringTime = moment(time).format('YYYY-MM-DD HH:mm:ss');
/*
* create a new post with the corresponding data
*/
const newPost = new Post({
title: title,
content: content,
author_id: req.session._id,
create_time: stringTime,
comments: [],
name: req.session.name,
hit: 0
});
/*
* save the post into database
*/
newPost.save(function(err) {
if (err) {
/*
* reprot the error if title is not provided
*/
res.render('addPost', {error: "Please provide a title."});
} else {
res.redirect('/main-forum');
}
});
});
//get the detailed information and comments of the post
app.get('/posts/:slug',(req, res) => {
const slug = sanitize(req.params.slug);
const name = sanitize(req.query.option);
/*
* search for post based on its slug and set the info to front-end
*/
Post.findOneAndUpdate({slug: slug}, {$inc: {hit: 1}}, function(err, post) {
if (err) {
res.render('postContent', {error: "Server error"});
} else {
/*
* find the list of comments under this post
*/
Comment.find({_id: post.comments}, function(err, comments) {
if (err) {
res.render('postContent', {error: true});
} else if (req.query.option === "" || req.query.option === undefined) {
res.render('postContent', {post: post, comments: comments});
} else {
/*
* filter the comment if searching for comments from specific useer
*/
const filteredComments = comments.filter(function(commentObj) {
return commentObj.name === name;
});
res.render('postContent', {post: post, comments: filteredComments});
}
});
}
});
});
// get the page for making a new comment on the post
app.get('/posts/:slug/comments', (req, res) => {
const slug = sanitize(req.params.slug);
res.redirect('/posts/'+slug);
});
//make new comments under the post
app.post('/posts/:slug/comments', (req, res) => {
/*
* direct to the login page if the user hasn't logged in
*/
if (req.session.name === undefined) {
res.redirect('/login');
}
/*
* get the content,time and auther info of the comment from the front-end
*/
const slug = sanitize(req.params.slug);
const comment = sanitize(req.body.comment);
const name = req.session.name;
const authorId = req.session._id;
const myDate = new Date();
const time = myDate.getTime();
const stringTime = moment(time).format('YYYY-MM-DD HH:mm:ss');
/*
* create a new comment with these info
*/
const newComment = new Comment({
content: comment,
author_id: authorId,
create_time: stringTime,
name: name
});
/*
* save the new comment into database
*/
newComment.save(function(err, savedComment) {
if (err) {
console.log('error 1', err);
res.render('postContent', {commentError: true});
} else {
/*
* update the post in database by adding the new comment
*/
Post.findOneAndUpdate({slug: slug}, {$push: {comments: savedComment._id}}, function(err) {
if (err) {
console.log('error 2',err);
res.render('postConent', {commentError: true});
} else {
res.redirect('/posts/' + slug);
}
});
}
});
});
//get the page(calendar) for making an appointment with a doctor
app.get('/make-appointment/:slug', function(req, res){
/*
* direct to the login page if the user hasn't logged in
*/
if (req.session.name === undefined) {
res.redirect('/login');
}
const doctorSlug = req.params.slug;
/*
* find the doctor by the doctor slug
*/
Doctor.findOne({slug:doctorSlug}, function(error,doctor){
if (error){
const errormessage = "Server Error!";
res.render('errorPage', {"error": errormessage});
console.log(error);
}
else {
/*
* find all appointment of the doctor
*/
Appointment.find({doctor_id:doctor.id},function(error,appointment){
if (error){
const errormessage = "Server Error!";
res.render('errorPage', {"error": errormessage});
console.log(error);
} else {
/*
* send the list of appointments to the front-end
*/
res.render('makeAppointment',{doctor:doctor,appointment:JSON.stringify(appointment),slug:req.session.slug});
}
});
}
});
});
//store the new appointment into database
app.post('/update-appointment',function(req,res){
const event = JSON.parse(req.body.newEvent);
const doctorId = sanitize(req.body.doctor_id).toString();
/*
* create a new appointment event
* the detailed infomation of the appointment event comes from the front-end form and calendar API
*/
const newEvent = new Appointment({
title: event.title,
start: event.start,
end:event.end,
doctor_id: doctorId,
patient_id:req.session._id,
chief_complaint:event.chief_complaint,
});
/*
* save the new appointment event into database
*/
newEvent.save(function(err, appointment){
if (err){
const errormessage = "Server Error!";
res.render('errorPage', {"error": errormessage});
console.log(err);
} else {
Doctor.find({id: doctorId}, function(err, doctor) {
if (err) {
const errormessage = "Server Error!";
res.render('errorPage', {"error": errormessage});
console.log(err);
} else {
/*
* whenever making an appointment
* the patient and doctor can chat with each other
* so a new chat will be created if there isn't one
*/
Chat.findOne({doctor_id: doctorId, patient_id: req.session._id}, function(err, chat) {
/*
* check if there is a chat between the doctor and the patient
*/
if (err) {
const errormessage = "Server Error!";
res.render('errorPage', {"error": errormessage});
console.log(err);
} else if (chat == undefined) {
/*
* create a new chat
* save the chat into database
*/
const newChat = new Chat({
doctor_id: doctorId,
patient_id: req.session._id,
doctor_name: doctor[0].name,
patient_name: req.session.name,
messages: []
});
newChat.save(function(err, chat) {
if (err) {
console.log(err);
}
});
}
});
}
});
}
});
});
//view all the appointments, including history and upcoming
app.get('/appointment-history/:slug',function(req,res){
const upcoming = [];
const history = [];
let currentApp = {};
if (req.session.type === "Patient") {
/*
* search the appointments with the patient id
* distinguish the history appointments and upcoming appintments
*/
Appointment.find({patient_id: req.session._id}, function(err, appointments) {
if (err) {
res.render('appointmentHistory', {error: true});
} else {
for (let i=0; i<appointments.length; i++ ){
currentApp = appointments[i];
if (appointments[i].status === "Upcoming"){
/*
* use the helper function to get the doctor and patient info the the appointments
*/
getDoctorAndPatient(currentApp);
currentApp.slotEventOverlap=false;
upcoming.push(currentApp);
} else {
getDoctorAndPatient(currentApp);
currentApp.slotEventOverlap=false;
history.push(currentApp);
}
}
}
res.render('appointmentHistory', {upcoming:upcoming,history:history});
});
} else if (req.session.type==="Doctor") {
/*
* search the appointments with the doctor id
* distinguish the history appointments and upcoming appintments
*/
Appointment.find({doctor_id: req.session._id}, function(err, appointments) {
if (err) {
res.render('appointmentHistory', {error: true});
} else {
for (let i=0; i<appointments.length; i++ ){
currentApp = appointments[i];
if (appointments[i].status === "Upcoming"){
/*
* use the helper function to get the doctor and patient info the the appointments
*/
getDoctorAndPatient(currentApp);
currentApp.slotEventOverlap=false;
upcoming.push(currentApp);
//console.log(currentApp);
} else {
getDoctorAndPatient(currentApp);
currentApp.slotEventOverlap=false;
history.push(currentApp);
}
}
}
//console.log(upcoming);
res.render('appointmentHistory', {upcoming:upcoming,history:history});
});
}
});
//view the detail of an appointment
app.get('/appointments/:slug',function(req,res){
let currentAppointment = {};
const slug = req.params.slug;
let currDoctor;
Appointment.findOne({slug:slug},function(err,appointment){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
}else if (appointment){
currentAppointment = appointment;
Doctor.findOne({id:appointment.doctor_id},function(err,doctor){
currDoctor = doctor;
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
}else if (doctor){
MedicalProfile.findOne({patient_id:currentAppointment.patient_id}, function(err,data){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else if (data){
res.render("appointmentDetail",{appointment:currentAppointment,doctor:currDoctor,medicalProfile:data,slug:slug});
} else {
new MedicalProfile({
patient_id:currentAppointment.patient_id
}).save(function(err,data){
if(err) {
res.render('errorPage', {"error": errormessage});
}else if (data){
res.render("appointmentDetail",{appointment:currentAppointment,doctor:currDoctor,medicalProfile:data,slug:slug});
}
});
}
});
}
});
}
});
});
//view and edit the diagnosis corresponding to one appointment
app.get('/diagnosis/:slug',function(req,res){
let currentAppointment = {};
const slug = req.params.slug;
let currDoctor;
Appointment.findOne({slug:slug},function(err,appointment){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else if (appointment){
currentAppointment = appointment;
Doctor.findOne({id:appointment.doctor_id},function(err,doctor){
currDoctor = doctor;
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
}else if (doctor){
MedicalProfile.findOne({patient_id:currentAppointment.patient_id}, function(err,data){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else if (data){
res.render("diagnosisDetail",{appointment:currentAppointment,doctor:currDoctor,medicalProfile:data,slug:slug});
} else {
new MedicalProfile({
patient_id:currentAppointment.patient_id
}).save(function(err,data){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
}else{
res.render("diagnosisDetail",{appointment:currentAppointment,doctor:currDoctor,medicalProfile:data,slug:slug});
}
});
}
});
}
});
}
});
});
//get the page of rating an appoitment
app.get('/rate/:slug',function(req,res){
let currentAppointment = {};
const slug = req.params.slug;
let currDoctor;
/*
* find all the history appointments aviliable for rate
* get the appointment details
* send the info of the appointment to be rated to front-end
*/
Appointment.findOne({slug:slug},function(err,appointment){
if (appointment){
currentAppointment = appointment;
Doctor.findOne({id:appointment.doctor_id},function(err,doctor){
currDoctor = doctor;
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else{
MedicalProfile.findOne({patient_id:currentAppointment.patient_id}, function(err,data){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else if(data){
res.render("rateDetail",{appointment:currentAppointment,doctor:currDoctor,medicalProfile:data,slug:slug});
} else {
new MedicalProfile({
patient_id:currentAppointment.patient_id
}).save(function(err,data){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else{
res.render("rateDetail",{appointment:currentAppointment,doctor:currDoctor,medicalProfile:data,slug:slug});
}
});
}
});
}
});
}
});
});
//post and store the rate and comments of an appointment
app.post('/rate/:slug',function(req,res){
let doctor_id;
let doc_rate = 0;
let num = 0;
let avg_rate = 0;
const slug = req.params.slug;
const rate = sanitize(req.body.rate);
const rawComment = JSON.parse(sanitize(req.body.comment));
const comment = rawComment["ops"][0]["insert"].trim();
Appointment.findOneAndUpdate({slug:slug},{rate:rate,comment:comment},function(err,appointment){
if (appointment){
doctor_id = appointment.doctor_id;
Appointment.find({doctor_id:doctor_id}, function(err, doc_apps){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else{
for (let i=0; i<doc_apps.length; i++) {
if (doc_apps[i].rate && doc_apps[i].rate !== "0") {
doc_rate += parseInt(doc_apps[i].rate);
num += 1;
}
}
if (num !== 0) {
avg_rate = doc_rate/num;
}
Doctor.findOneAndUpdate({id:doctor_id},{rating: avg_rate.toFixed(1)}, function(err, doctor){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else {
res.redirect('/rate/'+slug);
}
});
}
});
}
});
});
//store the diagnosis
app.post('/diagnosis/:slug',function(req,res){
const slug = req.params.slug;
const rawDiagnosis = JSON.parse(sanitize(req.body.diagnosis));
const diagnosis = rawDiagnosis["ops"][0]["insert"].trim();
/*
* create a new medical profile
*/
const newMedical = {
height: sanitize(req.body.height),
weight: sanitize(req.body.weight),
right_eye_sight: sanitize(req.body.right_eye_sight),
left_eye_sight: sanitize(req.body.left_eye_sight),
blood_type: sanitize(req.body.blood_type),
medical_history: sanitize(req.body.medical_history),
allergy: sanitize(req.body.allergy),
blood_pressure_low: sanitize(req.body.blood_pressure_low),
blood_pressure_high: sanitize(req.body.blood_pressure_high),
};
/*
* update the appointment with new medical profile and diagnosis
*/
Appointment.findOneAndUpdate({slug:slug},{diagnosis:diagnosis,status:"History"},function(err,appointment){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else {
/*
* uodate the medical profile of the patient in the database
*/
MedicalProfile.findOneAndUpdate({patient_id:appointment.patient_id},newMedical,function(err){
res.redirect('/appointments/'+slug);
});
}
});
});
//get the profile of doctors and patients
app.get('/info-form/:slug',function(req,res){
if (req.session.type === "Doctor"){
/*
* search for doctor and get the profile
*/
Doctor.findOne({slug:req.session.slug},function(err,doctor){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else{
res.render("infoForm",{my:doctor});
}
});
} else if(req.session.type === "Patient"){
/*
* search for patient and get the profile
*/
Patient.findOne({slug:req.session.slug},function(err,patient){
if (patient){
MedicalProfile.findOne({patient_id:patient.id},function(err,medicalProfile){
if (err){
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
console.log(err);
} else {
res.render("infoForm",{my:patient, medicalProfile:medicalProfile});
}
});
}
});
}
});
//update the profile for patients and doctors after editing the profile
app.post('/update-profile/:slug',function(req,res){
let newProfile = {};
/*
* create a new profile with the updated personal information
* update the doctors'/patients' profile with the new profile
*/
if (req.session.type === "Doctor"){
newProfile = {
phone: sanitize(req.body.phone),
address: sanitize(req.body.address),
email: sanitize(req.body.email),
resume: sanitize(req.body.resume),
department: sanitize(req.body.department),
hospital: sanitize(req.body.hospital),
position: sanitize(req.body.position),
};
if (sanitize(req.body.gender)) {
newProfile[gender] = sanitize(req.body.gender);
}
Doctor.findOneAndUpdate({slug:req.session.slug},newProfile,function(err,doctor){
if (err) {
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else {
res.redirect('/info-form/:slug');
}
});
} else if(req.session.type === "Patient"){
/*
* create a new medical profile with the updated personal information
* update the patients' medical profile with the new one in addition the personal information profile
*/
const newMedical = {
height: sanitize(req.body.height),
weight: sanitize(req.body.weight),
right_eye_sight: sanitize(req.body.right_eye_sight),
left_eye_sight: sanitize(req.body.left_eye_sight),
medical_history: sanitize(req.body.medical_history),
allergy: sanitize(req.body.allergy),
blood_pressure_low: sanitize(req.body.blood_pressure_low),
blood_pressure_high: sanitize(req.body.blood_pressure_high),
};
if (sanitize(req.body.blood_type)){
newMedical.blood_type = sanitize(req.body.blood_type);
}
newProfile = {
phone: sanitize(req.body.phone),
address: sanitize(req.body.address),
email: sanitize(req.body.email),
};
if (sanitize(req.body.gender)) {
newProfile[gender] = sanitize(req.body.gender);
}
Patient.findOneAndUpdate({slug:req.session.slug},newProfile,function(err,patient){
if (patient){
MedicalProfile.findOneAndUpdate({patient_id:patient.id}, newMedical,function(err,medicalProfile){
if (err){
const errormessage = "errormessage";
res.render('errorPage', {"error": errormessage});
} else {
res.redirect('/info-form/:slug');
}
});
}
});
}
});
//get the chat page
app.get('/chat', (req, res) => {
if (req.session.name === undefined) {
res.redirect('/login');
}
/*
* doctor can create chat with patient who has upcoming appointments
* start with finding the upcoming appointments
* then find the patients corresponde with the appointments
* create new chat if no chat exists
*/
if (req.session.type === "Doctor") {
Appointment.find({doctor_id: req.session._id, status: ["Upcoming", "Ongoing"]}, function(err, appointments) {
if (err) {
console.log(err);
res.render('avaiChat', {error: true});
} else {
const appids = appointments.map(function(obj) {
return obj.patient_id;
});