-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tripRequest): user can make a trip request
- create a controller for handling trip requests - create a validation rule to handle the trip request validation - add a file to verify jsonwebtoken - create trip request services to abstract models for the trip request controller [Delivers #167750059]
- Loading branch information
Showing
17 changed files
with
160 additions
and
41 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import UsersController from './users'; | ||
import ResetPasswordController from './resetPassword'; | ||
import TripRequestController from './trip-request'; | ||
|
||
|
||
export { UsersController, ResetPasswordController }; | ||
export { UsersController, ResetPasswordController, TripRequestController }; |
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,36 @@ | ||
import RequestService from '../services/requestService'; | ||
import TripService from '../services/tripService'; | ||
import { | ||
messages, status, successResponse, errorResponse | ||
} from '../utils/index'; | ||
/** | ||
* @class TripRequestController | ||
* @description Controllers for TripRequest | ||
* @exports TripRequestController | ||
*/ | ||
export default class TripRequestController { | ||
/** | ||
* @method tripRequest | ||
* @description Method for users to make a trip request | ||
* @param {object} req - The Request Object | ||
* @param {object} res - The Response Object | ||
* @returns {object} response body object | ||
*/ | ||
static async tripRequest(req, res) { | ||
try { | ||
const { accomodationId } = req.params; | ||
const { userId } = req.decoded; | ||
const { id: requestId } = await RequestService(userId, accomodationId); | ||
const { | ||
origin, destination, departureDate, returnDate, | ||
travelReasons, typeOfTrip | ||
} = req.body; | ||
const result = await TripService(origin, destination, departureDate, returnDate, | ||
travelReasons, requestId, typeOfTrip); | ||
const response = result.toJSON(); | ||
return successResponse(res, status.created, messages.tripRequests.success, response); | ||
} catch (error) { | ||
return errorResponse(res, status.error, messages.tripRequests.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default { | ||
up: queryInterface => queryInterface.bulkInsert( | ||
'Accomodations', | ||
[ | ||
{ | ||
name: '2-bedroom', | ||
address: 'kenya', | ||
image: 'https:yyys', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
], | ||
), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('Accomodations', null, {}) | ||
}; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,18 @@ | ||
|
||
import { hashPassword } from '../../utils'; | ||
export default { | ||
up: queryInterface => queryInterface.bulkInsert( | ||
'Users', | ||
[ | ||
{ | ||
firstName: '', | ||
lastName: '', | ||
email: '[email protected]', | ||
password: 'funmi', | ||
email: '[email protected]', | ||
password: hashPassword('funmi1234'), | ||
isVerified: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
email: '[email protected]', | ||
password: 'Anonymous', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
password: 'Unkown', | ||
email: '[email protected]', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
} | ||
], | ||
{} | ||
), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('Users', null, {}) | ||
|
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,32 @@ | ||
import Jwt from 'jsonwebtoken'; | ||
import { | ||
messages, status, errorResponse | ||
} from '../utils/index'; | ||
|
||
const Auth = { | ||
/** | ||
* @method verifyToken | ||
* @description Method for validating the user token | ||
* @param {object} req - The Request Object | ||
* @param {object} res - The Response Object | ||
* @returns {object} response body object | ||
*/ | ||
|
||
async verifyToken(req, res, next) { | ||
const token = req.headers.authorization; | ||
try { | ||
if (!token) { | ||
return errorResponse(res, status.bad, messages.authentication.auth); | ||
} | ||
const decoded = Jwt.verify(token, process.env.SECRET); | ||
req.userId = decoded; | ||
req.decoded = decoded; | ||
} catch (error) { | ||
return errorResponse(res, status.error, messages.authentication.error); | ||
} | ||
return next(); | ||
} | ||
}; | ||
|
||
|
||
export default Auth; |
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,11 @@ | ||
import { Router } from 'express'; | ||
import middlewares from '../../middlewares'; | ||
import TripRequestController from '../../controllers/trip-request'; | ||
import Auth from '../../middlewares/authentication'; | ||
|
||
const triprequestRoutes = new Router(); | ||
const { validate } = middlewares; | ||
|
||
triprequestRoutes.post('/:accomodationId', Auth.verifyToken, validate('TripRequest'), TripRequestController.tripRequest); | ||
|
||
export default triprequestRoutes; |
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,6 @@ | ||
import models from '../models'; | ||
|
||
// eslint-disable-next-line max-len | ||
const RequestService = (userId, accomodationId) => models.Requests.create({ userId, accomodationId }); | ||
|
||
export default RequestService; |
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 @@ | ||
import models from '../models'; | ||
|
||
// eslint-disable-next-line max-len | ||
const TripService = (origin, destination, departureDate, returnDate, travelReasons, requestId, typeOfTrip) => models.Trips.create({ | ||
origin, destination, departureDate, returnDate, travelReasons, requestId, typeOfTrip | ||
}); | ||
|
||
export default TripService; |
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 @@ | ||
|
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
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