Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Commit

Permalink
Merge 17934a0 into 080a930
Browse files Browse the repository at this point in the history
  • Loading branch information
Baraka-Mugisha committed Mar 19, 2020
2 parents 080a930 + 17934a0 commit e50a0b4
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 16 deletions.
64 changes: 64 additions & 0 deletions src/controllers/tripsController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import uuid from 'uuid/v4';
import dotenv from 'dotenv';
import db from '../models';
import Response from '../utils/ResponseHandler';
import TripsService from '../services/tripServices';
import stringHelper from '../utils/stringHelper';
import notifService from '../services/notificationService';
import SendNotification from '../utils/sendNotification';

dotenv.config();

/**
* @description RequestController Controller
* @class RequestController
Expand Down Expand Up @@ -428,4 +431,65 @@ export default class requestController {
return Response.errorResponse(res, 500, res.__('server error'));
}
}

/**
* @param {object} req
* @param {object} res
* @param {object} next
* @return {object} view specific trip request
*/
static async viewRequest(req, res) {
try {
const { user } = req;
const { requestId } = req.params;
if (user.role === 'manager') {
const availableRequest = await db.Request.findOne({
where: {
id: requestId,
managerId: user.id
}
});
return !availableRequest
? Response.errorResponse(res, 404, res.__('Request not found or not yours to manage'))
: Response.success(res, 200, res.__('Request found'), availableRequest);
}
if (user.role === 'requester') {
const Request = await db.Request.findOne({ where: { id: requestId, email: user.email } });
return !Request
? Response.errorResponse(res, 404, res.__('Request not found or not yours'))
: Response.success(res, 200, res.__('Request found'), Request);
}
return Response.errorResponse(res, 401, res.__('you are not authorised for this operation'));
} catch (err) {
return Response.errorResponse(res, 500, res.__('server error'));
}
}

/**
* @param {object} req
* @param {object} res
* @param {object} next
* @return {object} view all trip requests
*/
static async viewAllRequests(req, res) {
try {
const { user } = req;
if (user.role === 'manager') {
const availableRequests = await db.Request.findAll({ where: { managerId: user.id } });
return availableRequests.length === 0
? Response.errorResponse(res, 404, res.__('No trip requests available'))
: Response.success(res, 200, res.__('Pending requests to approve'), availableRequests);
}

if (user.role === 'requester') {
const Requests = await db.Request.findAll({ where: { email: user.email } });
return Requests.length === 0
? Response.errorResponse(res, 404, res.__('Requests not found'))
: Response.success(res, 200, res.__('Requests found'), Requests);
}
return Response.errorResponse(res, 401, res.__('you are not authorised for this operation'));
} catch (err) {
return Response.errorResponse(res, 500, res.__('server error'));
}
}
}
5 changes: 4 additions & 1 deletion src/routes/tripsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ router.post('/one-way', protectRoute.verifyUser, protectRoute.verifyRequester, p
router.post('/return', protectRoute.verifyUser, protectRoute.verifyRequester, protectRoute.checkUserManager, rememberProfile, returnTripRules, validationResult, tripsController.createReturnRequest);
router.post('/multi-city', protectRoute.verifyUser, protectRoute.verifyRequester, rememberProfile, multiCityTripRules, validationResult, tripsController.createMultiCityRequest);
router.patch('/edit', protectRoute.verifyUser, protectRoute.verifyRequester, rememberProfile, requestRules, validationResult, tripsController.editRequest);
router.get('/view', protectRoute.verifyUser, protectRoute.verifyManager, tripsController.availTripRequests);
router.get('/view-pending', protectRoute.verifyUser, protectRoute.verifyManager, tripsController.availTripRequests);
router.put('/:requestId/confirm', protectRoute.verifyUser, protectRoute.verifyManager, tripsController.confirmRequest);
router.patch('/:requestId/reject', protectRoute.verifyUser, protectRoute.verifyManager, tripsController.rejectRequest);
router.patch('/:requestId/approve', protectRoute.verifyUser, protectRoute.verifyManager, tripsController.approveRequest);
router.get('/search', protectRoute.verifyUser, searchQueryRules, validationResult, tripsController.requestSearch);

router.get('/:requestId/view', protectRoute.verifyUser, tripsController.viewRequest);
router.get('/view', protectRoute.verifyUser, tripsController.viewAllRequests);

export default router;
7 changes: 6 additions & 1 deletion src/services/localesServices/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,10 @@
"Invalid value": "Invalid value",
"password can not be empty": "password can not be empty",
"email can not be empty": "email can not be empty",
"a comment is required with maximum 200 characters": "a comment is required with maximum 200 characters"
"a comment is required with maximum 200 characters": "a comment is required with maximum 200 characters",
"Request not found or not yours": "Request not found or not yours",
"Requests found": "Requests found",
"Request found": "Request found",
"Requests not found": "Requests not found",
"Request not found or not yours to approve": "Request not found or not yours to approve"
}
8 changes: 6 additions & 2 deletions src/services/localesServices/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
"This request has been a created by another user and belongs to another manager": "Cette demande a été créée par un autre utilisateur et appartient à un autre gestionnaire",
"all unread notifications marked as read": "toutes les notifications non lues marquées comme lues",
"no unread notifications": "pas de notifications non lues",
"Your trip to bisenyi on 2020-10-01 you requested has been approved": "Votre voyage à Bisenyi le 01/10/2020 que vous avez demandé a été approuvé",
"To view this approved request you made click below": "Pour voir cette demande approuvée que vous avez faite, cliquez ci-dessous",
"View request": "Afficher la demande",
"the trip to": "le voyage à",
Expand All @@ -168,5 +167,10 @@
"Comment not found": "Commentaire non trouvé",
"You are not authorised to delete this comment": "Vous n'êtes pas autorisé à supprimer ce commentaire",
"a comment is required with maximum 200 characters": "un commentaire est requis avec un maximum de 200 caractères",
"Comment is successfully deleted": "Le commentaire a bien été supprimé"
"Comment is successfully deleted": "Le commentaire a bien été supprimé",
"Request not found or not yours": "Demande non trouvée ou pas la vôtre",
"Requests found": "Demandes trouvées",
"Requests not found": "Demandes non trouvées",
"Request found": "Demande trouvée",
"Request not found or not yours to approve": "Demande d'approbation introuvable ou non à vous"
}
64 changes: 62 additions & 2 deletions src/swagger/trips.swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@
* description: The request does not exist or it's either been approved or rejected
* '401':
* description: Only the requester of this trip can edit the trip.
* */
*/

