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

Commit

Permalink
Merge pull request #55 from andela/ft-travelled-destinations-170947580
Browse files Browse the repository at this point in the history
  • Loading branch information
jabichris committed Mar 25, 2020
2 parents 26a7a62 + efab6ad commit ceeadbd
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/controllers/tripsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,28 @@ export default class requestController {
return Response.errorResponse(res, 500, res.__('server error'));
}
}

/**
* @description this function finds and displays most travelled destinations
* @param {object} req
* @param {object} res
* @return {object} most travelled destinations
*/
static async mostTravelledDestinations(req, res) {
try {
const counting = await db.Request.findAll({
attributes: ['destination', [db.sequelize.fn('count', db.sequelize.col('destination')), 'count']],
group: ['Request.destination'],
raw: true,
order: db.sequelize.literal('count DESC'),
where: {
status: 'approved',
confirm: true
}
});
return Response.success(res, 200, res.__('Most travelled destinations'), { Destinations: counting });
} catch (err) {
return Response.errorResponse(res, 500, res.__('server error'));
}
}
}
3 changes: 1 addition & 2 deletions src/routes/tripsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ router.patch('/:requestId/reject', protectRoute.verifyUser, protectRoute.verifyM
router.patch('/:requestId/approve', protectRoute.verifyUser, protectRoute.verifyManager, tripsController.approveRequest);
router.get('/search', protectRoute.verifyUser, searchQueryRules, validationResult, tripsController.requestSearch);
router.get('/stats', protectRoute.verifyUser, tripsController.TripStats);

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

router.get('/most-travelled', protectRoute.verifyUser, tripsController.mostTravelledDestinations);
export default router;
3 changes: 2 additions & 1 deletion src/services/localesServices/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,6 @@
"Request not found or not yours to manage": "Request not found or not yours to manage",
"feedback is required": "feedback is required",
"feedback can't be longer than 300 character": "feedback can't be longer than 300 character",
"feedback saved successfully" : "feedback saved successfully"
"feedback saved successfully" : "feedback saved successfully",
"Most travelled destinations": "Most travelled destinations"
}
3 changes: 2 additions & 1 deletion src/services/localesServices/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,6 @@
"Trips statistics": "Statistiques de voyages",
"feedback is required": "une rétroaction est requise",
"feedback can't be longer than 300 character": "les commentaires ne peuvent pas dépasser 300 caractères",
"feedback saved successfully" : "commentaires enregistrés avec succès"
"feedback saved successfully" : "commentaires enregistrés avec succès",
"Most travelled destinations": "Destinations les plus fréquentées"
}
25 changes: 25 additions & 0 deletions src/swagger/trips.swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,28 @@
* '401':
* description: You are not authorized to perform this action
*/
/**
* @swagger
* /api/v1/trips/most-travelled:
* get:
* security:
* - bearerAuth: []
* tags:
* - Trips
* name: Most travelled destination
* summary: Users should be able to view the most travelled destination
* parameters:
* - name: token
* in: header
* schema:
* type: string
* produces:
* - application/json
* consumes:
* - application/json
* responses:
* '200':
* description: Most travelled destination.
* '401':
* description: You are not authorized to perform this action
*/
13 changes: 13 additions & 0 deletions src/tests/trips.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,16 @@ describe('TRIP STATS TESTS', () => {
});
});
});
describe('MOST TRAVELLED DESTINATION TEST', () => {
it('should return the most travelled destinations', (done) => {
chai
.request(app)
.get('/api/v1/trips/most-travelled')
.set('token', token)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.message).to.equal('Most travelled destinations');
done();
});
});
});
16 changes: 16 additions & 0 deletions src/tests/unitTests/tripService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import sgMail from '@sendgrid/mail';
import sinon from 'sinon';
import TripsService from '../../services/tripServices';
import stringHelper from '../../utils/stringHelper';
import db from '../../models';

const {
expect
Expand Down Expand Up @@ -59,3 +60,18 @@ describe('APPROVE REQUEST SERVICE TEST', () => {
expect(approvedRequest.status).to.equal('approved');
});
});
describe('MOST TRAVELLED DESTINATION UNIT TEST', () => {
it('should return the most travelled destinations', async () => {
const counting = await db.Request.findAll({
attributes: ['destination', [db.sequelize.fn('count', db.sequelize.col('destination')), 'count']],
group: ['Request.destination'],
raw: true,
order: db.sequelize.literal('count DESC'),
where: {
status: 'approved',
confirm: true
}
});
expect(counting).to.be.an('array');
});
});

0 comments on commit ceeadbd

Please sign in to comment.