Skip to content

Commit

Permalink
feat(avail request): approve pending requests
Browse files Browse the repository at this point in the history
* PATCH /trips/approve

[Starts #167891588]
  • Loading branch information
meetKazuki authored and daniellamarr committed Sep 10, 2019
1 parent 9e89d2b commit 22b6e94
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 6 deletions.
140 changes: 140 additions & 0 deletions src/controllers/Trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,146 @@ class TripController {
}
}

/**
* Get pending requests
* Route: GET: /trips
* @param {object} req - HTTP Request object
* @param {object} res - HTTP Response object
* @return {res} res - HTTP Response object
* @memberof TripsController
*/
static async getPendingRequests(req, res) {
try {
const { type } = req.query;
const { payload: { companyId } } = req.payload;
let trips;
switch (type) {
case 'pending':
trips = await Trip.findAll({
where: { status: 'pending' },
include: [{
model: User,
as: 'user',
attributes: {
exclude: [
'id',
'firstName',
'lastName',
'dob',
'gender',
'email',
'password',
'role',
'status',
'companyId',
'favorites',
'createdAt',
'updatedAt'
]
},
where: { companyId }
}],
});
break;
case 'approved':
trips = await Trip.findAll({
where: { status: 'approved' },
include: [{
model: User,
as: 'user',
attributes: {
exclude: [
'id',
'firstName',
'lastName',
'dob',
'gender',
'email',
'password',
'role',
'status',
'companyId',
'favorites',
'createdAt',
'updatedAt'
]
},
where: { companyId }
}],
});
break;
case 'rejected':
trips = await Trip.findAll({
where: { status: 'approved' },
include: [{
model: User,
as: 'user',
attributes: {
exclude: [
'id',
'firstName',
'lastName',
'dob',
'gender',
'email',
'password',
'role',
'status',
'companyId',
'favorites',
'createdAt',
'updatedAt'
]
},
where: { companyId }
}],
});
break;
default:
trips = await Trip.findAll({
include: [{
model: User,
as: 'user',
attributes: {
exclude: [
'id',
'firstName',
'lastName',
'dob',
'gender',
'email',
'password',
'role',
'status',
'companyId',
'favorites',
'createdAt',
'updatedAt'
]
},
where: { companyId }
}],
});
}
if (!trips.length) {
const response = new Response(
false, 404, 'No pending requests'
);
return res.status(response.code).json(response);
}

const response = new Response(
true, 200, 'Requests retrieved', trips
);
return res.status(response.code).json(response);
} catch (error) {
const response = new Response(
false, 500, 'Server error, Please try again later'
);
return res.status(response.code).json(response);
}
}

/**
* @description - this method update the status of a travel request
* @param {object} req - the request sent to the router
Expand Down
26 changes: 25 additions & 1 deletion src/database/seeders/20190821112700-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ module.exports = {
status: 'active',
role: 'super admin'
},
{
id: '91542e6f-94bc-4e80-a667-586fb3752f26',
companyId: 'a6e35eb9-8c59-5c7d-b8d4-ae724aa7fb62',
firstName: 'Emperor',
lastName: 'Ghadaffi',
email: 'ghaddafi@gmail.com',
password: hashHelper.hashPassword('password2019'),
gender: 'male',
dob: '2012-09-10',
status: 'active',
role: 'manager'
},
{
id: '91542e6f-94bc-4e80-a667-586fb3752f65',
companyId: 'a6e35eb9-8c59-4c7d-b8d4-ae724aa7fb61',
Expand All @@ -85,7 +97,19 @@ module.exports = {
dob: '2012-09-10',
status: 'unverified',
role: 'staff'
}
},
{
id: 'ffe25dbe-29ea-4759-8464-ed116f6739dd',
companyId: 'a6e35eb9-8c59-4c7d-b8d4-ae724aa7fb61',
firstName: 'Robert',
lastName: 'Mugabe',
email: 'thaRealMugabe@gmail.com',
password: hashHelper.hashPassword('password2019'),
gender: 'male',
dob: '2012-09-12',
status: 'active',
role: 'manager'
},
]),
down: queryInterface => queryInterface.bulkDelete('Users', null, {})
};
10 changes: 10 additions & 0 deletions src/database/seeders/20190829125546-create-trips.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ module.exports = {
reason: 'for holiday',
status: 'pending'
},
{
id: 'ffe25dbe-29ea-4759-8468-ed116f6739df',
type: 'multiple',
startBranchId: 'ffe35dbe-29ea-4759-8461-ed116f6739dd',
userId: '91542e6f-94bc-4e80-a667-586fb0752f23',
tripDate: '2019-02-17',
returnDate: '2019-02-23',
reason: 'for holiday',
status: 'pending'
},
{
id: 'ffe25dbe-29ea-4759-8462-ed116f6739df',
type: 'return',
Expand Down
11 changes: 10 additions & 1 deletion src/routes/trip.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import validator from '../middlewares/validator';
import permit from '../middlewares/permission';
import verify from '../middlewares/AuthMiddlewares';
import {
tripRequestSchema, tripRequestStatusSchema, editTripRequestSchema
tripRequestSchema,
tripRequestStatusSchema,
editTripRequestSchema
} from '../validation/tripSchema';

const tripRoutes = Router();

tripRoutes.get('/user', Token.verifyToken, TripController.getUserTrips);

tripRoutes.get(
'/',
Token.verifyToken,
permit('manager'),
TripController.getPendingRequests
);

tripRoutes.post(
'/',
Token.verifyToken,
Expand Down
6 changes: 4 additions & 2 deletions src/validation/tripSchema.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { body, check, param } from 'express-validator';
import validateUUID from 'uuid-validate';
import {
isValid, parseISO, isFuture
} from 'date-fns';
import validateUUID from 'uuid-validate';
import models from '../database/models';

const { Branch, Accomodation, Stop } = models;
Expand Down Expand Up @@ -215,4 +215,6 @@ const editTripRequestSchema = [
}),
];

export { tripRequestStatusSchema, tripRequestSchema, editTripRequestSchema };
export {
tripRequestStatusSchema, tripRequestSchema, editTripRequestSchema
};
23 changes: 22 additions & 1 deletion test/mockData/mockAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,30 @@ const credentialsForServerError2 = {
companyCode: '4RHJHJJKSK'
};

const manager = {
email: 'thaRealMugabe@gmail.com',
password: 'password2019',
code: '4RHJHJJKSK'
};

const unverifiedLogin = {
email: 'john.doe@gmail.com',
password: 'password2019',
code: '4RHJHJJKSK'
};

const nonManager = {
email: 'tjhakeemus1@gmail.com',
password: '12345678',
code: '4RHJHJJKSK'
};

const nonManage = {
email: 'ghaddafi@gmail.com',
password: 'password2019',
code: 'BAREFOOT'
};

const users = {
user1,
user2,
Expand Down Expand Up @@ -411,7 +429,10 @@ const users = {
credentialsForServerError2,
staffAuth,
adminAuth,
unverifiedLogin
unverifiedLogin,
manager,
nonManager,
nonManage
};

export default users;
Loading

0 comments on commit 22b6e94

Please sign in to comment.