-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fca3377
commit 3ff4701
Showing
19 changed files
with
359 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { postFlightDetails } from '../services/flightServices'; | ||
import statusCode from '../helpers/statusCode'; | ||
import { respondWithSuccess, respondWithWarning } from '../helpers/responseHandler'; | ||
|
||
export const addFlightDetails = async (req, res) => { | ||
const { | ||
departureDate, userId, returnDate, origin, destinationId | ||
} = req.trip; | ||
if (!returnDate) { | ||
if (!req.body.returnDate) { return respondWithWarning(res, statusCode.badRequest, 'return date is required'); } | ||
const data = { | ||
...req.body, departureDate, userId, origin, destinationId | ||
}; | ||
|
||
try { | ||
const flightDetails = await postFlightDetails(data); | ||
return respondWithSuccess(res, statusCode.created, 'flight details created successful', flightDetails.dataValues); | ||
} catch (error) { | ||
return respondWithWarning(res, statusCode.internalServerError, 'internal server error', error); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Flights', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
departureDate: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false | ||
}, | ||
returnDate: { | ||
type: Sequelize.DATE | ||
}, | ||
airline: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
ticketNumber: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
origin: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
destinationId: { | ||
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, Sequelize) => queryInterface.dropTable('Flights') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const FlightCreate = { | ||
type: 'object', | ||
properties: { | ||
airline: { | ||
type: 'string', | ||
example: 'British Airways' | ||
}, | ||
ticketNumber: { | ||
type: 'string', | ||
example: 'BRAW16829033', | ||
}, | ||
departureDate: { | ||
type: 'date', | ||
example: '08-08-2019', | ||
}, | ||
returnDate: { | ||
type: 'date', | ||
example: '09-09-2019', | ||
}, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#### What this PR Does | ||
This PR creates an endpoint for user to add their travel flight ticket | ||
|
||
#### how it works | ||
- clone this repo and cd into the folder | ||
- run `npm install` in the terminal to install all packages | ||
- run | ||
#### Relevant Pivotal Tracker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export const flightPath = { | ||
post: { | ||
tags: [ | ||
'flights' | ||
], | ||
security: [ | ||
{ | ||
BearerToken: [] | ||
} | ||
], | ||
summary: 'User can enter flight details', | ||
description: 'user can enter flight details after trip request has been approved ', | ||
parameters: [ | ||
{ | ||
name: 'tripId', | ||
in: 'path', | ||
description: 'tripId', | ||
required: true, | ||
}, | ||
{ | ||
name: 'body', | ||
in: 'body', | ||
description: 'Flight details object', | ||
required: true, | ||
schema: { | ||
$ref: '#/definitions/FlightCreate' | ||
} | ||
} | ||
], | ||
responses: { | ||
201: { | ||
description: 'request successfully sent', | ||
schema: { | ||
$ref: '#/definitions/created' | ||
} | ||
}, | ||
400: { | ||
description: 'Invalid request details', | ||
schema: { | ||
$ref: '#/definitions/badRequest' | ||
} | ||
}, | ||
401: { | ||
description: 'Unauthorized', | ||
schema: { | ||
$ref: '#/definitions/notAuthorized' | ||
} | ||
}, | ||
422: { | ||
description: 'Unprocessable entity', | ||
schema: { | ||
$ref: '#/definitions/accessForbidden' | ||
} | ||
}, | ||
500: { | ||
description: 'Server error', | ||
schema: { | ||
$ref: '#/definitions/serverError' | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import BaseJoi from '@hapi/joi'; | ||
import Extension from '@hapi/joi-date'; | ||
import statusCode from '../helpers/statusCode'; | ||
import { respondWithWarning } from '../helpers/responseHandler'; | ||
import { joiValidator } from '../helpers/joiValidator'; | ||
|
||
const Joi = BaseJoi.extend(Extension); | ||
|
||
export const flightValidator = (req, res, next) => { | ||
const flightSchema = { | ||
airline: Joi.string().required(), | ||
ticketNumber: Joi.string().required(), | ||
returnDate: Joi.date().format('DD-MM-YYYY') | ||
}; | ||
const errors = joiValidator(req.body, flightSchema); | ||
if (!errors) return next(); | ||
return respondWithWarning(res, statusCode.badRequest, 'Bad request', errors); | ||
}; | ||
|
||
|
||
export const checkTripStatus = async (req, res, next) => { | ||
const { status } = req.trip; | ||
if (status === 'approved') { return next(); } | ||
return respondWithWarning(res, statusCode.unprocessableEntity, 'Not allowed'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
export default (sequelize, DataTypes) => { | ||
const Flight = sequelize.define('Flight', { | ||
userId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false | ||
}, | ||
departureDate: { | ||
type: DataTypes.DATE, | ||
allowNull: false | ||
}, | ||
returnDate: { | ||
type: DataTypes.DATE | ||
}, | ||
origin: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
destinationId: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
airline: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
ticketNumber: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
}, {}); | ||
Flight.associate = (models) => { | ||
Flight.hasMany(models.TripRequest, { foreignKey: 'returnDate' }); | ||
Flight.hasMany(models.TripRequest, { foreignKey: 'departureDate' }); | ||
}; | ||
return Flight; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Router } from 'express'; | ||
import { addFlightDetails } from '../../controllers/flightController'; | ||
import { authenticateUserToken } from '../../middlewares/authentication'; | ||
import { validateTripId } from '../../middlewares/validateTripRequest'; | ||
import { verifyTrip } from '../../middlewares/tripMiddleware'; | ||
import { flightValidator, checkTripStatus } from '../../middlewares/flightMiddleware'; | ||
|
||
|
||
const flight = Router(); | ||
|
||
flight.post('/add/:tripId', authenticateUserToken, validateTripId, flightValidator, verifyTrip, checkTripStatus, addFlightDetails); | ||
|
||
export default flight; |
Oops, something went wrong.