-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
498 lines (438 loc) · 19.5 KB
/
test.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
require('dotenv').config(); // contains secure details about hash and salt to secure the password . Do not touch this, if you want your DB to be consistent.
const express = require('express'), // Includes Express, backend web framework to be used with Node.js
bodyParser = require('body-parser'), // library to parse details in the forms
ejs = require('ejs'), // embedded javascript to write javascript inside HTML to make it more robust and help in reusing a single piece of HTML code in multiple files
mongoose = require('mongoose'), // A medium for operating on our mongodb database using Node.js
passport = require('passport'), // An authentication and cookies library which helps in making passwords secure and hashed, adding salting.
passportLocal = require('passport-local'), // Used internally by passport. No explicit calls made in code yet
session = require('express-session'), // Saves user login session until logged out.
passportLocalMongoose = require('passport-local-mongoose'), // used to combine mongoose and passport to automatically save users into the Database.
ObjectId = require('mongodb').ObjectID, // Required to convert string into an objectId
path = require('path'), //Changing the File Streucture and hence need to modify some file strcutre
methodOverride = require("method-override"),
expressSanitizer = require("express-sanitizer");
const {
isObject
} = require('util');
const {
dateDiff
} = require('./dateDiff');
const app = express(); // We made an instance of the express framework here and will use it to further work with any type of requests.
app.use(bodyParser.urlencoded({
extended: true // We have integrated the body-parser into the express.
}));
app.set('view engine', 'ejs'); // use ejs as our view engine and access ejs using node. Hence, we have to get the ejs files from views folder.
const publicFilesDir = path.join(__dirname, '../public')
app.use(express.static(publicFilesDir)); // use the static files such as styles.css by mentioning this.
app.use(expressSanitizer());
app.use(methodOverride("_method"));
app.use(session({
secret: process.env.SECRET, // secret stored in a secret file
resave: false,
saveUninitialized: false
})); // Save the user session when logged in . This is integrated with the express framework now as well
app.use(passport.initialize()); // Initialise the passport library which does the function mentioned in the comment near the require statement of passport
app.use(passport.session()); // The main session starts here. Integrate Passport And Express session
mongoose.connect("mongodb+srv://admin-pratik:2zRzRbVAwHKxhbnh@cluster0-0iz6t.mongodb.net/TBS", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
// Connect mongoose to the localhost mongodb database. (While Development Phase). Change once produced.
const userSchema = new mongoose.Schema({
username: String,
password: String,
hotels: [{
id: String,
name: String,
rooms: Number,
checkIn: String,
checkOut: String,
price: Number
}]
}); // Create a schema for users having username and password, both as strings.
userSchema.plugin(passportLocalMongoose); // Plugin passportLocalMongoose within user scheme to integrate with the collection users
const user = new mongoose.model('User', userSchema); // create a new model named user. To be shifted to another file soon after changing of file system is discussed.
passport.use(user.createStrategy()); // passportLocalMongoose has its defualt strategy to use passport and hence used here.
passport.serializeUser(user.serializeUser()); // When user logs in, passport saves its details here.
passport.deserializeUser(user.deserializeUser()); // When user logs out, passport removes its details and cookies associated with the user
const hotelSchema = mongoose.Schema({
city: String,
hotelName: String,
stars: String,
contactNumber: String,
email: String,
imgURL: String,
rooms: Number,
price: Number,
customers: [{
name: String,
email: String,
price: Number,
roomsBooked: {
type: Number,
defualt: 1
},
checkIn: String,
checkOut: String
}]
});
// The schema for hotels . The data fields should be self-explanatory
const Hotels = new mongoose.model('Hotel', hotelSchema);
// Create a model using the hotelSchema
app.route('/') // Home route requests
.get(function (req, res) { // get requests for the home route
// console.log(req.user.username)
res.render('index', { // rendering the index file or home file
loggedIn: req.isAuthenticated(), // if the user is logged in, then logout is shown and vice versa. That is the pupose of this line
// val : req.isAuthenticated() ? req.user.username : "Nothing" // Testing purpose . req.user._details can be used to get _details field of the user who is logged in
});
});
app.route('/login') // login route requests handled here.
.get(function (req, res) { // get requests for the login route.
res.render('login'); // Renders the login page to the browsers
}).post(function (req, res) { // post requests to the login route.
let toBeLoggedInUser = new user({ // get details from the user typed data using body parser
username: req.body.username,
password: req.body.password
});
req.login(toBeLoggedInUser, function (err) { /// Use login method of passport and see the validity of the credentials
if (err) {
console.log(err); // let the server admin about potential errors
res.redirect('/login'); // If any errors are there, tell errors to developer, redirect user to login page again.
} else {
passport.authenticate('local')(req, res, function () {
res.redirect('/'); // If no problem , then redirect to home route . To choose among different options on the page
});
}
})
});
app.route("/logout") // logout route. Handled in ./views/login page by manipulating parameters
.get(function (req, res) {
req.logout(function (err) { // When the request to logout comes, log the user out of the page. Passport handles this
console.log(err); // If any errors encountered, inform the developer about the error
});
res.redirect('/'); // redirect the user to home page and log him out of the page.
});
app.route('/signup')
.get(function (req, res) { // Get the signup route
res.render('signup'); // render the signup page.
})
.post(function (req, res) {
user.register({ // passport takes care of registering the user and adds him to the DB.`
username: req.body.username // gets the username of the user.
}, req.body.password, function (err, newUser) { // encrypts the password
if (err) {
res.redirect('/signup'); // If any error encountered, redirect and request user to again signup
// alert('UIs')
} else {
passport.authenticate('local')(req, res, function () {
res.redirect('/'); // If he's authenticated successfully, redirect to home route and save his login session
});
}
});
});
app.get('/bookings', function (req, res) {
if (!req.isAuthenticated())
return res.redirect('/login')
res.render('bookings', {
loggedIn: req.isAuthenticated(),
data: req.user.hotels
})
})
app.post('/searchQuery', function (req, res) {
if (!req.isAuthenticated())
return res.redirect('/login');
const city = req.body.city;
res.redirect('/hotels/' + city);
})
// Route to get hotels sorted by particular city
app.route("/hotels/:cityName") // route to select hotels filtered by city names
.get(function (req, res) {
let city = req.params.cityName; // get the name of city from the express routing parameters
Hotels.find({
city: city // find all hotels having city field set to the requested name in the parameters
}, function (err, docs) {
res.render('hotels', {
loggedIn: req.isAuthenticated(),
hotels: docs
}) // render the ejs page, for only the filtered out hotels
});
})
app.route("/hotels") // routes for hotels all hotels
.get(function (req, res) {
Hotels.find({}, function (err, docs) {
res.render('hotels', {
hotels: docs,
loggedIn: req.isAuthenticated()
}); // get all hotels from the dba dn render it in the hotels ejs file. See the ejs file for more reference
})
})
.post(function (req, res) {
const newHotel = new Hotels({ // get the informations about the hotel using body-parser .Privilege given only to admins of hotels
city: req.body.city,
hotelName: req.body.name,
stars: req.body.stars,
contactNumber: req.body.phone,
email: req.body.email,
imgURL: req.body.imgURL,
rooms: parseInt(req.body.rooms),
price: req.body.price
});
newHotel.save(function (err) { // insert the hotel into the hotel collections
if (err)
console.log(err);
else
res.redirect('/hotels') // redirect to the hotels page
});
});
// Task for whoever is assigned task on this, add the put,patch and delete request features. I've left them empty while working on the frontend
app.route('/hotelAdmin') // Only privilege given to hotel admins. To upload the hotels
.get(function (req, res) {
res.render('hotels_admin', {
loggedIn: true
});
})
// One shortcoming, to save space on DB, ive used URL explicitly, anyone assigned this task is requested to come up with a better solution in which the proces occurs backgorund rather than infront of the user. Question of UX.
app.route('/book-hotel')
.post(function (req, res) {
if (!req.isAuthenticated())
res.redirect('/login');
else {
// console.log(req.user.username);
let passId = req.body.id;
res.redirect('/checkout/' + passId);
// res.redirect('/hotels');
}
});
app.route('/checkout/:passId') // Checkout page based on id of the hotel
.get(function (req, res) { // Gets the checkout route
let id = ObjectId(req.params.passId);
// let foundHotel = null;
Hotels.findById(id, function (err, docs) { // Searches the hotel.
if (err)
console.log(err);
else {
res.render('checkout', {
name: docs.hotelName,
city: docs.city,
id: docs.id,
loggedIn: req.isAuthenticated()
})
}
})
});
app.route('/confirm-book')
.post(function (req, res) {
const id = ObjectId(req.body.id);
Hotels.findById(id, function (err, docs) {
if (err)
console.log(err);
else if (docs.rooms < req.body.numRooms) { // Case when we don't have enough rooms in the hotel which are vacant
res.redirect('/hotels');
} else {
// return res.send(typeof req.body.checkIn);
const presentCustomers = docs.customers.filter(item => item.email === req.user.username);
if (presentCustomers.length > 0) {
console.log('No'); // We do not let any user book twice at a hotel and hence this line of code
} else {
// Calculate price of the stay based on difference in days. dateDiff implementation in src/dateDiff.js
const calcPrice = docs.price * req.body.numRooms * dateDiff(req.body.checkIn, req.body.checkOut);
docs.rooms -= req.body.numRooms; // reduce the number of rooms
docs.customers.push({
email: req.user.username,
roomsBooked: req.body.numRooms,
checkIn: req.body.checkIn,
checkOut: req.body.checkOut,
price: calcPrice
});
docs.save(); // update the hotel database
req.user.hotels.push({
id: id,
name: docs.hotelName,
rooms: req.body.numRooms,
checkIn: req.body.checkIn,
checkOut: req.body.checkOut,
price: calcPrice
});
req.user.save(); // update the user database
// console.log(user);
}
res.redirect('/');
}
})
});
app.post('/cancel-hotel', function (req, res) {
const ide = ObjectId(req.body.id);
Hotels.findById(ide, function (err, docs) {
// console.log(ide)
{
if (req.isAuthenticated()) { // if user is not authenticated he cannot cancel
if (!req.user.hotels.length > 0)
return res.redirect('/hotels');
let rooms = 0;
req.user.hotels = req.user.hotels.filter((hotel) => {
if (hotel.id === ide.toString())
rooms = hotel.rooms;
return hotel.id !== ide.toString();
})
req.user.save(); // remove the number of rooms and the hotel details from user's info
docs.rooms += rooms; // add up the rooms previously booked by the user
docs.customers = docs.customers.filter((customer) => {
return customer.email !== req.user.username // filter out the users
});
docs.save();
res.redirect('/hotels');
} else
return res.redirect('/login');
}
})
})
app.get('/manage-page/:id', function (req, res) {
const id = ObjectId(req.params.id);
Hotels.findById(id, function (error, docs) {
if (error)
return res.redirect('/')
if (docs) {
res.render('manager', {
loggedIn: true,
data: docs // show the hotel managing page to the manager or receptionist of the hotel
})
} else
res.redirect('/')
})
});
app.post('/check-click', function (req, res) { // works when called for checking out a customer from a hotel. accessed via the key provided to the hotel management
const id = ObjectId(req.body.hotelId); // get the hotel id
const mail = req.body.emailCustomer; // get the customer mail which can be used as a primary key
let rooms = 0;
user.findOne({
username: mail // search by the user's mail
}, function (err, docs) {
if (err)
return res.redirect('/manage-page/' + id);
if (docs) {
docs.hotels = docs.hotels.filter((hotel) => {
if (hotel.id == id.toString())
rooms = hotel.rooms;
return hotel.id !== id.toString();
})
docs.save(); // remove the hotel details from the user
Hotels.findById(id, function (err, docss) {
if (err) {
console.log(err)
return res.redirect('/manage-page/' + id);
}
if (docss) {
docss.rooms += rooms;
docss.customers = docss.customers.filter((customer) => {
return customer.email !== mail
});
docss.save(); // remove the user details from the hotel.
return res.redirect('/manage-page/' + id);
} else
return res.redirect('/manage-page/' + id);
// res.redirect('/')
})
})
})
//MONGOOSE Model Config.
var blogSchema = new mongoose.Schema({
title: String,
image: String,
body: String,
created: {
type: Date,
default: Date.now
}
});
var Blog = mongoose.model("Blog", blogSchema);
//RESTFUL Routes
// app.get("/",function (req,res){
// res.render("index");
// })
//INDEX
app.get("/blogs", function (req, res) {
Blog.find({}, function (err, blogs) {
if (err) {
console.log("ERROR!");
} else {
res.render("blog_index", {
blogs: blogs
});
}
})
})
//NEW
app.get("/blogs/new", function (req, res) {
res.render("blog_new");
})
//CREATE
app.post("/blogs", function (req, res) {
//sanitizing the post
req.body.blog.body = req.sanitize(req.body.blog.body);
Blog.create(req.body.blog, function (err, newBlog) {
if (err) {
res.render("blog_new");
} else {
res.redirect("/blogs");
}
});
});
//SHOW
app.get("/blogs/:id", function (req, res) {
Blog.findById(req.params.id, function (err, foundBlog) {
if (err) {
res.redirect("/blogs");
} else {
res.render("blog_show", {
blog: foundBlog
});
}
})
})
//EDIT
app.get("/blogs/:id/edit", function (req, res) {
Blog.findById(req.params.id, function (err, foundBlog) {
if (err) {
res.redirect("/blogs");
} else {
res.render("blog_edit", {
blog: foundBlog
});
}
})
})
//UPDATE
app.put("/blogs/:id", function (req, res) {
req.body.blog.body = req.sanitize(req.body.blog.body);
Blog.findByIdAndUpdate(req.params.id, req.body.blog, function (err, updatedBlog) {
if (err) {
res.redirect("/blogs");
} else {
res.redirect("/blogs/" + req.params.id);
}
})
})
////
//DESTROY
app.delete("/blogs/:id", function (req, res) {
Blog.findByIdAndRemove(req.params.id, function (err) {
if (err) {
res.redirect("/blogs");
} else {
res.redirect("/blogs");
}
})
})
app.get("*", function (req, res) {
res.render('404-page', {
loggedIn: req.isAuthenticated()
});
})
let port = process.env.PORT;
if (port == null || port == "")
port = 3000
app.listen(port, function () { // Start the server
console.log("Server Listening to port 3000");
});