Skip to content

Commit

Permalink
ft(manager-view-single-request): A manager can view a single request
Browse files Browse the repository at this point in the history
- create a middleware to check the role and pass the requesterId to controller

- create tests to cover the new implementation

[Finishes #169779349]
  • Loading branch information
nignanthomas committed Nov 19, 2019
1 parent b30731d commit 3f64a16
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 197 deletions.
15 changes: 0 additions & 15 deletions .vscode/settings.json

This file was deleted.

85 changes: 49 additions & 36 deletions src/controllers/requestController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import text from '../utils/strings';
import responseHelper from '../utils/responseHelper';
import responseUtil from '../utils/responseUtil';
import userServices from '../services/userServices';
import { createRequest, findOne } from '../services/requestServices/requestServices';
import {
createRequest,
findOne
} from '../services/requestServices/requestServices';
import destinationController from './destinationController';
import searchRequestsServices from '../services/searchRequestsServices';
import Utilities from '../utils/index';
Expand All @@ -18,14 +21,12 @@ import requestHelper from '../helpers/requestHelper';
import notifSender from '../helpers/notifSender';
import dateValidator from '../helpers/datesValidator';


const { Op } = Sequelize;
const { SUCCESSFULLY_RETRIEVED_REQUESTS } = strings.requests;
const { NO_REQUESTS, ASSIGNED_REQUESTS } = text.user.requests;
const { allSearch } = searchRequestsServices;

export default class requestController {

static async viewManagerRequests(req, res) {
const { id: lineManager } = req.user.payload;

Expand All @@ -43,21 +44,30 @@ export default class requestController {
{ model: models.locations, as: 'origin', attributes: ['id', 'name', 'country'] },
{ model: models.destinations, attributes: ['id', 'arrivalDate', 'departureDate', 'reasons'], include: [{ model: models.locations, as: 'location', attributes: ['name'] }] }
]
},
}
]
});
return responseUtil(res, 200, (!users.length)
? NO_REQUESTS
: ASSIGNED_REQUESTS, users);
return responseUtil(
res,
200,
!users.length ? NO_REQUESTS : ASSIGNED_REQUESTS,
users
);
}

static async viewMyRequests({ user }, res) {
const query = Utilities.userQueries.userRequests(user.payload);
const { requests } = await userServices.findOne(query, Utilities.queryScopes.responseScope);
const { requests } = await userServices.findOne(
query,
Utilities.queryScopes.responseScope
);

return responseUtil(res, 200, (!requests.length)
? NO_REQUESTS
: SUCCESSFULLY_RETRIEVED_REQUESTS, requests);
return responseUtil(
res,
200,
!requests.length ? NO_REQUESTS : SUCCESSFULLY_RETRIEVED_REQUESTS,
requests
);
}

