Skip to content

Commit

Permalink
Merge pull request #42 from andela/ft-multi-city-request-167727316
Browse files Browse the repository at this point in the history
[#167727316] Feature Multi City Request
  • Loading branch information
Hector101 committed Sep 12, 2019
2 parents 00a45ef + 3e23520 commit 4349629
Show file tree
Hide file tree
Showing 24 changed files with 9,783 additions and 30 deletions.
9,075 changes: 9,075 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/controllers/accommodationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const getTripAccommodations = async (req, res) => {
export const getOneAccommodation = async (req, res) => {
try {
const { accommodationId } = req.params;
const accommodations = await accommodationServices.findOneAccommodation(parseInt(accommodationId, 10));
const accommodations = await accommodationServices.findOneAccommodation(Number(accommodationId));
if (!accommodations) {
return respondWithWarning(res, statusCode.resourceNotFound, 'resource not found');
}
Expand Down
82 changes: 78 additions & 4 deletions src/controllers/tripController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { postTrip, updateTripStatus, getRequesterEmail } from '../services/tripServices';
import {
postTrip, updateTripStatus, getRequesterEmail, bulkCreate,
getTripRequests, findOneTripRequest
} from '../services/tripServices';
import { respondWithSuccess, respondWithWarning } from '../helpers/responseHandler';
import statusCode from '../helpers/statusCode';
import { approvedEmitter } from '../helpers/notificationHandler';
Expand Down Expand Up @@ -29,6 +32,43 @@ export const oneWayTripRequest = async (req, res) => {
}
};

/**
* make multi-city trip request
* @param {object} req
* @param {object} res
* @returns {object} json response
*/
export const multiCityTripRequest = async (req, res) => {
let tripRequest;
const { id } = req.auth;
const { destination } = req.destination;
const {
origin, destinationId, reason, departureDate, type, subRequest
} = req.body;
try {
const payload = {
origin,
destinationId,
reason,
departureDate,
type,
status: 'pending',
userId: id
};
const multiCityTrip = await postTrip(payload);
const multiCityRequests = subRequest.map(sub => ({
tripId: multiCityTrip.id,
...sub
}));
const subRequestedTrips = await bulkCreate(multiCityRequests);
tripRequest = { ...multiCityTrip.toJSON(), destination, subRequestedTrips };

return respondWithSuccess(res, statusCode.created, 'request successfully sent', tripRequest);
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, 'Server Error');
}
};

export const approveTripRequest = async (req, res) => {
const status = 'approved';
try {
Expand Down Expand Up @@ -57,9 +97,6 @@ export const approveTripRequest = async (req, res) => {
}
};

export const getTripRequest = async (req, res) => (!req.trip
? respondWithWarning(res, statusCode.internalServerError, 'Oops something bad happened')
: respondWithSuccess(res, statusCode.success, 'Operation successful', req.trip));
/**
* A function to create a return trip
* @param {object} req
Expand Down Expand Up @@ -88,3 +125,40 @@ export const returnTripRequest = async (req, res) => {
return respondWithWarning(res, statusCode.internalServerError, 'Internal Server Error');
}
};

/**
* Function gets all tripRequests
* @param {object} req
* @param {object} res
* @returns {object} response object
*/
export const getAllTripRequests = async (req, res) => {
try {
const tripRequests = await getTripRequests();
if (!tripRequests) {
return respondWithWarning(res, statusCode.resourceNotFound, 'resource not found');
}
return respondWithSuccess(res, statusCode.success, 'resource successfully fetched', tripRequests);
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, 'Server Error');
}
};

/**
* Function gets one tripRequest
* @param {object} req
* @param {object} res
* @returns {object} response object
*/
export const getTripRequest = async (req, res) => {
try {
const { tripId } = req.params;
const tripRequests = await findOneTripRequest(Number(tripId));
if (!tripRequests) {
return respondWithWarning(res, statusCode.resourceNotFound, 'resource not found');
}
return respondWithSuccess(res, statusCode.success, 'resource successfully fetched', tripRequests.toJSON());
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, 'Server Error');
}
};
40 changes: 40 additions & 0 deletions src/database/migrations/20190909214411-create-sub-trip-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('SubTripRequests', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
tripId: {
type: Sequelize.INTEGER,
allowNull: false
},
subOrigin: {
type: Sequelize.STRING,
allowNull: false
},
subDestinationId: {
type: Sequelize.INTEGER,
allowNull: false
},
subDepartureDate: {
type: Sequelize.STRING,
allowNull: false
},
subReason: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
}
}),
down: (queryInterface) => queryInterface.dropTable('SubTripRequests')
};
26 changes: 26 additions & 0 deletions src/database/migrations/20190910223922-create-destination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('Destinations', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
destination: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
}
}),
down: (queryInterface) => queryInterface.dropTable('Destinations')
};
20 changes: 20 additions & 0 deletions src/database/seeders/20190910084329-create-subrequest-seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
up: async (queryInterface) => queryInterface.bulkInsert('SubTripRequests', [
{
subOrigin: 'Lagos',
subDestinationId: 6,
subDepartureDate: '2019-10-09T00:09:31.812Z',
subReason: 'Meet CEO',
tripId: 2
},
{
subOrigin: 'Kampala',
subDestinationId: 3,
subDepartureDate: '2019-11-09T00:09:31.812Z',
subReason: 'Meeting with top company clients',
tripId: 2
}
], {}),

