Skip to content

Commit

Permalink
feat(tripRequest): user can make a trip request
Browse files Browse the repository at this point in the history
- 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
feobaby committed Sep 10, 2019
1 parent 3789722 commit 0a06393
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/controllers/index.js
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 };
36 changes: 36 additions & 0 deletions src/controllers/trip-request.js
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);
}
}
}
4 changes: 4 additions & 0 deletions src/database/migrations/create-8-trips.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export default {
type: Sequelize.STRING,
allowNull: false
},
typeOfTrip: {
type: Sequelize.ENUM('One Way', 'Return', 'Multi-City'),
allowNull: false
},
requestId: {
type: Sequelize.DataTypes.UUID,
allowNull: false,
Expand Down
16 changes: 16 additions & 0 deletions src/database/seeders/acc.js
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, {})
};
21 changes: 0 additions & 21 deletions src/database/seeders/profile.js

This file was deleted.

20 changes: 4 additions & 16 deletions src/database/seeders/user.js
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, {})
Expand Down
32 changes: 32 additions & 0 deletions src/middlewares/authentication.js
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;
16 changes: 15 additions & 1 deletion src/models/Trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ export default (sequelize, DataTypes) => {
travelReasons: {
type: DataTypes.STRING,
allowNull: false
}
},
typeOfTrip: {
type: DataTypes.ENUM('One Way', 'Return', 'Multi-City'),
allowNull: false
},
requestId: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: 'Requests',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
},
}, {});
return Trip;
};
2 changes: 2 additions & 0 deletions src/routes/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import express from 'express';
import userRoute from './users';
import authRoutes from './auth';
import resetPasswordRoute from './resetPassword';
import triprequestRoutes from './trip-request';

const router = express.Router();

router.use('/auth', authRoutes);
router.use('/users', userRoute);
router.use('/', resetPasswordRoute);
router.use('/triprequest', triprequestRoutes);
export default router;
11 changes: 11 additions & 0 deletions src/routes/api/trip-request.js
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;
6 changes: 6 additions & 0 deletions src/services/requestService.js
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;
8 changes: 8 additions & 0 deletions src/services/tripService.js
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;
1 change: 1 addition & 0 deletions src/test/controllers/TripRequest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ import './utils/responses.test';
import './utils/jwt.test';
import './utils/emailTemplatesFunction.test';
import './services/autoMailer.test';
import './controllers/TripRequest.test';
8 changes: 8 additions & 0 deletions src/utils/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ const messages = {
error: 'Could not sign in user try again',
invalid: 'Invalid Credentials',
unverified: 'Email not verified, check your mail to verify'
},
tripRequests: {
success: 'The trip request has been created!',
error: 'Oops, there was an error trying to create a trip request!'
},
authentication: {
error: 'Oops, something went wrong...',
auth: 'Sorry, you are not authorized to access this resource.'
}
};

Expand Down
6 changes: 4 additions & 2 deletions src/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {
userRegister, userLogin,
forgotPassword, resetPassword
forgotPassword, resetPassword,
TripRequest
} from './validators/rules';

const getValidator = (validationName) => {
const rules = {
userRegister,
userLogin,
forgotPassword,
resetPassword
resetPassword,
TripRequest
};

return rules[validationName];
Expand Down
10 changes: 10 additions & 0 deletions src/validation/validators/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ export const resetPassword = [
return true;
})
];


export const TripRequest = [
check('origin', 'please provide the origin..').not().isEmpty(),
check('destination', 'please provide the destination..').not().isEmpty(),
check('returnDate', 'please select the return date..').not().isEmpty(),
check('departureDate', 'please select the departure date..').not().isEmpty(),
check('travelReasons', 'please provide the travel reason.').not().isEmpty(),
check('typeOfTrip', 'please select the type of trip..').not().isEmpty()
];

0 comments on commit 0a06393

Please sign in to comment.