static async changeStatus(req, res) {
Expand All @@ -79,7 +89,7 @@ export default class requestController {
if (requestToProcess) {
let request = await models.requests.update(
{ statusId },
{ where: { id }, returning: true, }
{ where: { id }, returning: true }
);

request = request[1][0].dataValues;
Expand All @@ -90,37 +100,33 @@ export default class requestController {
return responseHelper(res, strings.requests.NOT_FOUND, null, 404);
}

static async updateRequest(req, res) {
static async updateRequest(req, res) {
const { id } = req.request;

try {
await models.requests.update(req.body, {
where: { id }, returning: true, raw: true,
where: { id },
returning: true,
raw: true
});

const destination = req.body.destinations;
const { lineManager } = req.user.payload;
const APP_URL_BACKEND = `${req.protocol}://${req.headers.host}`;

destination.forEach(async element => {
destination.forEach(async element => {
await models.destinations.update(element, {
where: { [Op.and]: [{ requestId: id }, { id: element.id }] },
where: { [Op.and]: [{ requestId: id }, { id: element.id }] }
});
});

const request = await allSearch({ id });
const requestData = request[0].dataValues;
await notifSender(
'Request edited',
requestData,
lineManager,
APP_URL_BACKEND,
'edited',
'request'
);
await notifSender('Request edited', requestData, lineManager, APP_URL_BACKEND, 'edited', 'request');
return responseUtil(res, 200, strings.request.success.SUCCESS_UPDATE_REQUEST, request);

} catch (error) { return res.status(500).json({ error: 'Something wrong' }); }
} catch (error) {
return res.status(500).json({ error: 'Something wrong' });
}
}

static async searchRequests(req, res) {
Expand All @@ -145,21 +151,27 @@ export default class requestController {

static async findOne(req, res) {
const { id } = req.params;
const { user } = req;
const query = Utilities.requestQueries.singleRequest(id, user.payload.id);
const { requesterId } = req;
const query = Utilities.requestQueries.singleRequest(id, requesterId);
const request = await findOne(query);
return Utilities.responseHelper(
res,
Utilities.stringsHelper.user.requests.SUCCESSFULLY_RETRIEVED_REQUESTS,
!request ? NO_REQUESTS : SUCCESSFULLY_RETRIEVED_REQUESTS,
request,
200
!request ? 404 : 200
);
}

static async storeRequest(req, res) {
const { body, user } = req;
const request = await createRequest(body, user.payload.id);
return destinationController.storeDestination(req, res, body, user, request);
return destinationController.storeDestination(
req,
res,
body,
user,
request
);
}

static async getStats(req, res) {
Expand All @@ -177,10 +189,11 @@ export default class requestController {
const requestResults = await requestHelper.findStatRequest(req);
const tripsNumber = requestResults.length;

return responseUtil(res, 200, (!tripsNumber)
? NO_REQUESTS
: strings.request.success.RESULT, { NumberOfTrips: tripsNumber, Trips: requestResults });

return responseUtil(
res,
200,
!tripsNumber ? NO_REQUESTS : strings.request.success.RESULT,
{ NumberOfTrips: tripsNumber, Trips: requestResults }
);
}

}
8 changes: 2 additions & 6 deletions src/helpers/alreadyProcessed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ const alreadyProcessed = async (res, id, statusId, message, next) => {
const request = await findOneRequest({ id, statusId });

if (request) {
return responseError(
res,
400,
message,
{}
);
return responseError(res, 400, message, {});
}

return next();
};

Expand Down
33 changes: 33 additions & 0 deletions src/middlewares/managerViewRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import requestsSearch from '../utils/requestsSearch';
import findUser from '../helpers/findUser';
import responseUtil from '../utils/responseUtil';
import strings from '../utils/strings';

const managerViewRequest = async (req, res, next) => {
const { role } = req.user.payload;
const { id } = req.params;
let requesterId;
const request = await requestsSearch({ id });
let user;

if (!request.length || !request) {
return responseUtil(res, 404, strings.user.requests.NO_REQUESTS);
}

switch (role) {
case 4:
requesterId = request[0].dataValues.requester.id;
user = await findUser({ id: requesterId });
if (user.lineManager !== req.user.payload.id) {
return responseUtil(res, 403, strings.user.requests.NOT_MANAGER);
}
break;
case 6:
requesterId = req.user.payload.id;
break;
}
req.requesterId = requesterId;
return next();
};

export default managerViewRequest;
3 changes: 2 additions & 1 deletion src/routes/api/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import pendingRequest from '../../middlewares/request';
import isProcessed from '../../middlewares/isProcessed';
import wrongAction from '../../middlewares/wrongAction';
import catchOriginDestination from '../../middlewares/catchOriginDestination';
import managerViewRequest from '../../middlewares/managerViewRequest';

const router = new Router();
const {
Expand Down Expand Up @@ -184,6 +185,6 @@ router.patch('/:id', validateToken, pendingRequest.requestOwner, pendingRequest.
router.put('/comments/:id', validateToken, checkId, validateComment, editComment);
router.delete('/comments/:id', validateToken, checkId, deleteComment);
router.get('/stats/', validateToken, supplierNotAllowed, catchSearchQueries, validateRequestStas, getStats);
router.get('/:id', (req, res) => requestController.findOne(req, res));
router.get('/:id', checkId, managerViewRequest, (req, res) => requestController.findOne(req, res));

export default router;
Loading

0 comments on commit 3f64a16

Please sign in to comment.