down: queryInterface => queryInterface.bulkDelete('SubTripRequests', null, {})
};
24 changes: 24 additions & 0 deletions src/database/seeders/20190910225149-create-destination-seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default {
up: async (queryInterface) => queryInterface.bulkInsert('Destinations', [
{
destination: 'Lagos',
},
{
destination: 'Kampala',
},
{
destination: 'Cairo',
},
{
destination: 'Texas',
},
{
destination: 'Paris',
},
{
destination: 'Madrid',
}
], {}),

down: queryInterface => queryInterface.bulkDelete('Destinations', null, {})
};
144 changes: 144 additions & 0 deletions src/docs/swagger/definitions/trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,147 @@ export const returnTripSchema = {
},
}
};

export const createMultiCityTrip = {
type: 'object',
properties: {
type: {
type: 'string',
example: 'multi-city'
},
origin: {
type: 'string',
example: 'Nairobi'
},
destinationId: {
type: 'integer',
example: 2
},
reason: {
type: 'string',
example: 'Meet with clients'
},
departureDate: {
type: 'string',
format: 'date-time'
},
returnDate: {
type: 'string',
format: 'date-time',
},
subRequest: {
type: 'array',
items: {
type: 'object',
properties: {
subOrigin: {
type: 'string',
example: 'Johanesburg'
},
subDestinationId: {
type: 'integer',
example: 5
},
subDepartureDate: {
type: 'string',
format: 'date-time'
},
subReason: {
type: 'string',
example: 'Vacation'
},
}
}
}
}
};

export const multiCityTripRes = {
type: 'object',
properties: {
id: {
type: 'integer',
example: 3
},
userId: {
type: 'integer',
example: 2
},
type: {
type: 'string',
example: 'multi-city'
},
origin: {
type: 'string',
example: 'Nairobi'
},
destinationId: {
type: 'integer',
example: 2
},
reason: {
type: 'string',
example: 'Meet with clients'
},
status: {
type: 'string',
example: 'pending'
},
departureDate: {
type: 'string',
format: 'date-time'
},
returnDate: {
type: 'string',
example: null,
},
destination: {
type: 'string',
example: 'Kampala',
},
createdAt: {
type: 'string',
format: 'date-time'
},
updatedAt: {
type: 'string',
format: 'date-time'
},
subRequestedTrips: {
type: 'array',
items: {
type: 'object',
properties: {
id: {
type: 'integer',
example: 6
},
subOrigin: {
type: 'string',
example: 'Johanesburg'
},
subDestinationId: {
type: 'integer',
example: 4
},
subDepartureDate: {
type: 'string',
format: 'date-time'
},
subReason: {
type: 'string',
example: 'Vacation'
},
createdAt: {
type: 'string',
format: 'date-time'
},
updatedAt: {
type: 'string',
format: 'date-time'
},
}
}
}
}
};
Loading

0 comments on commit 4349629

Please sign in to comment.