-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
227 lines (210 loc) · 7.41 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
const express = require('express');
const app = express();
//database
const {db, Hospital, Hospital_Slots, User_Appts} = require('./db');
const session = require('express-session');
app.use(session({
secret: 'kdbfefwefascbasvafjdsvblsdjvlsfvsdjk',
resave: false,
saveUninitialized: true
}))
//middlewares for data exchange in proper format, in post requests
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.set('view engine', 'hbs');
app.use("/", express.static(__dirname + '/public'));
// requests
// admin login
app.post('/admin_login', (req,res)=>{
if(req.body.email && req.body.pass){
if(req.body.email=="[email protected]" && req.body.pass=="abhilasha"){
// res.send("ADMIN PAGE");
res.sendFile(__dirname + '/public/admin_page.html');
}
else{
return res.render('error', {error: "Invalid E-mail or Password! Please try logging in again with correct details."});
}
}
else{
res.render('error', {error: "Please enter all details to login!"});
}
})
// add hospital
app.post('/add_hospital', (req,res)=>{
if(req.body.id && req.body.name && req.body.city && req.body.locality && req.body.phone && req.body.contact_person){
Hospital.create({
id: req.body.id,
name: req.body.name,
city: req.body.city,
locality: req.body.locality,
contact_person: req.body.contact_person,
phone: req.body.phone
}).then((user)=>{
req.session.admin_logged_in = true;
res.sendFile(__dirname + '/public/admin_page.html');
}).catch((error)=>{
//throw error;
return res.render('error', {error, text: "Some error occured, while adding! Please try again."});
})
}
else{
return res.render('error', {error: "Please enter all details to add a hospital!"});
}
})
// view all hospitals on admin end
app.get('/show_hospitals', (req,res)=>{
Hospital.findAll()
.then((data)=>{
return res.send(data);
})
.catch((err)=>{
res.render('error', {err});
})
})
// view partiular hospital details
app.get('/hosp', (req,res)=>{
User_Appts.findAll({
where: {hospital_id: req.query.id}
})
.then((data)=>{
return res.render('hosp', {data});
}).catch((err)=>{
return res.render('error', {err});
})
})
// add slots for hosp if possible
app.post('/add_slots', (req,res)=>{
if(req.body.date && req.body.id){
Hospital_Slots.findOne({
where: {hospital_id: req.body.id, date: req.body.date}
})
.then((data)=>{
if(!data){
Hospital.findOne({where: {id: req.body.id}})
.then((hosp)=>{
Hospital_Slots.create({
hospital_id: hosp.id,
name: hosp.name,
city: hosp.city,
locality: hosp.locality,
date: req.body.date
}).then((s)=>{
//
}).catch((error)=>{
//throw error;
return res.render('error', {error, text: "Some error occured, while adding! Please try again."});
})
})
.catch((error)=>{
//throw error;
return res.render('error', {error, text: "Some error occured, while adding! Please try again."});
})
}
return res.send("Success!");
}).catch((err)=>{
return res.render('error', {err});
})
}
else{
res.render('error', {error: "Please enter all details to add slots!"});
}
})
// get slots
app.post('/getslots', (req,res)=>{
if(req.body.hosp_id && req.body.apt_date){
Hospital_Slots.findOne({
where: {hospital_id: req.body.hosp_id, date: req.body.apt_date}
})
.then((data)=>{
console.log(data);
if(!data){
return res.send("111111111111");
}
return res.send(data.slots);
})
.catch((err)=>{
return res.render('error', {err});
})
}
else{
return res.render('error', {error: "Please enter all details to find slots!"});
}
})
// add user (take appointment)
app.post('/add_user', (req,res)=>{
if(req.body.name && req.body.city && req.body.dob && req.body.email && req.body.phone && req.body.aadhaar && req.body.pass
&& req.body.hospital_name && req.body.date && req.body.slot && req.body.hospital_id && req.body.hospital_city){
User_Appts.create({
name: req.body.name,
city: req.body.city,
dob: req.body.dob,
phone: req.body.phone,
email: req.body.email,
aadhaar: req.body.aadhaar,
password: req.body.pass,
hospital_name: req.body.hospital_name,
hospital_city: req.body.hospital_city,
hospital_id: req.body.hospital_id,
appt_date: req.body.date,
appt_slot: req.body.slot
}).then((apt)=>{
//update slot info
Hospital_Slots.findOne({
where: {hospital_id: req.body.hospital_id, date: req.body.date}
})
.then((hosp_slot)=>{
slot_string = hosp_slot.slots;
new_string = slot_string.substring(0, req.body.slot-1) + '1' + slot_string.substring(req.body.slot);
Hospital_Slots.update({ slots: new_string }, {
where: {
hospital_id: req.body.hospital_id, date: req.body.date
}
});
return red.send("Success!");
})
.catch((error)=>{
return res.render('error', {error});
})
// return red.send("Success!");
// res.sendFile(__dirname + '/public/admin_page.html');
}).catch((error)=>{
//throw error;
return res.render('error', {error, text: "Some error occured! Please try again."});
})
}
else{
return res.render('error', {error: "Please enter all details to book vaccination appointment!"});
}
})
// user login
app.post('/user_login', (req,res)=>{
if(req.body.email && req.body.pass){
User_Appts.findOne({
where: {email: req.body.email}
}).then((user)=>{
if(!user){
return res.render('error', {error: "No appointment exists! Please try logging in again with correct email."});
}
else if(user.password==req.body.pass){
// sab kuch shi hai, then we should create a cookie with userID and redirect to profile
res.render('appt', {user});
}
else{
return res.render('error', {error: "Invalid E-mail or Password! Please try logging in again with correct details."});
}
}).catch((error)=>{
console.log(error);
return res.render('error', {error, text: "Some error occurred! Please try logging in again."});
})
}
else{
res.render('error', {error: "Please enter all details to login!"});
}
})
//syncing db
db.sync()
.then(()=>{
app.listen(4444, ()=>{
console.log("Server started successfully at http://localhost:4444 ");
})
})