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 Aug 30, 2019
2 parents 81c766e + 40e558f commit 8221936
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
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
83 changes: 83 additions & 0 deletions src/test/integrationTests/requestController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,87 @@ 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',
return_trip: 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 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', token).send({
origin: 'Onipan',
flightDate: '2019-06-25',
destination: ['mile12', 'okoko'],
flightDate: '2019-06-27',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
return_trip: true,
reason: 'EXPEDITION'
});
expect(response.status).to.equal(500);
expect(response.body).to.have.property('message');
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: ['mile12', 'okoko'],
flightDate: ['2019-06-27', '2019-06-25'],
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
return_trip: 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',
destination: ['mile12', 'okoko'],
flightDate: '2019-06-27',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
return_trip: 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',
destination: ['mile12', 'okoko'],
flightDate: '2019-06-27',
accommodationId: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '79ddfd3b-5c83-4beb-815e-55b1c95230e1',
return_trip: 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);
});
});
20 changes: 16 additions & 4 deletions src/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ const allFieldsRequired = (res, message) => {
});
};

/**
* Defines the failed message returned when required fields are missing.
* @param {object} res - Response object
* @param {string} message - specific error message
* @returns {res} - Response object
*/
const errorMsg = (res, message) => {
res.status(400).send({
success: false,
message,
});
};

/**
* Defines the failed message returned when required fields are missing.
* @param {object} requestBody - HTTP request object
Expand Down Expand Up @@ -130,13 +143,12 @@ 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 errorMsg(res, 'destination as to be more than one');
}
if (!flightDate || typeof (destination) !== 'object') {
return allFieldsRequired(res, 'please input flightDate for all destinations');
return errorMsg(res, 'please input flightDate for all destinations');
}
next();
}
Expand Down

0 comments on commit 8221936

Please sign in to comment.