-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
416 lines (312 loc) · 11.1 KB
/
server.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
//import statements
const crypto = require('crypto');
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const mailgen= require('mailgen');
const nodemailer = require('nodemailer');
var pn=1;
const app=express();
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine", "ejs");
app.use(express.static("public"));
/******************************************************************************************************************************************************************************/
mongoose.connect(
'mongodb+srv://arogya702:[email protected]/rogya_predictor',{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => console.log('DB Connection Successfull'))
.catch((err) => {
console.error(err);
});
/*******************************************************************************************************************************************************************************/
// define schema.....
const details_schema = {
cid:Number,
name_of_store: String,
address: String,
img:String,
contact_detail: String,
city:String,
pin:Number,
state:String
};
const userSchema = new mongoose.Schema({
uid:Number,
name: String,
email: String,
password: String,
});
//create tables.
const details = mongoose.model("details", details_schema);
const User = mongoose.model('User', userSchema);
/*########################################################################################################################################################################################################################### */
// render front page of the website
app.get("/",function(req,res){
res.render("front",{user:"Register"});
})
// render near by store page in website
app.get("/near_by_store",function(req,res){
async function saveItem() {
try {
const results = await details.find({});
const cnt = await details.countDocuments({});
res.render("near_by_store", { itemlist: results,pagenumber:pn,count:cnt});
} catch (error) {
console.log(error);
}
}
saveItem();
});
// render page where user can update data
app.get("/detail_page",async (req,res)=>{
const email=(req.query.name);
var nm,pass;
User.findOne({ email: email })
.select('name password') // Select only the name and password fields
.exec((err, user) => {
if (err) {
console.error('Error retrieving user:', err);
// Handle the error if needed
} else if (user) {
const { name, password } = user;
console.log('User found:');
const storedPassword = password;
var str2 = storedPassword.split("€");
let flag=1;
let i=-1;
str2.forEach(element => {
if(i!=-1&&password[i]!=String.fromCharCode(element))flag=0;
i++;
});
res.render("details",{name:name,password:"",email:email,msg:""});
} else {
console.log('User not found');
res.render("details",{name:"",password:"",email:"",msg:""});
}
});
});
// rendering login and signup page
app.get("/login_page",(req,res)=>{
res.render("login",{message:""});
})
// rendering doctor consulting page
app.get("/talk",(req,res)=>{
res.render("talk",{message:""});
})
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
app.post("/next",function(req,res){
pn++;
async function nextelement() {
try {
const results = await details.find({});
const cnt = await details.countDocuments({});
res.render("near_by_store", { itemlist: results,pagenumber:pn,count:cnt});
}
catch{
console.log("error");
}
}
nextelement();
})
app.post("/searchbycity",function(req,res){
pn=1;
const city=req.body.city;
async function nextelement() {
try {
const results = await details.find({city:city});
const cnt = await details.countDocuments({});
res.render("near_by_store", { itemlist: results,pagenumber:pn,count:cnt});
}
catch{
console.log("error");
}
}
nextelement();
})
/******************************************************start update************************************************************ */
app.post('/update',async(req,res)=>{
const name=req.body.name;
const email=req.body.email;
let password=req.body.password;
const user = await User.findOne({ email:email });
if (user) {
// Update the desired fields
user.name = name;
let clutter="";
let str = password.split("")
str.forEach(element => {
clutter += `€${(element.charCodeAt())} `
});
password=clutter;
// Save the updated document
user.password = password;
await user.save();
res.render("details",{name:name,password:password,email:email,msg:"User data updated successfully"});
console.log('User data updated successfully');
} else {
res.render("details",{name:name,password:password,email:email,msg:"User not found"});
console.log('User not found');
}
})
/*********************************************************end update************************************************************* */
app.post('/register', async (req, res) => {
//const { name, email, password } = req.body;
const name=req.body.name;
const email=req.body.email;
let password=req.body.password;
//encrypted
let clutter="";
let str = password.split("")
str.forEach(element => {
clutter += `€${(element.charCodeAt())} `
});
password=clutter;
// email sending
let config ={
service: 'gmail',
auth :{
user:'[email protected]',
pass:'zlentpmhusgtblww'
}
}
let transporter= nodemailer.createTransport(config);
let mailgenerator= new mailgen({
theme: "default",
product:{
name: "Arogya",
link:"https://mailgen.js/"
}
})
let response ={
body:{
intro:name,
table:{
data:{
description:{ f:"Welcome to rogys predictor! We're thrilled to have you on board and would like to extend our warmest greetings.",
s:"Congratulations on successfully logging into your account. You now have access to a world of exciting features and services designed to enhance your experience Should you have any questions, concerns, or need any assistance, our support team is always here to help.",
t:"Feel free to reach out to us . Thank you once again for joining Unikon. We look forward to serving you and ensuring a seamless and enjoyable experience. " }
}
},
outro:"stay healthy!!"
}
}
let mail=mailgenerator.generate(response);
let message={
from:"[email protected]",
to:email,
subject:"you login successfully",
html:mail
}
transporter.sendMail(message).then(()=>{
console.log("send succesfully !!");
}).catch(error=>{
console.log("error!!");
});
const item1 = new User({
email: email,
password: password,
name: name
});
async function saveItem() {
try {
await item1.save();
console.log("Item saved successfully!");
res.render("login",{message:"signin succesfully kindly login !!"});
} catch (error) {
console.log(error);
res.render("login",{message:"error try again!!"});
}
}
saveItem();
});
//sending patient detail to doctor
app.post('/datacollection', async (req, res) => {
const phone=req.body.phone;
const email=req.body.email;
const age=req.body.age;
const gender=req.body.gender;
const symptom=req.body.symptom;
// email sending
let config ={
service: 'gmail',
auth :{
user:'[email protected]',
pass:'zlentpmhusgtblww'
}
}
let transporter= nodemailer.createTransport(config);
let mailgenerator= new mailgen({
theme: "default",
product:{
name: "Arogya",
link:"https://mailgen.js/"
}
})
let response ={
body:{
intro:"Dear ,",
table:{
data:{
description:{ f:"I hope this message finds you well. I wanted to personally thank you for taking the time to share your symptoms, age, and gender with us. Your health and well-being are of paramount importance to us, and we are committed to providing you with the best possible care. ",
s:"Your symptoms have been duly noted, and I am pleased to inform you that a qualified medical professional will be reaching out to you shortly. Our team of experienced doctors will carefully review the information you've provided and will be in touch via the phone number you've given us.",
t:"Please rest assured that your privacy and confidentiality are our top priorities. The information you've shared will only be used for the purpose of facilitating your consultation with the doctor." }
}
},
outro:"stay healthy!!"
}
}
let mail=mailgenerator.generate(response);
let message={
from:"[email protected]",
to:email,
subject:"you login successfully",
html:mail
}
transporter.sendMail(message).then(()=>{
console.log("send succesfully !!");
}).catch(error=>{
console.log("error!!");
});
res.render("talk",{message:"This is to confirm that we have received your symptom details. Our medical team will review them shortly. Please keep an eye on your email for further communication."});
});
// Handle user sign-in
app.post("/login", function (req, res) {
const username = req.body.name1;
const password = req.body.pass1;
User
.findOne({ email: username })
.then(async (data) => {
if (data) {
const storedPassword = data.password;
var str2 = storedPassword.split("€");
let flag=1;
let i=-1;
str2.forEach(element => {
if(i!=-1&&password[i]!=String.fromCharCode(element))flag=0;
i++;
});
if (flag==1) {
res.cookie('user_name', username, {
httpOnly: true
});
res.render("front", { user:username });
} else {
res.render("front", { user: "Please enter a valid username or password!" });
}
} else {
res.render("front", { user: "Please enter a valid username or password!" });
}
})
.catch((err) => {
console.error(err);
});
});
///////////////////////////////////////////////////////////listen///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
app.listen(3000,function(){
console.log("server started at port 3000");
})
/*############################################################################# THE END ########################################################################################################################## */