-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
318 lines (269 loc) · 9.47 KB
/
app.js
File metadata and controls
318 lines (269 loc) · 9.47 KB
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
const { GoogleGenerativeAI } = require("@google/generative-ai");
require('dotenv').config();
const googleAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = googleAI.getGenerativeModel({
model: "gemini-2.0-flash",
});
const express=require('express')
const app=express()
const path=require('path')
const bcrypt=require('bcrypt')
const jwt=require('jsonwebtoken')
const cookieParser=require('cookie-parser')
app.use(cookieParser())
const db=require("./config/mongoose-connection")
const userModel=require("./models/user-model.js")
const memberModel=require("./models/member-model.js")
const hospitalModel=require("./models/hospital-model.js")
const vacModel=require("./models/vaccine_collection-model.js")
const multer = require("multer");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads/"); // ensure the folder exists
},
filename: function (req, file, cb) {
const uniqueName = Date.now() + "-" + file.originalname;
cb(null, uniqueName);
}
});
const fileFilter = function (req, file, cb) {
if (file.mimetype === "application/pdf") cb(null, true);
else cb(new Error("Only PDF files are allowed"), false);
};
const upload = multer({ storage, fileFilter });
app.use(express.json())
app.use(express.urlencoded({extended:true}))
app.set("view engine","ejs")
app.use(express.static(path.join(__dirname,"public")))
app.use("/uploads", express.static("uploads"));
const session = require('express-session');
app.use(session({
secret: "Keepmeloggedin247",
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // set to true only with HTTPS
maxAge: 1000 * 60 * 60 * 24
}
}));
app.get("/",(req,res)=>{
res.render("animate")
})
app.get("/register",(req,res)=>{
res.render("register_user")
})
app.get("/homepage2",(req,res)=>{
res.render("homepage2")
})
app.get("/get_certificate",async(req,res)=>{
const extract_member = await memberModel.find({ user: req.session.userId });
const vaccines = await vacModel.find();
const recommendedMap = extract_member.map(member => {
const filtered = vaccines.filter(v => member.age >= v.minAge && member.age <= v.maxAge);
return filtered.map(v => v.name);
});
res.render("get_certificate",{ extract_member,recommendedMap})
})
app.get("/homepage",(req,res)=>{
res.render("homepage")
})
app.get("/hospital", async (req, res) => {
let selectedVaccines = req.query.vaccines;
// If only one vaccine is selected, req.query.vaccines will be a string
if (!Array.isArray(selectedVaccines)) {
selectedVaccines = [selectedVaccines];
}
const allHospitals = await hospitalModel.find();
const paidHospitals = allHospitals.filter(h =>
h.hos_category === "Paid" &&
h.Vaccine_available.some(v => selectedVaccines.includes(v))
);
const freeHospitals = allHospitals.filter(h =>
h.hos_category === "Free" &&
h.Vaccine_available.some(v => selectedVaccines.includes(v))
);
res.render("hospitals", { paidHospitals, freeHospitals });
});
app.get("/track", async (req, res) => {
if (!req.session.userId) return res.redirect("register");
const extract_member = await memberModel.find({ user: req.session.userId });
const vaccines = await vacModel.find();
const recommendedMap = extract_member.map(member => {
const filtered = vaccines.filter(v => member.age >= v.minAge && member.age <= v.maxAge);
return filtered.map(v => v.name);
});
res.render("track_status", { extract_member,recommendedMap});
});
app.get("/add_member",async(req,res)=>{
res.render("add_member")
})
app.get("/login",async(req,res)=>{
res.render("login")
})
app.get("/add_hospital",(req,res)=>{
res.render("add_hospital")
})
app.get("/developer",(req,res)=>{
res.render("developer")
})
app.post("/register_user",async(req,res)=>{
let{email,phone,password,confirm_password}=req.body
bcrypt.genSalt(10,(err,Salt)=>{
bcrypt.hash(password,Salt,async(err,hash)=>{
let new_user=await userModel.create({
email,
phone,
password:hash,
confirm_password:hash
})
let token=jwt.sign({email},"store")
res.cookie("token",token)
res.redirect("interface")
})
})
})
function isLogged(req,res,next){
if(req.cookies.token === ""){
res.status(400).send("You need to login")
}
else{
let data=jwt.verify(req.cookies.token,"pause")
req.user = data
next();
}
}
app.post("/added_members", async (req, res) => {
let { name, age, gender, building, street, city, state, pincode, vaccines } = req.body;
if (!vaccines) vaccines = [];
else if (!Array.isArray(vaccines)) vaccines = [vaccines];
await memberModel.create({
name,
age,
gender,
building,
street,
city,
state,
pincode,
vaccines_taken: vaccines,
user: req.session.userId //link member to logged-in user
});
res.redirect("register")
});
app.post("/upload-document/:memberId", upload.single("pdf"), async (req, res) => {
const memberId = req.params.memberId;
if (!req.file) {
return res.status(400).send("No file uploaded or not a PDF.");
}
const documentPath = "/uploads/" + req.file.filename;
try {
await memberModel.findByIdAndUpdate(memberId, { document: documentPath });
res.redirect("/interface");
} catch (err) {
console.error(err);
res.status(500).send("Error uploading document");
}
});
app.post("/login_user", async (req, res) => {
const check_user = await userModel.findOne({ email: req.body.email });
bcrypt.compare(req.body.password, check_user.password, (err, result) => {
if (err) {
return res.status(500).send("Error during password check");
}
if (result) {
req.session.userId = check_user._id;
return res.redirect("homepage2");
} else {
return res.send("Incorrect password");
}
});
});
app.get("/logout",async(req,res)=>{
res.cookie("token","")
res.redirect("register")
})
app.get("/interface", async (req, res) => {
//if (!req.session.userId) return res.redirect("/login");
const extract_member = await memberModel.find({ user: req.session.userId });
const allHospitals = await hospitalModel.find();
const paidHospitals = allHospitals.filter(h => h.hos_category === "Paid");
const freeHospitals = allHospitals.filter(h => h.hos_category === "Free");
const vaccines = await vacModel.find(); // contains name, minAge, maxAge
const availableVaccines = vaccines.map(v => v.name);
const recommendedMap = extract_member.map(member => {
const filtered = vaccines.filter(v => member.age >= v.minAge && member.age <= v.maxAge);
return filtered.map(v => v.name);
});
res.render("interface", {
extract_member,
paidHospitals,
freeHospitals,
recommendedMap,
availableVaccines
});
});
//hospitals-site-to-add-their-respective-hospital
app.post("/added_hospital",async(req,res)=>{
let {hos_name,hos_email,hos_phone,hos_category,hos_vaccines,address_1,street,city,state,pincode}=req.body;``
if (!hos_vaccines) {
hos_vaccines = [];
} else if (!Array.isArray(hos_vaccines)) {
hos_vaccines = [hos_vaccines];
}
let hospitals=await hospitalModel.create({
hos_name,
hos_email,
hos_phone,
hos_category,
Vaccine_available : hos_vaccines,
address_1,
street,
city,
state,
pincode
})
res.send(hospitals)
})
//developers-site-to-add-vaccine
app.post("/add_vaccines", async (req, res) => {
try {
let { vaccines } = req.body;
if (!Array.isArray(vaccines)) return res.status(400).send("Invalid format");
await vacModel.insertMany(vaccines);
res.status(201).send("Vaccines added");
} catch (err) {
console.error(err);
res.status(500).send("Failed to add vaccines");
}
});
//gemini
app.post("/gemini_health_query", async (req, res) => {
function isValidHealthcareQuery(input) {
const keywords = [
"vaccine", "vaccination", "immunization", "dose", "hospital",
"side effects", "healthcare", "medicine", "disease", "covid",
"booster", "appointment", "doctor", "clinic", "injection"
];
const lowerInput = input.toLowerCase();
return keywords.some(keyword => lowerInput.includes(keyword));
}
const userInput = req.body.prompt;
if (!userInput || typeof userInput !== "string") {
return res.status(400).json({ error: "Invalid or missing prompt." });
}
if (!isValidHealthcareQuery(userInput)) {
return res.status(400).json({
error: "This assistant only handles healthcare and vaccine-related queries."
});
}
try {
const instruction = `Respond briefly in 1-2 lines. Only provide a detailed answer if the user asks for more details.`;
const result = await model.generateContent(instruction + "\n" + userInput);
const response = await result.response.text();
res.json({ response });
} catch (err) {
console.error(err);
res.status(500).json({ error: "Error generating response from Gemini." });
}
});
app.listen(3000)