Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Booking #10

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
75 changes: 75 additions & 0 deletions controllers/Booking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { Booking, Package, User, Person } = require('../models')
const { success, failure } = require('./helpers')

exports.postBooking = (req, res) => {
const id = req.body.package
Package.findById(id)
.select()
.then(package => {
const userid = req.session.userId
User.findById(userid)
.select()
.then(user => {
const persondetails = new Person([
{
title: req.body.title,
first_name: req.body.first_name,
last_name: req.body.last_name,
},
])
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))
})
})
}

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,
})
})
})
}
5 changes: 3 additions & 2 deletions controllers/Package.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ exports.getAllPackages = (req, res) => {
name: 1,
description: 1,
images: 1,
slug: 1,
})
.then(allpackages => {
res.send(allpackages)
Expand All @@ -78,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,
Expand All @@ -91,7 +92,7 @@ exports.postPackage = ({ body }, res) => {
packagedetails
.save()
.then(() => {
res.send(success(packagedetials))
res.send(success(packagedetails))
})
.catch(err => {
res.send(failure(err))
Expand Down
91 changes: 91 additions & 0 deletions models/Booking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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(
{
package: [
{
type: Schema.ObjectId,
ref: package,
},
],

user: {
type: Schema.ObjectId,
ref: user,
},

dateoftravel: {
type: Date,
required: true,
},

person: [
{
type: Schema.ObjectId,
ref: personschema,
},
],

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 },
)
module.exports = mongoose.model('booking', BookingSchema)
23 changes: 23 additions & 0 deletions models/Person.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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)
2 changes: 2 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ module.exports = {
Package: require('./Package'),
Itenary: require('./Itenary'),
Category: require('./Category'),
Booking: require('./Booking'),
Person: require('./Person'),
}
10 changes: 10 additions & 0 deletions routes/Booking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { Router } = require('express')
const controller = require('../controllers/Booking')
const middlewares = require('./middlewares')
const router = Router()

router.post('/', middlewares.Loggedin, controller.postBooking)

router.get('/:viewbooking', controller.viewbooking)

module.exports = router
3 changes: 3 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -30,4 +31,6 @@ router.use('/itenary', ItenaryRouter)

router.use('/search', SearchRouter)

router.use('/book', BookingRouter)

module.exports = router
15 changes: 15 additions & 0 deletions routes/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down