-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
15 changed files
with
226 additions
and
39 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
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,26 +1,77 @@ | ||
import models from '../database/models'; | ||
import strings from '../utils/stringsUtil'; | ||
import text from '../utils/strings'; | ||
import responseHelper from '../utils/responseHelper'; | ||
import responseUtil from '../utils/responseUtil'; | ||
import userServices from '../services/userServices'; | ||
import Utilities from '../utils/index'; | ||
import findRequests from '../helpers/findRequests'; | ||
|
||
const { | ||
APPROVED, REJECTED, SUCCESSFULLY_RETRIEVED_REQUESTS | ||
} = strings.requests; | ||
const { NO_REQUESTS, ASSIGNED_REQUESTS } = text.user.requests; | ||
|
||
export default class requestController { | ||
|
||
static async viewRequests({ user }, res) { | ||
static async viewManagerRequests(req, res) { | ||
const { id: lineManager } = req.user.payload; | ||
|
||
const users = await models.users.findAll({ | ||
where: { lineManager }, | ||
attributes: ['id', 'username', 'email'], | ||
include: [ | ||
{ | ||
model: models.requests, | ||
where: { statusId: [1, 2, 3] }, | ||
attributes: ['id', 'createdAt'], | ||
include: [ | ||
{ model: models.tripTypes, as: 'type', attributes: ['id', 'name'] }, | ||
{ model: models.requestStatus, as: 'status', attributes: ['id', 'name'] }, | ||
{ 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); | ||
} | ||
|
||
static async viewMyRequests({ user }, res) { | ||
const query = Utilities.requestQueries.userRequests(user.payload); | ||
const { requests } = await userServices.findOne(query, Utilities.queryScopes.responseScope); | ||
|
||
if (!requests.length) { | ||
return Utilities.responseHelper( | ||
res, | ||
Utilities.stringsHelper.user.requests.NO_REQUESTS, | ||
requests, | ||
404 | ||
); | ||
return responseUtil(res, 200, (!requests.length) | ||
? NO_REQUESTS | ||
: SUCCESSFULLY_RETRIEVED_REQUESTS, requests); | ||
} | ||
|
||
static async approveRequest(req, res) { | ||
const { id } = req.params; | ||
|
||
const requestToApprove = findRequests(req); | ||
|
||
if (requestToApprove) { | ||
await models.requests.update({ statusId: 3 }, { where: { id } }); | ||
|
||
return responseHelper(res, APPROVED, null, 200); | ||
} | ||
return responseHelper(res, strings.requests.NOT_FOUND, null, 404); | ||
} | ||
|
||
static async rejectRequest(req, res) { | ||
const { id } = req.params; | ||
|
||
const requestToReject = findRequests(req); | ||
|
||
if (requestToReject) { | ||
await models.requests.update({ statusId: 2 }, { where: { id } }); | ||
|
||
return responseHelper(res, REJECTED, null, 200); | ||
} | ||
return responseHelper(res, strings.user.requests.NOT_FOUND, null, 404); | ||
|
||
return Utilities.responseHelper( | ||
res, | ||
Utilities.stringsHelper.user.requests.SUCCESSFULLY_RETRIEVED_REQUESTS, | ||
requests, | ||
200 | ||
); | ||
} | ||
} |
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
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,20 @@ | ||
import models from '../database/models'; | ||
|
||
const findSpecificRequest = async req => { | ||
const { id: userManager } = req.user.payload; | ||
const { id } = req.params; | ||
|
||
const foundRequest = await models.requests.findOne({ | ||
where: { id }, | ||
attributes: ['id'], | ||
include: [ | ||
{ | ||
model: models.users, | ||
attributes: ['id', 'username', 'email'], | ||
where: { lineManager: userManager } | ||
}] | ||
}); | ||
return foundRequest; | ||
}; | ||
|
||
export default findSpecificRequest; |
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 |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import { Router } from 'express'; | ||
import requestController from '../../controllers/requestController'; | ||
import validateToken from '../../middlewares/auth/validateToken'; | ||
import checkId from '../../middlewares/checkId'; | ||
import checkRole from '../../middlewares/checkRole'; | ||
|
||
const router = new Router(); | ||
const { | ||
viewMyRequests, approveRequest, rejectRequest, viewManagerRequests | ||
} = requestController; | ||
|
||
router.get('/', validateToken, (req, res) => requestController.viewRequests(req, res)); | ||
const { checkManagerRole } = checkRole; | ||
|
||
router.get('/', validateToken, viewMyRequests); | ||
router.get('/manager', validateToken, checkManagerRole, viewManagerRequests); | ||
router.patch('/manager/approve/:id', validateToken, checkManagerRole, checkId, approveRequest); | ||
router.patch('/manager/reject/:id', validateToken, checkManagerRole, checkId, rejectRequest); | ||
|
||
export default router; |
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
Oops, something went wrong.