/**
* @swagger
* definitions:
Expand Down Expand Up @@ -304,7 +305,7 @@

/**
* @swagger
* /api/v1/trips/view:
* /api/v1/trips/view-pending:
* get:
* security:
* - bearerAuth: []
Expand All @@ -327,6 +328,7 @@
* '401':
* description: You are not authorized to perform this action
*/

/**
* @swagger
* /api/v1/trips/{requestId}/confirm:
Expand All @@ -351,6 +353,7 @@
* '200':
* description: request re-confirmed.
* */

/**
* @swagger
* /api/v1/trips/{requestId}/reject:
Expand Down Expand Up @@ -400,6 +403,7 @@
* '404':
* description: request with that id and still open is not found in your direct report
* */

/**
* @swagger
* /api/v1/trips/search:
Expand Down Expand Up @@ -436,3 +440,59 @@
* '404':
* description: request not found.
* */

/**
* @swagger
* /api/v1/trips/{requestId}/view:
* get:
* security:
* - bearerAuth: []
* tags:
* - Trips
* name: User view a trip request
* summary: User should be able to view a trip request he made
* parameters:
* - name: token
* in: header
* - name: requestId
* in: path
* produces:
* - application/json
* consumes:
* - application/json
* responses:
* '200':
* description: Request found
* '401':
* description: You are not authorized to perform this action
* '404':
* description: Request not found or not yours
*/

/**
* @swagger
* /api/v1/trips/view:
* get:
* security:
* - bearerAuth: []
* tags:
* - Trips
* name: Manager or requester view trip requests
* summary: Requester and manager should be able to view all requests the requester's requests
* parameters:
* - name: token
* in: header
* schema:
* type: string
* produces:
* - application/json
* consumes:
* - application/json
* responses:
* '200':
* description: Requests found
* '404':
* description: Requests not found
* '401':
* description: You are not authorized to perform this action
*/
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('AVAIL REQUESTS FOR APPROVAL', () => {
it('should pending requests to approve', (done) => {
chai
.request(index)
.get('/api/v1/trips/view')
.get('/api/v1/trips/view-pending')
.set('token', token)
.end((_err, res) => {
expect(res.status).to.equal(200);
Expand All @@ -40,7 +40,7 @@ describe('AVAIL REQUESTS FOR APPROVAL', () => {
.request(index)
.post('/api/v1/auth/login')
.send({
email: 'jdev@andela.com',
email: 'jim@andela.com',
password: 'Bien@BAR789'
})
.end((err, res) => {
Expand All @@ -51,7 +51,7 @@ describe('AVAIL REQUESTS FOR APPROVAL', () => {
it('should return that your not authorised for this operation', (done) => {
chai
.request(index)
.get('/api/v1/trips/view')
.get('/api/v1/trips/view-pending')
.set('token', unauthToken)
.end((_err, res) => {
expect(res.status).to.equal(401);
Expand Down
12 changes: 6 additions & 6 deletions src/tests/booking.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import chai from 'chai';
import chaiHttp from 'chai-http';
import server from '../index';
import app from '../index';
import db from '../models';


Expand All @@ -19,7 +19,7 @@ describe('FACILITY BOOKING', () => {
password: 'Bien@BAR789',
};
chai
.request(server)
.request(app)
.post('/api/v1/auth/login')
.send(loggedUser)
.end((err, res) => {
Expand All @@ -30,7 +30,7 @@ describe('FACILITY BOOKING', () => {

it('should book a room', (done) => {
chai
.request(server)
.request(app)
.post(bookingRoute)
.set('token', token)
.send({
Expand All @@ -48,7 +48,7 @@ describe('FACILITY BOOKING', () => {
});
it('should not book for a request does not exist or is not yours', (done) => {
chai
.request(server)
.request(app)
.post(bookingRoute)
.set('token', token)
.send({
Expand All @@ -66,7 +66,7 @@ describe('FACILITY BOOKING', () => {
});
it('should not book an unexisting room', (done) => {
chai
.request(server)
.request(app)
.post(bookingRoute)
.set('token', token)
.send({
Expand All @@ -85,7 +85,7 @@ describe('FACILITY BOOKING', () => {

it('should not book a room with wrong checkin greater than checkout', (done) => {
chai
.request(server)
.request(app)
.post(bookingRoute)
.set('token', token)
.send({
Expand Down
Loading

0 comments on commit e50a0b4

Please sign in to comment.