-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
684 lines (555 loc) · 20.9 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
require('dotenv').config();
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const connectBD = require('./config');
const { vbuCollege, KolhanUniv, JRSU, JWU, DSMPU, BBMKU, NPU, RU, SKMU, application } = require("./views/models/college_model");
const SIGNUP = require('./views/models/studentLogin_model');
const { clgLoginModel, clgAppliModel } = require("./views/models/collegeLogin_model");
const passport = require('passport');
const session = require('express-session');
const LocalStrategy = require('passport-local').Strategy;
const moment = require('moment-timezone');
const multer = require('multer');
const path = require('path');
const port = process.env.PORT;
connectBD();
const staticPath = path.join(__dirname, "./Public");
const uploadPath = path.join(__dirname, "./upload");
app.use(express.static(staticPath));
app.use(express.static(uploadPath));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
passport.use('student', new LocalStrategy(SIGNUP.authenticate()));
passport.use('college', new LocalStrategy(clgLoginModel.authenticate()));
passport.serializeUser((user, done) => {
done(null, { _id: user._id, type: user.constructor.modelName });
});
passport.deserializeUser((obj, done) => {
const Model = (obj.type == 'StudentSignup' ? SIGNUP : clgLoginModel);
Model.findById(obj._id, (err, user) => {
done(err, user);
})
})
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
const Storage = multer.diskStorage({
destination: "../upload",
filename: (req, file, callback) => {
callback(null, Date.now() + file.originalname);
}
})
const upload = multer({ storage: Storage });
app.get('/refresh', (req, res) => {
if (req.session.signupErr) {
delete req.session.signupErr;
}
res.redirect('/signup');
});
app.get("/", (req, res) => {
res.render('home', { logmessage: req.session.err });
});
app.get("/signup", (req, res) => {
res.render('signup', { message: req.session.signupErr });
})
// ----------------- Signup Start ----------------------
app.post("/signup", async (req, res) => {
try {
const username = await SIGNUP.findOne({ username: req.body.username });
const usermobile = await SIGNUP.findOne({ mobileno: req.body.mobile });
if (username) {
req.session.signupErr = "Email Id already exist!";
return res.redirect('/signup');
}
else if (usermobile) {
req.session.err = "Mobile no already exist!";
return res.redirect('/signup');
}
else {
await SIGNUP.register(new SIGNUP({
name: req.body.name,
username: req.body.username, // email
mobileno: req.body.mobile,
profile: "userdemo.png"
}),
req.body.password,
function (err, user) {
if (err) {
console.log(err);
req.session.signupErr = "Something went wrong please try again";
res.redirect('/signup');
}
else {
passport.authenticate("student")(req, res, function () {
req.session.signupErr = "Account Successfully Created Please Login!";
res.redirect('/signup');
});
}
}
)
}
} catch (err) {
console.log(err);
}
});
// ---------------- Signup end -----------------------
app.get("/student/dashboard", async (req, res) => {
try {
const stud = await SIGNUP.findOne({ username: req.session.usr });
const stuApp = await application.find({ emailId: stud.username })
if (req.isAuthenticated()) {
res.render('Dashboard', {
username: stud.name,
useremail: stud.username,
usermobile: stud.mobileno,
userprofile: stud.profile,
appDetails: stuApp
});
} else {
res.redirect("/");
}
} catch (err) {
console.log(err);
}
})
//-------------------- login start ---------------------
app.post("/", async (req, res) => {
try {
const logStu = new SIGNUP({
username: req.body.username,
password: req.body.password
});
if (req.body.username != "" || req.body.password != "") {
const stud = await SIGNUP.findOne({ username: req.body.username });
if (stud) {
await passport.authenticate("student", function (err, logStu, info) {
if (!logStu) {
req.session.err = "Invalid Username and Password!";
return res.redirect('/');
}
req.logIn(logStu, async function (err) {
if (err) {
console.log(err);
req.session.err = "Someting went wrong!";
res.redirect('/');
} else {
req.session.usr = stud.username;
res.redirect("/student/dashboard");
}
});
})(req, res);
} else {
req.session.err = "User does not exists!";
res.redirect('/');
}
} else {
req.session.err = "All field are mandatory!";
res.redirect('/');
}
} catch (error) {
console.log("Catch error : " + error);
}
});
// ------------------- logout start ------------------
app.get("/logout", (req, res) => {
req.logOut(function (err) {
if (err) {
console.log(err);
} else {
res.redirect("/");
}
});
});
// ------------------- logout end --------------------
//------------------------- Instute part start ----------
// Define the authentication strategy for the collegeLoginModal model
app.get("/collegelogin", (req, res) => {
res.render("CollegeLogin", { LogMessage: req.session.LogErr });
});
app.get("/college/collegeprofile", async (req, res) => {
try {
const clgFound = await clgLoginModel.findOne({ username: req.session.clgusr });
const collName = clgFound.coll_name;
const ugAppFound = await clgAppliModel.find({ collegeName: collName, courseType: "UG" });
const pgAppFound = await clgAppliModel.find({ collegeName: collName, courseType: "PG" });
if (req.isAuthenticated()) {
res.render("CollegeProfile", {
username: clgFound.coll_name,
userprofile: clgFound.profile,
ugApplication: ugAppFound,
pgApplication: pgAppFound,
});
} else {
res.redirect("/collegelogin")
}
} catch (err) {
console.log(err);
}
});
app.get("/collegesignup", async (req, res) => {
const vbucollege = await vbuCollege.find({});
const koluniv = await KolhanUniv.find({}).sort({ _id: 1 });
const Jrsu = await JRSU.find({}).sort({ _id: 1 });
const Jrw = await JWU.find({}).sort({ _id: 1 });
const Dsmpu = await DSMPU.find({}).sort({ _id: 1 });
const Bbmku = await BBMKU.find({}).sort({ _id: 1 });
const Npu = await NPU.find({}).sort({ _id: 1 });
const Ru = await RU.find({}).sort({ _id: 1 });
const Skmu = await SKMU.find({}).sort({ _id: 1 });
const allCollName = await [...vbucollege, ...koluniv, ...Jrsu, ...Jrw, ...Dsmpu, ...Bbmku, ...Npu, ...Ru, ...Skmu];
res.render("CollegeSignUp", { message: req.session.clgSign, college: allCollName });
})
// --------------- college Signup start ------------
app.post("/collegesignup", async (req, res) => {
try {
const collName = await clgLoginModel.findOne({ coll_name: req.body.instute });
// console.log(req.body.instute);
// console.log(collName);
// console.log(req.body.univ,);
if (collName) {
req.session.clgSign = "Account already exist!";
return res.redirect("/collegesignup");
}
else {
await clgLoginModel.register(new clgLoginModel({
university: req.body.university,
coll_name: req.body.instute,
username: req.body.username, // college_code
profile: "userdemo.png"
}), req.body.password, function (err, college) {
if (err) {
console.log("Error : " + err);
res.redirect("/collegesignup");
} else {
passport.authenticate('college')(req, res, function () {
req.session.clgSign = "Account Successfully created please login!";
res.redirect("/collegesignup");
});
}
});
}
} catch (err) {
console.log("Error Catched : " + err);
}
})
// --------------- college Signup end --------------
// --------------- college login start --------------
app.post("/collegelogin", async (req, res) => {
try {
const logStu = new clgLoginModel({
username: req.body.username,
password: req.body.password
});
if (req.body.username != "" || req.body.password != "") {
const stud = await clgLoginModel.findOne({ username: req.body.username });
if (stud) {
await passport.authenticate("college", function (err, logStu, info) {
if (!logStu) {
req.session.LogErr = "Invalid Username and Password!";
return res.redirect('/collegelogin');
}
req.logIn(logStu, async function (err) {
if (err) {
console.log(err);
req.session.LogErr = "Someting went wrong!";
res.redirect('/collegelogin');
} else {
req.session.clgusr = stud.username;
res.redirect("/college/collegeprofile");
}
});
})(req, res);
} else {
req.session.LogErr = "User does not exists!";
res.redirect('/collegelogin');
}
} else {
req.session.LogErr = "All field are mandatory!";
res.redirect('/collegelogin');
}
} catch (error) {
console.log("Catch error : " + error);
}
});
// --------------- college login end --------------
// ------------ open application ------------------
app.get("/college/collegeprofile/application", async (req, res) => {
try {
const clgFound = await clgLoginModel.findOne({ username: req.session.clgusr });
if (req.isAuthenticated()) {
res.render('collegeApplication', {
username: clgFound.coll_name,
year: year,
university: clgFound.university
});
} else {
res.redirect("/collegelogin")
}
} catch (err) {
console.log(err);
}
})
app.get("/college/collegeprofile/application/update", async (req, res) => {
const id = req.query.id;
try {
const applcationFound = await clgAppliModel.findById(id);
const fromDate = moment.tz(applcationFound.from, 'Asia/Kolkata');
const from = fromDate.format('YYYY-MM-DD hh:mm A');
const toDate = moment.tz(applcationFound.To, 'Asia/Kolkata');
const to = toDate.format('YYYY-MM-DD hh:mm A');
// console.log("from : " + from);
// console.log("to : " + to);
if (req.isAuthenticated()) {
res.render('clgAppUpdate', {
year: year,
appDetails: applcationFound,
start: applcationFound.from,
end: applcationFound.To
});
} else {
res.redirect("/collegelogin")
}
} catch (err) {
console.log(err);
}
});
app.post("/update/:id", async (req, res) => {
const id = req.params.id;
// convert to moment object and set the timezone to Indian Standard Time
const fromDate = moment.tz(req.body.from, 'Asia/Kolkata');
// format the date in 12-hour format with Indian time standard
const from = fromDate.format('YYYY-MM-DD hh:mm A');
const toDate = moment.tz(req.body.to, 'Asia/Kolkata');
const to = toDate.format('YYYY-MM-DD hh:mm A');
// console.log("from : " + from);
// console.log("to : " + to);
const updatedData = {
...req.body,
from: from,
To: to
};
try {
const updatedApplication = await clgAppliModel.findByIdAndUpdate(id, { $set: updatedData }, { new: true });
console.log("Application Successfully Updated!");
res.redirect("/college/collegeprofile");
}
catch (err) {
console.log("Catched error : " + err);
// res.status(500).send(err);
}
});
app.get("/college/collegeprofile/studentapplication", async (req, res) => {
try {
const clgFound = await clgLoginModel.findOne({ username: req.session.clgusr });
const collName = clgFound.coll_name;
const ugAppFound = await application.find({ collegeName: collName, course: "UG" });
const pgAppFound = await application.find({ collegeName: collName, course: "PG" });
if (req.isAuthenticated()) {
res.render("viewApplication", { ugApplication: ugAppFound, pgApplication: pgAppFound });
} else {
res.redirect("/collegelogin")
}
} catch (err) {
console.log(err);
}
})
app.post("/application/status/:id", async (req, res) => {
const id = req.params.id;
const update = (req.body.reject) === 'Rejected' ? 'Rejected' : 'Accept';
try {
await application.findByIdAndUpdate(id, { $set: { status: update } }, { new: true });
console.log("status updated successfully!");
res.redirect("/college/collegeprofile/studentapplication");
} catch (err) {
console.log(err);
}
})
app.post("/clgapplication", async (req, res) => {
// const value = req.body;
// console.log("all data" + value);
const fromDate = moment.tz(req.body.from, 'Asia/Kolkata');
const from = fromDate.format('YYYY-MM-DD hh:mm A');
const toDate = moment.tz(req.body.to, 'Asia/Kolkata');
const to = toDate.format('YYYY-MM-DD hh:mm A');
const openApplication = new clgAppliModel({
university: req.body.universityName,
collegeName: req.body.collegeName,
courseType: req.body.courseType,
from: from,
To: to,
course: req.body.course,
notice: req.body.notice
});
try {
if (req.isAuthenticated()) {
await openApplication.save();
req.session.appSuccess = "Application Successfully Submited!";
res.redirect("/college/collegeprofile");
} else {
res.redirect("/collegelogin")
}
} catch (err) {
console.log(err);
}
})
app.post("/clgappdelete", async (req, res) => {
try {
const removedDocument = await clgAppliModel.findByIdAndRemove(req.body.delInp);
console.log("Record Successfully deleted .......");
res.redirect("/college/collegeprofile");
} catch (err) {
console.log(err);
res.redirect("/college/collegeprofile");
}
});
//------------------------- Instute part end ----------
// ---------------------- application start -----------
app.get("/student/dashboard/applicatiion", async (req, res) => {
const today = new Date();
try {
const stud = await SIGNUP.findOne({ username: req.session.usr });
const college = await clgAppliModel.find({
$and: [
{ from: { $lte: today } },
{ To: { $gte: today } }
]
});
if (req.isAuthenticated()) {
res.render('Application', {
username: stud.name,
useremail: stud.username,
usermobile: stud.mobileno,
userprofile: stud.profile,
Course: [req.session.course, req.session.crs],
session: req.session.session,
appNo: req.session.appNo,
formNo: req.session.formNo,
clgOpen: college
});
} else {
res.redirect("/");
}
} catch (err) {
console.log(err);
}
})
// Generate a unique form number using timestamp
function generateRegistrationNumber() {
const timestamp = new Date().getTime();
const registrationNumber = timestamp + timestamp;
return registrationNumber;
}
const year = new Date().getFullYear();
app.post("/application", (req, res) => {
const formNo = generateRegistrationNumber() + generateRegistrationNumber();
const Course = req.body.course;
req.session.crs = Course;
req.session.course = Course === "UG" ? `Under Graduate Admission ${year}` : `Post Graduate Admission ${year}`;
req.session.session = Course === "UG" ? `${year}-${year + 3}` : `${year}-${year + 2}`;
req.session.appNo = generateRegistrationNumber().toString().substring(4, 12);
req.session.formNo = formNo.toString().substring(7, 12);
res.redirect("/student/dashboard/applicatiion");
})
app.post("/stuapplication", upload.fields([
{ name: 'photofile', maxCount: 1 },
{ name: 'sigfile', maxCount: 1 }
]), (req, res) => {
const { photofile, sigfile } = req.files;
// console.log(photofile[0].filename);
// console.log(sigfile[0].filename);
const newApplication = new application({
appliedSession: req.body.appliedSession,
applicationNo: req.body.applicationNo,
formNo: req.body.formNo,
course: req.body.course,
fName: req.body.fName,
mName: req.body.mName,
lName: req.body.lName,
fatherName: req.body.FatherName,
motherName: req.body.MotherName,
mobileNo: req.body.mobileNo,
emailId: req.body.emailId,
gender: req.body.gender,
cast: req.body.cast,
dateOfBirth: req.body.dob,
addressLine1: req.body.addL1,
addressLine2: req.body.addL2,
block: req.body.block,
district: req.body.distric,
pincode: req.body.pinCode,
lastPassedExam: req.body.lastPassedExam,
universityNameLastPassed: req.body.LPassUniversity,
session: req.body.session,
registrationNo: req.body.regNo,
passingYear: req.body.passYear,
courseTypeStudied: req.body.courseTypeStudied,
subjectStudied: req.body.subject,
cgpa: req.body.cgpa,
percentageOfMarks: req.body.percentMarks,
totalCredit: req.body.totalMarks,
totalSecuredCredit: req.body.totalMarksSecured,
university: req.body.univName,
collegeName: req.body.clgName,
stream: req.body.stream,
courseType: req.body.courseType,
status: "pending",
photo: photofile[0].filename,
signature: sigfile[0].filename
})
try {
newApplication.save();
res.redirect("/student/dashboard");
} catch (err) {
console.log(err);
}
});
app.get("/student/print", async (req, res) => {
const id = req.query.id;
const stuDetails = await application.findById(id);
res.render("./print", { details: stuDetails });
})
// ---------------------- application end -------------
app.get("/university", async (req, res) => {
const vbucollege = await vbuCollege.find({});
const koluniv = await KolhanUniv.find({}).sort({ _id: 1 });
const Jrsu = await JRSU.find({}).sort({ _id: 1 });
const Jrw = await JWU.find({}).sort({ _id: 1 });
const Dsmpu = await DSMPU.find({}).sort({ _id: 1 });
const Bbmku = await BBMKU.find({}).sort({ _id: 1 });
const Npu = await NPU.find({}).sort({ _id: 1 });
const Ru = await RU.find({}).sort({ _id: 1 });
const Skmu = await SKMU.find({}).sort({ _id: 1 });
await res.render('University', {
vbuCollege: vbucollege,
kolhanUniv: koluniv,
jrsu: Jrsu,
jrw: Jrw,
dsmpu: Dsmpu,
bbmku: Bbmku,
npu: Npu,
ru: Ru,
skmu: Skmu
});
});
app.get("/notification", async (req, res) => {
try {
const college = await clgAppliModel.find({});
res.render("notification", { clgNotice: college });
} catch (err) {
console.log(err);
}
})
app.get("/contact", (req, res) => {
res.render("contact");
})
app.get("*", (req, res) => { // treted as a universal page if above are not matched
res.status(404).send("<h1>Oops this about page couldn't be found!</h1>");
})
app.listen(port, () => {
console.log(`we are listening at port number ${port}`);
})