-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.js
293 lines (287 loc) · 10.7 KB
/
new.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
var express = require("express");
var router = express();
var bodyParse = require("body-parser");
var mongoose = require("mongoose");
const { request, response } = require("express");
const nodemailer = require("nodemailer");
// Creating app
var exp = require("express");
const app = exp()
app.engine('html', require('ejs').renderFile)
app.use(exp.static("./public"))
app.use(bodyParse.json())
app.use(express.static('public'))
app.use(bodyParse.urlencoded({
extended: true
}))
// Connecting with mongodb
mongoose.connect('mongodb://0.0.0.0:27017/HMS-New', {
useNewUrlParser: true,
useUnifiedTopology: true
})
var db = mongoose.connection;
// Checking for connection
db.on('error', () => console.log("error in Creating database"));
db.once('open', () => console.log("Connected to database."));
// Creating checking page
app.get("/", (req, res) => {
return res.sendFile(__dirname + '/front.html');
});
app.post("/send", (req, res) => {
const { firstname, email, phone, date, message } = req.body;
// const { firstname, email, phone, date, messsage } = req.body;
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "rvhospital2023@gmail.com",
pass: "looqofkmlqupjdxg",
},
});
const mailOptions = {
from: "rvhospital2023@gmail.com",
to: email,
subject: "Appointment Booking Confirmation at RV Hospitals❤️🩹",
text: `Dear ${firstname},\n\nWe are writing to confirm that your appointment at RV Hospitals❤️🩹 has been successfully booked on ${date}.\n\nPlease arrive at least 15 minutes before your appointment time to allow for registration and any necessary paperwork.\n\nIf you need to cancel or reschedule your appointment, please contact us as soon as possible to make alternative arrangements.\n\nWe look forward to seeing you soon.\n\nBest regards,\n\nRV Hospitals`,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.send("error");
} else {
console.log("Email sent: " + info.response);
// res.send("Appointment Booked");
return res.sendFile(__dirname + '/booking.html');
}
});
const book = new Appoint({ firstname, email, phone, date, message });
Promise.all([book.save()]);
});
app.get("/viewProfile", (req, res) => {
return res.sendFile(__dirname + '/viewProfile.html');
});
app.get("/search", (req, res) => {
return res.sendFile(__dirname + '/search.html');
});
app.get("/viewDoctor", (req, res) => {
return res.sendFile(__dirname + '/viewDoctor.html');
});
app.get("/addDoctor", (req, res) => {
return res.sendFile(__dirname + '/addDoctor.html');
});
app.get("/deleteDoctor", (req, res) => {
return res.sendFile(__dirname + '/deleteDoctor.html');
});
app.get("/index", function (req, res) {
res.sendFile(__dirname + '/index.html')
});
app.get("/logout", function (req, res) {
res.sendFile(__dirname + '/login.html')
});
app.get("/editDoctor", (req, res) => {
return res.sendFile(__dirname + '/editDoctor.html');
});
app.get("/signup", function (req, res) {
res.sendFile(__dirname + '/signup.html')
});
app.get("/login", function (req, res) {
res.sendFile(__dirname + '/login.html')
});
const DETAILS_COLLECTION = 'details';
const VIEW_PROFILE_PATH = __dirname + '/viewProfile.html';
const INDEX_PATH = __dirname + '/index.html';
const VIEW_DOCTOR_PATH = __dirname + '/viewDoctor.html';
app.post("/login", async (request, response) => {
try {
const { firstname, password, designation } = request.body;
const user = await db.collection(DETAILS_COLLECTION).findOne({ firstname });
if (!user) {
return response.send("Information not match. Please create an account first.");
}
if (user.password !== password || user.designation !== designation) {
console.log("Password doesn't match.");
return response.send("Password not match.");
}
if (user.designation === "Admin") {
console.log("Login Successful.");
response.render(VIEW_PROFILE_PATH);
} else if (user.designation === "Patient") {
const { email, phone } = user;
response.render(INDEX_PATH, { firstname, email, phone });
} else if (user.designation === "Doctor") {
const { firstname, secondname, email, dob, education, gender, phone, address } = user;
response.render(VIEW_DOCTOR_PATH, { firstname, secondname, email, dob, education, gender, phone, address });
}
} catch (error) {
console.log("Invalid.");
response.send("Invalid.");
}
});
const patientSchema = new mongoose.Schema({
firstname: String,
secondname: String,
email: String,
dob: String,
phone: Number,
gender: String,
marital: String,
occupation: String,
aadhar: Number,
address: String,
blood: String,
height: Number,
weight: Number,
bmi: Number,
password: String,
confirmpassword: String,
designation: String
});
const detailSchema = new mongoose.Schema({
firstname: String,
secondname: String,
email: String,
dob: String,
gender: String,
phone: String,
address: String,
password: String,
confirmpassword: String,
designation: String
});
// Add Doctor Schema
const userSchema = new mongoose.Schema({
firstname: String,
secondname: String,
email: String,
dob: String,
education: String,
gender: String,
phone: String,
address: String,
password: String,
confirmpassword: String,
designation: String
});
const appSchema = new mongoose.Schema({
firstname:String,
email:String,
phone:String,
date:String,
message:String
});
const Appoint = mongoose.model('booking', appSchema);
const Detail = mongoose.model('details', detailSchema);
const patient = mongoose.model('patients', patientSchema);
const Doctor = mongoose.model('doctors', userSchema);
app.post("/addDoctor", async function (req, res) {
try {
const { firstname, secondname, email, dob, education, gender, phone, address, password, confirmpassword, designation } = req.body;
// Check if email already exists in the database
const existingDoctor = await Doctor.findOne({ email });
if (existingDoctor) {
return res.status(400).send("Email already exists in the database.");
}
const doctor = new Doctor({ firstname, secondname, email, dob, education, gender, phone, address, password, confirmpassword, designation });
const detail = new Detail({ firstname, secondname, email, dob, gender, phone, address, password, confirmpassword, designation });
await Promise.all([detail.save(), doctor.save()]);
return res.status(200).send("Doctor Added Successfully.");
} catch (error) {
console.error(error);
res.status(500).send('Error connecting to database');
}
});
app.post("/signup", async (req, res) => {
try {
const {firstname,secondname,email,dob,phone,gender,marital,occupation,aadhar,address,blood,height,weight,bmi,password,confirmpassword,designation,} = req.body;
if (password !== confirmpassword) {
return res.status(400).send('Password and confirm password do not match');
}
// Check if email already exists in the database
const existingUser = await patient.findOne({ email });
if (existingUser) {
return res.status(400).send('Email already exists');
}
const newUser = new patient({firstname,secondname,email,dob,phone,gender,marital,occupation,aadhar,address,blood,height,weight,bmi,password,confirmpassword,designation,});
const newDetail = new Detail({firstname,secondname,email,dob,gender,phone,address,password,confirmpassword,designation});
await Promise.all([newUser.save(), newDetail.save()]);
console.log("Login Success");
return res.sendFile(__dirname + '/login.html');
} catch (error) {
console.error(error);
return res.status(500).send('Error connecting to database');
}
});
app.post("/deleteDoctor", (req, res) => {
const query = { email: req.body.email };
db.collection('details').deleteOne(query, function (err, result) {
if (err) {
throw err;
res.send(result);
}
else {
res.render(__dirname + '/deleteDoctor.html');
console.log("Deleted Successfully.");
}
})
db.collection('doctors').deleteOne(query, function (err, result) {
if (err) {
throw err;
res.send(result);
}
else {
// res.render(__dirname + '/deleteDoctor.html');
console.log("Deleted Successfully.");
}
})
})
app.post("/search", (req, response) => {
const mail = req.body.email;
const usermail = db.collection('doctors').findOne({ email: mail }, (err, res) => {
if (res === null) {
return response.send("Details Not Found");
}
else if (err) throw err;
if (res.designation === "Doctor") {
const firstname = res.firstname;
const secondname = res.secondname;
const email = res.email;
const dob = res.dob;
const education = res.education;
const gender = res.gender;
const phone = res.phone;
const address = res.address;
response.render(__dirname + '/editDoctor.html', { firstname, secondname, email, dob, education, gender, phone, address });
console.log("Search Success");
}
})
})
app.post("/editDoctor", async function (req, res) {
try {
const { firstname, secondname, email, dob, education, gender, phone, address } = req.body;
const useer2 = await Doctor.findOne({ email: req.body.email })
const useer1 = await Detail.findOne({ email: req.body.email })
useer2.firstname = firstname;
useer2.secondname = secondname;
useer2.email = email;
useer2.dob = dob;
useer2.education = education;
useer2.gender = gender;
useer2.phone = phone;
useer2.address = address;
await useer2.save();
useer1.firstname = firstname;
useer1.secondname = secondname;
useer1.email = email;
useer1.dob = dob;
useer1.education = education;
useer1.gender = gender;
useer1.phone = phone;
useer1.address = address;
await useer1.save();
res.sendFile(__dirname + '/search.html')
}
catch (error) {
console.error(error);
res.status(500).send('Error connecting to database');
}
})
app.listen(3002, () => { console.log("Server Listening") });