From d621792981d7295b12310322e3e49f1547599ac3 Mon Sep 17 00:00:00 2001 From: Sanjay Date: Sun, 28 Jul 2019 23:49:54 +0530 Subject: [PATCH 01/11] Added BookingSchema --- models/Booking.js | 14 ++++++++++++++ models/index.js | 1 + 2 files changed, 15 insertions(+) create mode 100644 models/Booking.js diff --git a/models/Booking.js b/models/Booking.js new file mode 100644 index 0000000..4296cfa --- /dev/null +++ b/models/Booking.js @@ -0,0 +1,14 @@ +const mongoose = require('mongoose') +const package = require('./Package') +const { Schema } = mongoose +const BookingSchema = new Schema({ + packageid: { + type: Schema.ObjectId, + ref: package, + }, + Name: { + type: package.name, + ref: package, + }, +}) +module.exports = mongoose.model('booking', BookingSchema) diff --git a/models/index.js b/models/index.js index ca08567..e8ded7e 100644 --- a/models/index.js +++ b/models/index.js @@ -3,4 +3,5 @@ module.exports = { Package: require('./Package'), Itenary: require('./Itenary'), Category: require('./Category'), + Booking: require('./Booking'), } From 2daf18778237480e01040887da3f76bfcfe7044f Mon Sep 17 00:00:00 2001 From: Sanjay Date: Mon, 29 Jul 2019 00:23:40 +0530 Subject: [PATCH 02/11] Added Controllers and Routes for Booking --- controllers/Booking.js | 25 +++++++++++++++++++++++++ models/Booking.js | 22 +++++++++++++--------- routes/Booking.js | 7 +++++++ routes/index.js | 3 +++ 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 controllers/Booking.js create mode 100644 routes/Booking.js diff --git a/controllers/Booking.js b/controllers/Booking.js new file mode 100644 index 0000000..7151664 --- /dev/null +++ b/controllers/Booking.js @@ -0,0 +1,25 @@ +const { Booking, Package, User } = require('../models') +const { success, failure } = require('./helpers') + +exports.booking = (req, res) => { + const id = req.body.packageid + Package.findById(id) + .select() + .then(package => { + const { userId } = req.session + if (userId) { + User.findById(userId).then(() => { + const packagebooked = new Booking({ + packageid: id, + Name: package.name, + }) + packagebooked.save().then(bookeddetails => { + res.send(success(bookeddetails)) + }) + }) + } + }) + .catch(err => { + res.send(failure(err)) + }) +} diff --git a/models/Booking.js b/models/Booking.js index 4296cfa..ced7ee2 100644 --- a/models/Booking.js +++ b/models/Booking.js @@ -1,14 +1,18 @@ const mongoose = require('mongoose') const package = require('./Package') const { Schema } = mongoose -const BookingSchema = new Schema({ - packageid: { - type: Schema.ObjectId, - ref: package, +const BookingSchema = new Schema( + { + packageid: { + type: Schema.ObjectId, + ref: package, + }, + Name: { + type: String, + minlength: 3, + required: true, + }, }, - Name: { - type: package.name, - ref: package, - }, -}) + { versionKey: false }, +) module.exports = mongoose.model('booking', BookingSchema) diff --git a/routes/Booking.js b/routes/Booking.js new file mode 100644 index 0000000..299bb7f --- /dev/null +++ b/routes/Booking.js @@ -0,0 +1,7 @@ +const { Router } = require('express') +const controller = require('../controllers/Booking') +const router = Router() + +router.post('/', controller.booking) + +module.exports = router diff --git a/routes/index.js b/routes/index.js index d16de77..810f4aa 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,6 +4,7 @@ const CategoryRouter = require('./Category') const UserRouter = require('./User') const ItenaryRouter = require('./Itenary') const SearchRouter = require('./Search') +const BookingRouter = require('./Booking') const middlewares = require('./middlewares') const router = Router() @@ -30,4 +31,6 @@ router.use('/itenary', ItenaryRouter) router.use('/search', SearchRouter) +router.use('/book', BookingRouter) + module.exports = router From 3f229a5082d02de32dd75a7ff4b1131b33628b1a Mon Sep 17 00:00:00 2001 From: Sanjay Date: Mon, 29 Jul 2019 00:23:40 +0530 Subject: [PATCH 03/11] Added Controllers and Routes for Booking --- controllers/Booking.js | 20 ++++++++++++++++++++ models/Booking.js | 22 +++++++++++++--------- routes/Booking.js | 8 ++++++++ routes/index.js | 3 +++ routes/middlewares.js | 15 +++++++++++++++ 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 controllers/Booking.js create mode 100644 routes/Booking.js diff --git a/controllers/Booking.js b/controllers/Booking.js new file mode 100644 index 0000000..d3fc561 --- /dev/null +++ b/controllers/Booking.js @@ -0,0 +1,20 @@ +const { Booking, Package, User } = require('../models') +const { success, failure } = require('./helpers') + +exports.booking = (req, res) => { + const id = req.body.packageid + Package.findById(id) + .select() + .then(package => { + const packagebooked = new Booking({ + packageid: id, + Name: package.name, + }) + packagebooked.save().then(bookeddetails => { + res.send(success(bookeddetails)) + }) + }) + .catch(err => { + res.send(failure(err)) + }) +} diff --git a/models/Booking.js b/models/Booking.js index 4296cfa..ced7ee2 100644 --- a/models/Booking.js +++ b/models/Booking.js @@ -1,14 +1,18 @@ const mongoose = require('mongoose') const package = require('./Package') const { Schema } = mongoose -const BookingSchema = new Schema({ - packageid: { - type: Schema.ObjectId, - ref: package, +const BookingSchema = new Schema( + { + packageid: { + type: Schema.ObjectId, + ref: package, + }, + Name: { + type: String, + minlength: 3, + required: true, + }, }, - Name: { - type: package.name, - ref: package, - }, -}) + { versionKey: false }, +) module.exports = mongoose.model('booking', BookingSchema) diff --git a/routes/Booking.js b/routes/Booking.js new file mode 100644 index 0000000..df26265 --- /dev/null +++ b/routes/Booking.js @@ -0,0 +1,8 @@ +const { Router } = require('express') +const controller = require('../controllers/Booking') +const middlewares = require('./middlewares') +const router = Router() + +router.post('/', middlewares.Loggedin, controller.booking) + +module.exports = router diff --git a/routes/index.js b/routes/index.js index d16de77..810f4aa 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,6 +4,7 @@ const CategoryRouter = require('./Category') const UserRouter = require('./User') const ItenaryRouter = require('./Itenary') const SearchRouter = require('./Search') +const BookingRouter = require('./Booking') const middlewares = require('./middlewares') const router = Router() @@ -30,4 +31,6 @@ router.use('/itenary', ItenaryRouter) router.use('/search', SearchRouter) +router.use('/book', BookingRouter) + module.exports = router diff --git a/routes/middlewares.js b/routes/middlewares.js index 92b00ae..b0187a3 100644 --- a/routes/middlewares.js +++ b/routes/middlewares.js @@ -51,6 +51,21 @@ module.exports = { res.send(err) }) }, + + Loggedin: function(req, res, next) { + const { userId } = req.session + User.findById(userId) + .then(user => { + if (user) { + next() + } else { + res.send('Log in to Book') + } + }) + .catch(err => { + res.send('Booking Failed') + }) + }, } // isAuthenticatinon From 5327029e544e80af83ffbba5b04bc927efeac888 Mon Sep 17 00:00:00 2001 From: Sanjay Date: Mon, 29 Jul 2019 12:09:57 +0530 Subject: [PATCH 04/11] Updated Booking Model --- controllers/Booking.js | 4 +++- models/Booking.js | 10 +++++++++- routes/middlewares.js | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/controllers/Booking.js b/controllers/Booking.js index d3fc561..622b7e4 100644 --- a/controllers/Booking.js +++ b/controllers/Booking.js @@ -6,8 +6,10 @@ exports.booking = (req, res) => { Package.findById(id) .select() .then(package => { + const userid = req.session.userId const packagebooked = new Booking({ - packageid: id, + Packageid: id, + Userid: userid, Name: package.name, }) packagebooked.save().then(bookeddetails => { diff --git a/models/Booking.js b/models/Booking.js index ced7ee2..adec16a 100644 --- a/models/Booking.js +++ b/models/Booking.js @@ -1,12 +1,20 @@ const mongoose = require('mongoose') const package = require('./Package') +const user = require('./User') +// const { User } = require('./index') Is this valid here ? const { Schema } = mongoose const BookingSchema = new Schema( { - packageid: { + Packageid: { type: Schema.ObjectId, ref: package, }, + + Userid: { + type: Schema.ObjectId, + ref: user, + }, + Name: { type: String, minlength: 3, diff --git a/routes/middlewares.js b/routes/middlewares.js index b0187a3..14eee8b 100644 --- a/routes/middlewares.js +++ b/routes/middlewares.js @@ -57,6 +57,7 @@ module.exports = { User.findById(userId) .then(user => { if (user) { + console.log(userId) next() } else { res.send('Log in to Book') From 8b5f000ed53989e30959dab7ce5f75dd390eb349 Mon Sep 17 00:00:00 2001 From: Sanjay Date: Tue, 30 Jul 2019 13:36:30 +0530 Subject: [PATCH 05/11] Added ViewBooking Route And TimeStamp for Booking-Module --- controllers/Booking.js | 28 +++++++++++++++++++++++----- controllers/Package.js | 2 +- models/Booking.js | 18 ++++++++---------- routes/Booking.js | 4 +++- routes/middlewares.js | 1 - 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/controllers/Booking.js b/controllers/Booking.js index 622b7e4..9073602 100644 --- a/controllers/Booking.js +++ b/controllers/Booking.js @@ -1,16 +1,15 @@ const { Booking, Package, User } = require('../models') const { success, failure } = require('./helpers') -exports.booking = (req, res) => { - const id = req.body.packageid +exports.postBooking = (req, res) => { + const id = req.body.package Package.findById(id) .select() .then(package => { const userid = req.session.userId const packagebooked = new Booking({ - Packageid: id, - Userid: userid, - Name: package.name, + package: id, + user: userid, }) packagebooked.save().then(bookeddetails => { res.send(success(bookeddetails)) @@ -20,3 +19,22 @@ exports.booking = (req, res) => { res.send(failure(err)) }) } + +exports.viewbooking = (req, res) => { + const id = req.params.viewbooking + Booking.findById(id) + .select() + .then(bookingdetails => { + Package.find({ _id: { $in: bookingdetails.package } }) + .select({ + _id: 1, + name: 1, + }) + .then(package => { + res.send({ + ...bookingdetails.toObject(), + package, + }) + }) + }) +} diff --git a/controllers/Package.js b/controllers/Package.js index 842139d..f43d360 100644 --- a/controllers/Package.js +++ b/controllers/Package.js @@ -91,7 +91,7 @@ exports.postPackage = ({ body }, res) => { packagedetails .save() .then(() => { - res.send(success(packagedetials)) + res.send(success(packagedetails)) }) .catch(err => { res.send(failure(err)) diff --git a/models/Booking.js b/models/Booking.js index adec16a..5991880 100644 --- a/models/Booking.js +++ b/models/Booking.js @@ -5,21 +5,19 @@ const user = require('./User') const { Schema } = mongoose const BookingSchema = new Schema( { - Packageid: { - type: Schema.ObjectId, - ref: package, - }, + package: [ + { + type: Schema.ObjectId, + ref: package, + }, + ], - Userid: { + user: { type: Schema.ObjectId, ref: user, }, - Name: { - type: String, - minlength: 3, - required: true, - }, + time: { type: Date, default: Date.now }, }, { versionKey: false }, ) diff --git a/routes/Booking.js b/routes/Booking.js index df26265..3ee280f 100644 --- a/routes/Booking.js +++ b/routes/Booking.js @@ -3,6 +3,8 @@ const controller = require('../controllers/Booking') const middlewares = require('./middlewares') const router = Router() -router.post('/', middlewares.Loggedin, controller.booking) +router.post('/', middlewares.Loggedin, controller.postBooking) + +router.get('/:viewbooking', controller.viewbooking) module.exports = router diff --git a/routes/middlewares.js b/routes/middlewares.js index 14eee8b..b0187a3 100644 --- a/routes/middlewares.js +++ b/routes/middlewares.js @@ -57,7 +57,6 @@ module.exports = { User.findById(userId) .then(user => { if (user) { - console.log(userId) next() } else { res.send('Log in to Book') From db211dd41f86847d6ff3416cd463abebbe7c4635 Mon Sep 17 00:00:00 2001 From: Sanjay Date: Fri, 2 Aug 2019 23:36:32 +0530 Subject: [PATCH 06/11] Included Slug in /all in Packages --- controllers/Package.js | 1 + 1 file changed, 1 insertion(+) diff --git a/controllers/Package.js b/controllers/Package.js index f43d360..6ad59d6 100644 --- a/controllers/Package.js +++ b/controllers/Package.js @@ -55,6 +55,7 @@ exports.getAllPackages = (req, res) => { name: 1, description: 1, images: 1, + slug: 1, }) .then(allpackages => { res.send(allpackages) From 9297cd251b7208c92fec2fba3bb74e9f77e3b69c Mon Sep 17 00:00:00 2001 From: Sanjay Date: Sat, 3 Aug 2019 12:04:00 +0530 Subject: [PATCH 07/11] Modified FilterUser --- controllers/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/helpers.js b/controllers/helpers.js index 4ce54c2..e677e1b 100644 --- a/controllers/helpers.js +++ b/controllers/helpers.js @@ -1,7 +1,7 @@ module.exports = { filterUser: function(user) { delete user.password - delete user._id + // delete user._id return user }, From aa75be26ad301048e0f680227e655ffc9c58a3c8 Mon Sep 17 00:00:00 2001 From: Sanjay Date: Sun, 4 Aug 2019 00:57:41 +0530 Subject: [PATCH 08/11] Modified filterUser --- controllers/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/helpers.js b/controllers/helpers.js index e677e1b..4ce54c2 100644 --- a/controllers/helpers.js +++ b/controllers/helpers.js @@ -1,7 +1,7 @@ module.exports = { filterUser: function(user) { delete user.password - // delete user._id + delete user._id return user }, From dcf420f2d23231bc54c5b92de44999217ef02a1c Mon Sep 17 00:00:00 2001 From: Sanjay Date: Sun, 4 Aug 2019 12:44:10 +0530 Subject: [PATCH 09/11] Updated Booking Model and Controller --- controllers/Booking.js | 44 ++++++++++++++++++------ models/Booking.js | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 10 deletions(-) diff --git a/controllers/Booking.js b/controllers/Booking.js index 9073602..a0746aa 100644 --- a/controllers/Booking.js +++ b/controllers/Booking.js @@ -7,16 +7,40 @@ exports.postBooking = (req, res) => { .select() .then(package => { const userid = req.session.userId - const packagebooked = new Booking({ - package: id, - user: userid, - }) - packagebooked.save().then(bookeddetails => { - res.send(success(bookeddetails)) - }) - }) - .catch(err => { - res.send(failure(err)) + User.findById(userid) + .select() + .then(user => { + const packagebooked = new Booking({ + package: id, + user: userid, + dateoftravel: req.body.dateoftravel, + person: [ + { + title: req.body.title, + first_name: user.first_name, + last_name: user.last_name, + }, + ], + contactdetails: { + first_name: user.first_name, + last_name: user.last_name, + phone: user.phone, + email: user.email, + }, + billingaddress: { + address: req.body.address, + city: req.body.city, + postalcode: req.body.postalcode, + country: req.body.country, + }, + }) + packagebooked.save().then(bookeddetails => { + res.send(success(bookeddetails)) + }) + }) + .catch(err => { + res.send(failure(err)) + }) }) } diff --git a/models/Booking.js b/models/Booking.js index 5991880..2c17c17 100644 --- a/models/Booking.js +++ b/models/Booking.js @@ -17,6 +17,84 @@ const BookingSchema = new Schema( ref: user, }, + dateoftravel: { + type: Date, + required: true, + }, + + person: [ + { + title: { + type: String, + required: true, + }, + first_name: { + type: String, + required: true, + minlength: 4, + }, + last_name: { + type: String, + minlength: 4, + trim: true, + }, + }, + ], + + contactdetails: { + first_name: { + type: String, + required: true, + minlength: 4, + }, + last_name: { + type: String, + minlength: 4, + trim: true, + }, + phone: { + type: Number, + minlength: 9, + maxlength: 11, + required: true, + }, + email: { + type: String, + trim: true, + lowercase: true, + unique: true, + required: 'Email address is required', + match: [ + /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, + 'Please fill a valid email address', + ], + }, + }, + + billingaddress: { + address: { + type: String, + required: true, + trim: true, + }, + city: { + type: String, + required: true, + trim: true, + }, + postalcode: { + type: Number, + required: true, + minlength: 6, + maxlength: 6, + }, + country: { + type: String, + required: true, + trim: true, + }, + }, + time: { type: Date, default: Date.now }, }, { versionKey: false }, From c8d3a052c899a08d6e4175d360916990f6d33327 Mon Sep 17 00:00:00 2001 From: Sanjay Date: Mon, 5 Aug 2019 14:09:43 +0530 Subject: [PATCH 10/11] Updated Iternaries --- controllers/Booking.js | 12 +++++------- controllers/Package.js | 2 +- models/Booking.js | 20 ++------------------ models/Person.js | 21 +++++++++++++++++++++ models/index.js | 1 + 5 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 models/Person.js diff --git a/controllers/Booking.js b/controllers/Booking.js index a0746aa..9c827dd 100644 --- a/controllers/Booking.js +++ b/controllers/Booking.js @@ -14,13 +14,11 @@ exports.postBooking = (req, res) => { package: id, user: userid, dateoftravel: req.body.dateoftravel, - person: [ - { - title: req.body.title, - first_name: user.first_name, - last_name: user.last_name, - }, - ], + person: { + title: req.body.title, + first_name: user.first_name, + last_name: user.last_name, + }, contactdetails: { first_name: user.first_name, last_name: user.last_name, diff --git a/controllers/Package.js b/controllers/Package.js index 6ad59d6..17bb067 100644 --- a/controllers/Package.js +++ b/controllers/Package.js @@ -79,7 +79,7 @@ exports.postPackage = ({ body }, res) => { duration: body.duration, activites: body.activites, images: body.images, - itenary: body.itenary, + itenaries: body.itenaries, categories: body.categories, inclusions: body.inclusions, exclusions: body.exclusions, diff --git a/models/Booking.js b/models/Booking.js index 2c17c17..798335a 100644 --- a/models/Booking.js +++ b/models/Booking.js @@ -1,6 +1,7 @@ const mongoose = require('mongoose') const package = require('./Package') const user = require('./User') +const personschema = require('./Person').schema // const { User } = require('./index') Is this valid here ? const { Schema } = mongoose const BookingSchema = new Schema( @@ -22,24 +23,7 @@ const BookingSchema = new Schema( required: true, }, - person: [ - { - title: { - type: String, - required: true, - }, - first_name: { - type: String, - required: true, - minlength: 4, - }, - last_name: { - type: String, - minlength: 4, - trim: true, - }, - }, - ], + person: [personschema], contactdetails: { first_name: { diff --git a/models/Person.js b/models/Person.js new file mode 100644 index 0000000..54a38f4 --- /dev/null +++ b/models/Person.js @@ -0,0 +1,21 @@ +const mongoose = require('mongoose') +const { Schema } = mongoose + +const PersonSchema = new Schema({ + title: { + type: String, + required: true, + }, + first_name: { + type: String, + required: true, + minlength: 4, + }, + last_name: { + type: String, + minlength: 4, + trim: true, + }, +}) + +module.exports = mongoose.model('Registration Details', PersonSchema) diff --git a/models/index.js b/models/index.js index e8ded7e..2cafc70 100644 --- a/models/index.js +++ b/models/index.js @@ -4,4 +4,5 @@ module.exports = { Itenary: require('./Itenary'), Category: require('./Category'), Booking: require('./Booking'), + Person: require('./Person'), } From 6fb3de6cc12a9c7832b952da9ad1460be4ae6523 Mon Sep 17 00:00:00 2001 From: Sanjay Date: Tue, 6 Aug 2019 23:17:32 +0530 Subject: [PATCH 11/11] Modified Booking/controllers --- controllers/Booking.js | 59 ++++++++++++++++++++++++++---------------- models/Booking.js | 7 ++++- models/Person.js | 34 ++++++++++++------------ 3 files changed, 60 insertions(+), 40 deletions(-) diff --git a/controllers/Booking.js b/controllers/Booking.js index 9c827dd..ed9ea09 100644 --- a/controllers/Booking.js +++ b/controllers/Booking.js @@ -1,4 +1,4 @@ -const { Booking, Package, User } = require('../models') +const { Booking, Package, User, Person } = require('../models') const { success, failure } = require('./helpers') exports.postBooking = (req, res) => { @@ -10,32 +10,45 @@ exports.postBooking = (req, res) => { User.findById(userid) .select() .then(user => { - const packagebooked = new Booking({ - package: id, - user: userid, - dateoftravel: req.body.dateoftravel, - person: { + const persondetails = new Person([ + { title: req.body.title, - first_name: user.first_name, - last_name: user.last_name, - }, - contactdetails: { - first_name: user.first_name, - last_name: user.last_name, - phone: user.phone, - email: user.email, - }, - billingaddress: { - address: req.body.address, - city: req.body.city, - postalcode: req.body.postalcode, - country: req.body.country, + first_name: req.body.first_name, + last_name: req.body.last_name, }, - }) - packagebooked.save().then(bookeddetails => { - res.send(success(bookeddetails)) + ]) + persondetails.save().then(details => { + const packagebooked = new Booking({ + package: id, + user: userid, + dateoftravel: req.body.dateoftravel, + person: [details._id], + contactdetails: { + first_name: user.first_name, + last_name: user.last_name, + phone: user.phone, + email: user.email, + }, + billingaddress: { + address: req.body.address, + city: req.body.city, + postalcode: req.body.postalcode, + country: req.body.country, + }, + }) + packagebooked.save().then(bookeddetails => { + Person.find({ _id: { $in: bookeddetails.person } }).then( + person => { + res.send({ + ...bookeddetails.toObject(), + person, + }) + }, + ) + }) }) }) + .catch(err => { res.send(failure(err)) }) diff --git a/models/Booking.js b/models/Booking.js index 798335a..010d23f 100644 --- a/models/Booking.js +++ b/models/Booking.js @@ -23,7 +23,12 @@ const BookingSchema = new Schema( required: true, }, - person: [personschema], + person: [ + { + type: Schema.ObjectId, + ref: personschema, + }, + ], contactdetails: { first_name: { diff --git a/models/Person.js b/models/Person.js index 54a38f4..e895149 100644 --- a/models/Person.js +++ b/models/Person.js @@ -1,21 +1,23 @@ const mongoose = require('mongoose') const { Schema } = mongoose -const PersonSchema = new Schema({ - title: { - type: String, - required: true, +const PersonSchema = new Schema([ + { + title: { + type: String, + required: true, + }, + first_name: { + type: String, + required: true, + minlength: 4, + }, + last_name: { + type: String, + minlength: 4, + trim: true, + }, }, - first_name: { - type: String, - required: true, - minlength: 4, - }, - last_name: { - type: String, - minlength: 4, - trim: true, - }, -}) +]) -module.exports = mongoose.model('Registration Details', PersonSchema) +module.exports = mongoose.model('Registration_Details', PersonSchema)