Skip to content

Commit

Permalink
bg(facility):add facility_id column to bookings table
Browse files Browse the repository at this point in the history
This bugs adds the facility_id column to the booking table
which was hitherto missing. And then, it reverts the functionality
of giving ratings/feedbacks to the checked_in column on the bookings
table. It was formerly a stand-alone functionality.

[Finishes #168477780]
  • Loading branch information
oosahon authored and parkerthegeniuschild committed Sep 13, 2019
1 parent cc36ae9 commit 21a56c8
Show file tree
Hide file tree
Showing 36 changed files with 2,527 additions and 2,570 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Make company global travel and accommodation easy and convenient",
"main": "index.js",
"scripts": {
"clear:log": "rm ./src/logs/app.log && touch ./src/logs/app.log",
"clean": "rm -rf dist && mkdir dist",
"clear:logs": "rm ./src/logs/app.log && touch ./src/logs/app.log",
"clean": "rm -rf dist coverage && mkdir dist coverage",
"build": "babel src -d dist --copy-files",
"start:dev": "NODE_ENV=development nodemon --exec babel-node src/index.js",
"start:staging": "NODE_ENV=staging node dist/index.js",
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/facilities.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default class FacilitiesController {
}

/**
* Books a room in an accomodation facility
* Books a room in an accommodation facility
* @param {object} req
* @param {object} res
* @param {object} next
Expand All @@ -151,19 +151,19 @@ export default class FacilitiesController {
const room = await Room.findByPk(room_id, { attributes: [] });
if (!room) return Response.NotFoundError(res, 'Room not found');
// Check if an existing booking conflicts with the one to be made and return a 409
const { departure_date, return_date } = req.body;
const { departure_date, return_date, facility_id } = req.body;
const conflictingBooking = await Booking.findOne(FacilityUtils
.getConflictingBookingQuery(room_id, departure_date, return_date));

if (conflictingBooking) {
return Response.ConflictError(res, {
message: `This accomodation is already book from ${conflictingBooking.departure_date} to ${
message: `This accommodation is already book from ${conflictingBooking.departure_date} to ${
conflictingBooking.return_date}`
});
}
// If all checks out so far, create a new booking and return its details
const newBooking = await Booking.create({
room_id, user_id, departure_date, return_date
room_id, user_id, departure_date, return_date, facility_id
}, { returning: true });

return Response.Success(res, newBooking.dataValues, 201);
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/rating_feedback.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import models from '../db/models';
import Response from '../utils/response.utils';

const {
Feedback, Rating, Facility, Check_in
Feedback, Rating, Facility, Booking
} = models;

/**
Expand Down Expand Up @@ -42,7 +42,9 @@ export default class RatingAndFeedbackController {
}

// if facility exists, check if user has checked in
const hasCheckedIn = await Check_in.findAll({ where: { user_id, facility_id } });
const hasCheckedIn = await Booking.findAll({
where: { user_id, facility_id, checked_in: true }
});

// if user hasn't checked in, return error message
if (!hasCheckedIn.length) {
Expand Down
12 changes: 5 additions & 7 deletions src/controllers/request.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// import sequelize from 'sequelize';

import models from '../db/models';
import sender from '../services/email.service';
import Response from '../utils/response.utils';
Expand Down Expand Up @@ -73,19 +71,19 @@ export default class RequestController {
try {
const { id: user_id } = req.currentUser.dataValues;
const {
category, origin, destination, departure_date, return_date, reason, room_id
category, origin, destination, departure_date, return_date, reason, room_id, facility_id
} = req.body;
const bookingData = {
departure_date, return_date, user_id, room_id
departure_date, return_date, user_id, room_id, facility_id
};
const booking = await models.Booking.create(bookingData);
const booking = await Booking.create(bookingData);
const { id: booking_id } = booking;
const requestData = {
user_id, category, origin, destination: destination.split(', '), departure_date, return_date, reason, booking_id
};

requestData.destination = requestData.destination.map((el) => el.toLowerCase());
const request = await models.Request.create(requestData);
const request = await Request.create(requestData);
return res.status(201).json({
status: 'success',
data: { request, booking }
Expand All @@ -105,7 +103,7 @@ export default class RequestController {
* @param {object} req
* @param {object} res
*
* @returns {objet} status and message
* @returns {object} status and message
*/
static async createReturnTripRequest(req, res) {
try {
Expand Down
10 changes: 9 additions & 1 deletion src/db/migrations/20190821153031-create-booking.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ module.exports = {
},
onDelete: 'CASCADE'
},
facility_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'facilities',
key: 'id'
},
onDelete: 'CASCADE'
},
departure_date: {
type: Sequelize.DATEONLY,
allowNull: false
},
return_date: {
type: Sequelize.DATEONLY,
allowNull: false
},
checked_in: {
type: Sequelize.BOOLEAN,
Expand Down
43 changes: 0 additions & 43 deletions src/db/migrations/20190909225417-create-check_in.js

This file was deleted.

5 changes: 4 additions & 1 deletion src/db/models/booking.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = (sequelize, DataTypes) => {
},
return_date: {
type: DataTypes.DATEONLY,
allowNull: false
},
checked_in: {
type: DataTypes.BOOLEAN,
Expand All @@ -25,6 +24,10 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'room_id',
onDelete: 'CASCADE'
});
Booking.belongsTo(models.Facility, {
foreignKey: 'facility_id',
onDelete: 'CASCADE'
});
};
return Booking;
};
23 changes: 0 additions & 23 deletions src/db/models/check_in.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/db/seeders/20190828065846-bookings-seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,39 @@ module.exports = {
up: (queryInterface) => queryInterface.bulkInsert('bookings', [{
user_id: 1,
room_id: 1,
facility_id: 1,
departure_date: new Date(),
return_date: new Date(new Date().getTime() + 14 * 24 * 3600000),
checked_in: false,
created_at: new Date(),
updated_at: new Date()
},
{
user_id: 7,
room_id: 1,
facility_id: 1,
departure_date: new Date(),
return_date: new Date(new Date().getTime() + 14 * 24 * 3600000),
checked_in: true,
created_at: new Date(),
updated_at: new Date()
},
{
user_id: 7,
room_id: 2,
facility_id: 2,
checked_in: true,
departure_date: new Date(),
created_at: new Date(),
updated_at: new Date()
},
{
user_id: 2,
room_id: 2,
facility_id: 1,
departure_date: new Date(new Date().getTime() + 7 * 24 * 3600000),
return_date: new Date(new Date().getTime() + 24 * 24 * 3600000),
checked_in: false,
created_at: new Date(),
updated_at: new Date()
}], {}),
Expand Down
1 change: 0 additions & 1 deletion src/db/seeders/20190828065852-comments-seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
origin: 'Paris',
destination: ['lagos'],
departure_date: new Date(),
// return_date: new Date(),
reason: 'I am tired of this office',
booking_id: 1,
created_at: new Date(),
Expand Down
18 changes: 0 additions & 18 deletions src/db/seeders/20190909231921-check_ins-seeder.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/middlewares/facilities.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const FacilitiesChecks = {
if ((new Date(departure_date) - new Date(today)) < 0) {
errors.departureDateError = 'Departure date cannot be lesser than today';
}
// If any error has occured return a bad request error, else proceed to the controller
// If any error has occurred return a bad request error, else proceed to the controller
if (Object.keys(errors).length) return Response.BadRequestError(res, errors);

next();
Expand Down
2 changes: 1 addition & 1 deletion src/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('App.js', () => {
done();
});
});

it('Should return a 404 error if page is not found', (done) => {
chai.request(app)
.get('/not/an/endpoint')
Expand Down
10 changes: 5 additions & 5 deletions src/test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ describe('/Auth Middlewares', () => {
done();
});

it("fakes server error on verifyManager", done => {
it('fakes server error on verifyManager', (done) => {
const req = { body: {} };
const res = {
status() {},
send() {}
};
sinon.stub(res, "status").returnsThis();
sinon.stub(res, 'status').returnsThis();
Auth.verifyManager(req, res);
res.status.should.have.callCount(1);
done();
});

it("fakes server error on verifyTravelAdmin", done => {
it('fakes server error on verifyTravelAdmin', (done) => {
const req = { body: {} };
const res = {
status() { },
send() { }
};
sinon.stub(res, "status").returnsThis();
sinon.stub(res, 'status').returnsThis();
Auth.verifyTravelAdmin(req, res);
res.status.should.have.callCount(1);
done();
});
});
});
Loading

0 comments on commit 21a56c8

Please sign in to comment.