-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
370 lines (293 loc) · 11.3 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
if(process.env.NODE_ENV !=='production'){
require('dotenv').config();
}
const express = require('express');
const ejsMate = require('ejs-mate');
const app = express();
const path = require('path');
const bodyParser = require("body-parser");
const mongoose = require('mongoose');
const MongoDBS = require('connect-mongo');
const Student = require('./models/studentSchema');
const passport = require('passport');
const passportLocal = require('passport-local');
const session = require('express-session');
const flash = require('connect-flash');
const { isLoggedIn } = require('./middleware');
const Request = require('./models/requestSchema');
const { sendMail } = require('./gmail/register');
const multer = require('multer');
const { storage } = require('./cloudinary/index');
const upload = multer({ storage });
const { approveMail } = require('./gmail/approved');
const catchAsync=require('./utils/catchAsync');
const { appliedMail } = require('./gmail/applied');
app.engine('ejs', ejsMate);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
const dburl = process.env.MONGO_ATLAS;
const secret = process.env.SECREAT;
mongoose.connect(dburl)
.then(() => {
console.log('connected');
})
.catch((err) => {
console.log('error');
console.log(err);
});
const store = new MongoDBS({
mongoUrl: dburl,
secret: "some",
touchAfter: 24 * 60 * 60
})
const sessionConfig = {
store: store,
secret: 'some',
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true,
expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
maxAge: 1000 * 60 * 60 * 24 * 7
}
}
app.use(session(sessionConfig));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new passportLocal(Student.authenticate()));
passport.serializeUser(Student.serializeUser());
passport.deserializeUser(Student.deserializeUser());
app.use(express.static('public'))
app.use((req, res, next) => {
// console.log(req.session);
// res.locals.currentUser=req.user;
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
next();
})
app.get('/signup', (req, res) => {
res.render('signup');
})
app.get('/', (req, res) => {
res.render('login');
})
app.get('/delete', async (req, res) => {
await Request.deleteMany({});
console.log('deleted');
})
app.get('/post',catchAsync( async (req, res) => {
const reqs = {
address: "asdf asdfas fdsaf asdf",
railway_line: "central",
class: "second",
starting: "ghansoli",
destination: "matunga",
student: "63c12e59996d902af649c8b8",
request_status: false
}
// const newReq=await Reuqest.find({}).populate('student');
const newReq = new Request(reqs);
await newReq.save();
// console.log(newReq);
res.send(newReq);
}))
app.get('/showReq', async (req, res) => {
const data = await Request.find({});
res.send(data);
})
app.get('/showStudent', async (req, res) => {
const data = await Request.find({});
res.send(data);
})
app.get('/institute_view',catchAsync( async (req, res) => {
// const StudentData=await Student.find({});
const Requests = await Request.find({ "request_status": "false" }).populate(
"student"
);
// console.log(StudentData);
res.render('institute_view', { Requests });
}))
app.get('/institute_view/aprroved',catchAsync( async (req, res) => {
const Requests = await Request.find({ request_status: true }).populate(
"student"
);
// console.log(Requests);
res.render('accepted', { Requests });
}))
app.get('/institute_view/:id',catchAsync( async (req, res) => {
const id = req.params.id;
const reqs = await Request.findById(id).populate('student');
console.log(reqs);
res.render('view', { reqs });
}))
app.post('/deleteAP/:id',catchAsync( async (req, res) => {
const id = req.params.id;
await Request.findByIdAndDelete(id);
req.flash('success','request deleted');
res.redirect('/institute_view');
}))
app.get('/institute_view/approved/:id',catchAsync( async (req, res) => {
const id = req.params.id;
const reqs = await Request.findById(id).populate('student');
// console.log(reqs);
res.render('viewA', { reqs });
}))
app.get('/institute_view/reverted/:id',catchAsync( async (req, res) => {
const req_id = req.params.id
console.log(req_id);
const request = await Request.findById(req_id).populate('student');
// console.log(request);
res.render('rejected', { request });
}))
app.post('/institue_view/reverted/:id',catchAsync( async (req, res) => {
const req_id = req.params.id
const request = await Request.findById(req_id).populate('student');
const id = request.student._id;
const student = await Student.findById(id);
const mailOptions = {
from: "[email protected]",
to: student.email,
subject: "Request Reverted",
html: `<p>Dear ${student.first_name} ${student.last_name}, your application for the concession has been reverted back due to the provided reason, <strong> ${req.body.reason} ${req.body.reason1} </strong>.</p> <p> You may now check the status of the application and make the required changes and re-apply for the application. </p>`
};
sendMail(mailOptions);
req.flash('error','Request has been Reverted');
res.redirect('/institute_view');
}))
app.post('/institute_view/accepted/:id',catchAsync( async (req, res) => {
const req_id = req.params.id
const request = await Request.findById(req_id).populate('student');
request.request_status = true;
await request.save();
const id = request.student._id;
const student = await Student.findById(id);
const mailOptions = {
from: "[email protected]",
to: student.email,
subject: "Request Approved",
html: `<p> Dear ${student.first_name} ${student.last_name}, your request for the railway concession has been approved.</p> <p>You may now visit the Railway Concession Office and get your approved concession, duly stamped and signed. </p>`
};
approveMail(mailOptions);
req.flash('success','Request has been Approved');
res.redirect('/institute_view');
}))
app.post('/application', passport.authenticate('local', { failureFlash: true, failureRedirect: '/' }),catchAsync( async (req, res) => {
const s = await Student.findOne({ username: req.session.passport.user });
res.render('application', { id: s._id });
}));
app.post('/application/basicdetails', isLoggedIn,catchAsync( async (req, res) => {
const user = await Student.findOne({ username: req.session.passport.user });
const id = user._id;
const st = await Student.findByIdAndUpdate({ _id: id }, { ...req.body }, {
returnOriginal: false
})
const caste = st.caste;
res.redirect(`/application/uploaddoc/${id}`);
}))
app.get('/application/uploaddoc/:id', isLoggedIn,catchAsync( async (req, res) => {
const st = await Student.findById(req.params.id);
res.render('documents', { st });
}))
app.post('/application/uploaddoc/:id', isLoggedIn, upload.array('image', 3), async (req, res) => {
images = req.files.map(f => ({ url: f.path, filename: f.filename }));
const student = await Student.findByIdAndUpdate({ _id: req.params.id }, { images });
res.redirect(`/application/conssesion/${req.params.id}`);
})
app.get('/application/conssesion/:id', isLoggedIn, async (req, res) => {
const student = await Student.findById(req.params.id);
// console.log(req.files);
// res.send('done');
res.render('concession-details', { student });
})
app.post('/application/conssesion/:id', isLoggedIn, async (req, res) => {
// const student=await Student.findByIdAndUpdate({_id:req.params._id},{...req.body});
const request = await Request({ ...req.body });
request.student = req.params.id;
const date= new Date();
const d=`${date.getDate()} / ${date.getMonth()+1} / ${date.getFullYear()}`;
request.date=d;
await request.save();
// const student=await Student.findById(req.params.id);
const student = await Student.findOneAndUpdate({ _id: req.params.id }, { $addToSet: { requests: { $each: [request._id] } } },{
returnOriginal:false
});
const mailOptions = {
from: "[email protected]",
to: student.email,
subject: "Application Submitted",
// text: "Testing mail sending using NodeJS"
html: `<p> Dear ${student.first_name} ${student.last_name}, You have successfully applied for the Railway Concession.</p> <p> You can always view you status of the application at Website. </p>`
};
appliedMail(mailOptions);
res.redirect(`/status/${student._id}`);
})
app.get('/status/:id', isLoggedIn, async (req, res) => {
// const re = await Request.findById(req.params.id);
const id=req.params.id;
// console.log(id);
const student=await Student.findById(id).populate({
path:'requests'
});
// console.log('in status');
// console.log(student);
res.render('status', { student });
})
app.get('/logout', isLoggedIn, async (req, res) => {
req.logout(function (err) {
if (err) {
// req.flash('error', 'something went wrong');
return res.redirect('/');
}
req.flash('success', 'You have successfully logged out');
res.redirect('/');
});
})
app.get('/institute_login', async (req, res) => {
res.render('institute_login');
});
app.post('/signup', async (req, res) => {
try{
// console.log(req.body);
// const student=await Student(req.body);
// await student.save();
// res.send(req.body);
const { username, email, password, first_name, last_name } = req.body;
const user = new Student({ email, username, first_name, last_name });
const registeredStudent = await Student.register(user, password);
req.login(registeredStudent, err => {
if (err) return next(err);
// req.flash('success', 'Welcom!!!');
// res.redirect('/home');
const mailOptions = {
from: "[email protected]",
to: registeredStudent.email,
subject: "Registration Successful",
// text: "Testing mail sending using NodeJS"
html: `<p>Dear User, you have successfully registered on our portal.</p> <p> Your USERNAME is <strong>${registeredStudent.username}</strong> and your registered email id is <strong>${registeredStudent.email}</strong>.</p> <p> You can now login, to apply for your concession at <strong>Login </strong>. </p>`
};
sendMail(mailOptions);
// res.send(registeredStudent);
res.redirect('/');
})}catch{
req.flash('error', 'User already exists');
res.redirect('/signup');
}
})
app.get('/view', (req, res) => {
res.render('view');
})
app.post('/institute_login', (req, res) => {
const { email, password } = req.body;
if (email === '[email protected]' && password === '123') {
res.redirect('/institute_view');
} else {
res.redirect('/institute_login');
}
})
const port=process.env.PORT|| 3000;
app.listen(port,()=>{
console.log(`Serving on port${port}`);
})