Skip to content

Commit

Permalink
feature(request):add multi-city request route
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-shagor committed Sep 2, 2019
2 parents 81c766e + 40e558f commit 4552842
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 126 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test": "NODE_ENV=test yarn undo:migration && NODE_ENV=test yarn migrate && NODE_ENV=test yarn undo:seed && NODE_ENV=test yarn seed && NODE_ENV=test nyc --reporter=html --reporter=text mocha --exit --require @babel/register --require @babel/polyfill src/test/index.js",
"test": "NODE_ENV=test yarn undo:migration && NODE_ENV=test yarn migrate && NODE_ENV=test yarn undo:seed && NODE_ENV=test yarn seed && NODE_ENV=test nyc --reporter=html --reporter=text mocha --exit --require @babel/register --require @babel/polyfill src/test/index.js --timeout 3000 --exit",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"dev-start": "set NODE_ENV=development && nodemon --exec babel-node src/index.js",
"build": "babel src --out-dir build",
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const dotenv = require('dotenv');
import dotenv from 'dotenv';

dotenv.config();
const databaseEnvDetails = {
Expand Down
22 changes: 6 additions & 16 deletions src/controllers/RequestController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import models from '../models';
import { HelperMethods } from '../utils';

const { Request, Destination } = models;
const { Request } = models;

/**
* Class representing the Request controller
Expand All @@ -21,14 +21,9 @@ class RequestController {
try {
const { id } = req.decoded;
const { body } = req;
const {
// eslint-disable-next-line camelcase
origin, returnDate, return_trip, destination, flightDate
} = body;
const { dataValues } = await Request.create({
origin, returnDate, return_trip, userId: id
...body, userId: id
});
await Destination.create({ destination, flightDate, requestId: dataValues.id });
if (dataValues.id) {
HelperMethods.requestSuccessful(res, {
success: true,
Expand Down Expand Up @@ -75,7 +70,7 @@ class RequestController {
}

/**
* Book a Trip
* Book a Multicity Trip
* Route: POST: /request/multicity
* @param {object} req - HTTP Request object
* @param {object} res - HTTP Response object
Expand All @@ -86,17 +81,12 @@ class RequestController {
try {
const { id } = req.decoded;
const { body } = req;
const {
// eslint-disable-next-line camelcase
origin, returnDate, return_trip, destination, flightDate
} = body;
const { destination, flightDate, origin } = body;
const { dataValues } = await Request.create({
origin, returnDate, return_trip, userId: id
});
await Destination.create({
origin,
userId: id,
multiDestination: destination,
multiflightDate: flightDate,
requestId: dataValues.id
});
if (dataValues.id) {
HelperMethods.requestSuccessful(res, {
Expand Down
16 changes: 14 additions & 2 deletions src/migrations/03-create-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ module.exports = {
type: Sequelize.STRING,
allowNull: false,
},
destination: {
type: Sequelize.STRING
},
flightDate: {
type: Sequelize.STRING
},
multiDestination: {
type: Sequelize.ARRAY(Sequelize.STRING),
},
multiflightDate: {
type: Sequelize.ARRAY(Sequelize.DATEONLY),
},
returnDate: {
type: Sequelize.DATEONLY,
},
Expand All @@ -31,9 +43,9 @@ module.exports = {
key: 'id'
}
},
return_trip: {
returnTrip: {
type: Sequelize.BOOLEAN,
allowNull: true,
defaultValue: false,
},
createdAt: {
allowNull: false,
Expand Down
38 changes: 0 additions & 38 deletions src/migrations/04-create-destination.js

This file was deleted.

51 changes: 0 additions & 51 deletions src/models/Destination.js

This file was deleted.

18 changes: 13 additions & 5 deletions src/models/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ export default (sequelize, DataTypes) => {
}
}
},
destination: {
type: DataTypes.STRING,
},
multiDestination: {
type: DataTypes.ARRAY(DataTypes.STRING),
},
flightDate: {
type: DataTypes.DATEONLY
},
multiflightDate: {
type: DataTypes.ARRAY(DataTypes.DATEONLY),
},

returnDate: {
type: DataTypes.DATEONLY
Expand All @@ -28,16 +40,12 @@ export default (sequelize, DataTypes) => {
values: ['BUSINESS', 'VACATION', 'EXPEDITION'],
defaultValue: 'BUSINESS'
},
return_trip: {
returnTrip: {
type: DataTypes.BOOLEAN,
},
});

Request.associate = models => {
Request.hasMany(models.Destination, {
foreignKey: 'requestId',
as: 'request_id',
});
Request.belongsTo(models.User, {
foreignKey: 'userId',
onDelete: 'CASCADE',
Expand Down
2 changes: 1 addition & 1 deletion src/routes/requestRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const requestRoutes = app => {
RequestController.bookAReturnTrip);

app.post('/api/v1/request/multicity',
Validate.validateMulticity,
Authorization.checkToken,
Validate.validateMulticity,
RequestController.bookMulticity);
};

Expand Down
8 changes: 4 additions & 4 deletions src/seeders/03-demo-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
returnDate: '2019-03-21',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '3821b930-ce48-4ac8-9ddf-ee3bf7980d08',
return_trip: true,
returnTrip: true,
reason: 'VACATION',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -18,7 +18,7 @@ module.exports = {
accommodationId: '9c41e609-7a30-4211-9d10-146a9c54ee74',
userId: '96dc6b6d-7a77-4322-8756-e22f181d952c',
reason: 'BUSINESS',
return_trip: true,
returnTrip: true,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -29,7 +29,7 @@ module.exports = {
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
reason: 'EXPEDITION',
return_trip: true,
returnTrip: true,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -40,7 +40,7 @@ module.exports = {
accommodationId: '35106536-deb5-4111-bd90-9ddfac5d348b',
userId: '4712fc7e-ca41-457f-872e-4a64b79efbba',
reason: 'BUSINESS',
return_trip: true,
returnTrip: true,
createdAt: new Date(),
updatedAt: new Date()
},
Expand Down
68 changes: 66 additions & 2 deletions src/test/integrationTests/requestController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('Integration tests for the request controller', () => {
flightDate: ['2019-06-27', '2019-06-25'],
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
return_trip: true,
returnTrip: true,
reason: 'EXPEDITION'
});
expect(response.status).to.equal(201);
Expand All @@ -119,7 +119,7 @@ describe('Integration tests for the request controller', () => {
.set('x-access-token', token).send({
returnDate: '2019-03-21',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
return_trip: true,
returnTrip: true,
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
reason: 'EXPEDITION',
});
Expand All @@ -128,4 +128,68 @@ describe('Integration tests for the request controller', () => {
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
});
it('should not book a trip when a required detail is missing', async () => {
const response = await chai.request(app).post('/api/v1/request/multicity')
.set('x-access-token', token).send({
destination: 'okoko',
flightDate: '2019-03-21',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
returnTrip: true,
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
reason: 'EXPEDITION',
});
expect(response.status).to.equal(400);
expect(response.body).to.have.property('message');
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
expect(response.body.message).to.equal('destination as to be more than one');
});
it('should not book a trip when a required detail is missing', async () => {
const response = await chai.request(app).post('/api/v1/request/multicity')
.set('x-access-token', token).send({
destination: ['mile12', 'okoko'],
flightDate: ['2019-06-27', '2019-06-25'],
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
returnTrip: true,
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
reason: 'EXPEDITION',
});
expect(response.status).to.equal(400);
expect(response.body).to.have.property('message');
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
expect(response.body.message).to.equal('The "origin" field is required');
});
it('should allow a registered user to book a multcity trip', async () => {
const response = await chai.request(app).post('/api/v1/request/multicity')
.set({ 'x-access-token': '' }).send({
origin: 'Onipan',
flightDate: ['2019-06-25', '2019-06-27'],
destination: ['mile12', 'okoko'],
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
returnTrip: true,
reason: 'EXPEDITION'
});
expect(response.status).to.equal(401);
expect(response.body).to.have.property('message');
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
});
it('should allow a registered user to book a multcity trip', async () => {
const response = await chai.request(app).post('/api/v1/request/multicity')
.set({ 'x-access-token': 'jksjjjjsjsj' }).send({
origin: 'Onipan',
flightDate: ['2019-06-25', '2019-06-27'],
destination: ['mile12', 'okoko'],
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
returnTrip: true,
reason: 'EXPEDITION'
});
expect(response.status).to.equal(401);
expect(response.body).to.have.property('message');
expect(response.body).to.have.property('success');
expect(response.body.success).to.equal(false);
});
});
12 changes: 7 additions & 5 deletions src/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { HelperMethods } from '../utils';

/**
* Trims input values from user
* @param {object} objectWithValuesToTrim - request body to trim
Expand Down Expand Up @@ -130,13 +132,13 @@ class Validate {
* @memberof Validate
*/
static validateMulticity(req, res, next) {
const { origin, destination, flightDate } = req.body;
if (!origin) return allFieldsRequired(res);
const { destination, flightDate } = req.body;
if (!destination || typeof (destination) !== 'object') {
return allFieldsRequired(res, 'destination as to be more than one');
return HelperMethods.clientError(res, 'destination as to be more than one');
}
if (!flightDate || typeof (destination) !== 'object') {
return allFieldsRequired(res, 'please input flightDate for all destinations');
if (!flightDate || typeof (flightDate) !== 'object') {
return HelperMethods.clientError(res,
'please input flightDate for all destinations');
}
next();
}
Expand Down

0 comments on commit 4552842

Please sign in to comment.