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

Commit

Permalink
Merge 6a178e6 into 07cdf03
Browse files Browse the repository at this point in the history
  • Loading branch information
bbaime98 committed Mar 23, 2020
2 parents 07cdf03 + 6a178e6 commit 9c0f6d8
Show file tree
Hide file tree
Showing 11 changed files with 2,489 additions and 2,251 deletions.
Binary file modified .DS_Store
Binary file not shown.
4,534 changes: 2,298 additions & 2,236 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions src/controllers/tripsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TripsService from '../services/tripServices';
import stringHelper from '../utils/stringHelper';
import notifService from '../services/notificationService';
import SendNotification from '../utils/sendNotification';
import StatsController from '../utils/statsCheckings';

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

/**
* @description this function provides requester's trips statistics
* @param {object} req
* @param {object} res
* @return {object} number of all created trips
*/
static async TripStats(req, res) {
const { user } = req;
try {
if (user.role === 'manager') {
const allTrips = await db.Request.findAll({
where: { managerId: user.id },
});
StatsController.stats(allTrips, res);
}
if (user.role === 'requester') {
const allTrips = await db.Request.findAll({
where: { userId: user.id },
});
StatsController.stats(allTrips, res);
}
} catch (err) {
return Response.errorResponse(res, 500, res.__('server error'));
}
}
}
1 change: 1 addition & 0 deletions src/routes/tripsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ router.put('/:requestId/confirm', protectRoute.verifyUser, protectRoute.verifyMa
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('/stats', protectRoute.verifyUser, tripsController.TripStats);

router.get('/:requestId/view', protectRoute.verifyUser, tripsController.viewRequest);
router.get('/view', protectRoute.verifyUser, tripsController.viewAllRequests);
Expand Down
26 changes: 13 additions & 13 deletions src/seeders/20200222234112-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
type: 'two way',
status: 'open',
confirm: false,
returnDate: '2020-02-15',
returnDate: '2020-02-01',
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -23,14 +23,14 @@ module.exports = {
location: 'boston',
destination: 'kigali',
reason: 'meeting with engineers',
departureDate: '2020-12-01',
departureDate: '2017-05-01',
email: 'jdev@andela.com',
type: 'two way',
status: 'rejected',
status: 'approved',
managerId: "0119b84a-99a4-41c0-8a0e-6e0b6c385165",
confirm: false,
returnDate: '2021-02-15',
createdAt: new Date(),
returnDate: '2017-06-15',
createdAt: "2010-02-01T13:46:04.162Z",
updatedAt: new Date(),
},
{
Expand All @@ -39,14 +39,14 @@ module.exports = {
location: 'boston',
destination: 'gisenyi',
reason: 'meeting with engineers write',
departureDate: '2020-12-01',
departureDate: '2020-07-01',
email: 'jdev@andela.com',
type: 'two way',
status: 'rejected',
returnDate: '2021-02-15',
confirm: true,
managerId: '0119b84a-99a4-41c0-8a0e-6e0b6c385165',
createdAt: new Date(),
createdAt: "2020-03-08T13:46:04.162Z",
updatedAt: new Date(),
},
{
Expand All @@ -62,7 +62,7 @@ module.exports = {
confirm: false,
returnDate: '2021-02-15',
managerId: '0119b84a-99a4-41c0-8a0e-6e0b6c385165',
createdAt: new Date(),
createdAt: "2018-01-06T13:46:04.162Z",
updatedAt: new Date(),
},
{
Expand All @@ -71,12 +71,12 @@ module.exports = {
location: 'boston',
destination: 'bisenyi',
reason: 'meeting with engineers write',
departureDate: '2020-12-01',
departureDate: '2017-04-01',
email: 'rejectuser@andela.com',
status: 'open',
type: 'two way',
confirm: false,
returnDate: '2021-02-15',
returnDate: '2017-06-15',
managerId: '79660e6f-4b7d-4d21-81ad-74f64e9e1c8a',
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -102,15 +102,15 @@ module.exports = {
location: 'boston',
destination: 'bisenyi',
reason: 'meeting with engineers write',
departureDate: '2020-10-01',
departureDate: '2018-10-01',
email: 'jdev@andela.com',
userId: '79660e6f-4b7d-4g21-81re-74f54jk91c8a',
status: 'open',
type: 'two way',
confirm: false,
returnDate: '2021-02-24',
returnDate: '2018-11-20',
managerId: '0119b84a-99a4-41c0-8a0e-6e0b6c385165',
createdAt: new Date(),
createdAt: "2020-01-10T13:46:04.162Z",
updatedAt: new Date(),
},
{
Expand Down
3 changes: 2 additions & 1 deletion src/services/localesServices/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,6 @@
"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"
"Request not found or not yours to approve": "Request not found or not yours to approve",
"Trips statistics": "Trips statistics"
}
3 changes: 2 additions & 1 deletion src/services/localesServices/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,6 @@
"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"
"Request not found or not yours to approve": "Demande d'approbation introuvable ou non à vous",
"Trips statistics": "Statistiques de voyages"
}
25 changes: 25 additions & 0 deletions src/swagger/stats.swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @swagger
* /api/v1/trips/stats:
* get:
* security:
* - bearerAuth: []
* tags:
* - Trips
* name: Users can view trip requests
* summary: Users should be able to view stats of created requests
* parameters:
* - name: token
* in: header
* schema:
* type: string
* produces:
* - application/json
* consumes:
* - application/json
* responses:
* '200':
* description: Requests statistics.
* '401':
* description: You are not authorized to perform this action
*/
37 changes: 37 additions & 0 deletions src/tests/trips.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,40 @@ describe('VIEW REQUESTS', () => {
});
});
});

describe('TRIP STATS TESTS', () => {
it('should return statistics of all requestes for that specific manager', (done) => {
chai
.request(app)
.get('/api/v1/trips/stats')
.set('token', managerToken)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.message).to.equal('Trips statistics');
expect(res.body.data.totalTripsNumber).to.equal(10);
expect(res.body.data.upCommingTrips).to.equal(6);
expect(res.body.data.pastYears).to.equal(4);
expect(res.body.data.statusStatistics.openRequests).to.equal(6);
expect(res.body.data.statusStatistics.approvedRequests).to.equal(2);
expect(res.body.data.statusStatistics.rejectedRequets).to.equal(2);
done();
});
});
it('should return statistics of all requestes for that specific requester', (done) => {
chai
.request(app)
.get('/api/v1/trips/stats')
.set('token', token)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.message).to.equal('Trips statistics');
expect(res.body.data.totalTripsNumber).to.equal(7);
expect(res.body.data.upCommingTrips).to.equal(3);
expect(res.body.data.pastYears).to.equal(4);
expect(res.body.data.statusStatistics.openRequests).to.equal(3);
expect(res.body.data.statusStatistics.approvedRequests).to.equal(2);
expect(res.body.data.statusStatistics.rejectedRequets).to.equal(2);
done();
});
});
});
25 changes: 25 additions & 0 deletions src/tests/unitTests/stats.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import chai from 'chai';
import db from '../../models';

const {
expect
} = chai;
const managerId = '0119b84a-99a4-41c0-8a0e-6e0b6c385165';
const managerNoRequest = '75c34027-a2f0-4b50-808e-0c0169fb074c';

describe('STATS UNIT TESTS', () => {
it('should find all requests details', async () => {
const requestDetails = await db.Request.findAll({
where: { managerId }
});
expect(requestDetails).to.be.an('array');
expect(requestDetails.length).to.equal(10);
});
it('should return find an error message', async () => {
const requestDetails = await db.Request.findAll({
where: { managerId: managerNoRequest }
});
expect(requestDetails).to.be.an('array');
expect(requestDetails.length).to.equal(0);
});
});
59 changes: 59 additions & 0 deletions src/utils/statsCheckings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Response from './ResponseHandler';
/**
*
* @description view stats class
* @class StatsController
*/
export default class StatsController {
/**
* @param {object} allTrips
* @param {object} res
* @return {object} return stats
*/
static async stats(allTrips, res) {
const createdTrips = allTrips.length;
const currentYear = new Date().getFullYear();
const currentMonth = new Date().getMonth();
let previousMonths, openRequests, approvedRequests, rejectedRequets, upCommingTrips, pastYears;
const previousMonthsArr = [], upCommingTripsArray = [],
pastYearsArray = [], openRequestArr = [],
approvedRequestArr = [], rejectedRequetsArr = [];
allTrips.forEach(element => {
switch (element.status) {
case 'open':
openRequestArr.push(element);
openRequests = openRequestArr.length;
break;
case 'approved':
approvedRequestArr.push(element);
approvedRequests = approvedRequestArr.length;
break;
case 'rejected':
rejectedRequetsArr.push(element);
rejectedRequets = rejectedRequetsArr.length;
break;
default:
}
if (new Date(element.departureDate).getFullYear() === currentYear) {
if (new Date(element.departureDate).getMonth() < currentMonth) {
previousMonthsArr.push(element);
previousMonths = previousMonthsArr.length;
} else {
upCommingTripsArray.push(element);
upCommingTrips = upCommingTripsArray.length;
}
}
if (new Date(element.departureDate).getFullYear() < currentYear) {
pastYearsArray.push(element);
pastYears = pastYearsArray.length;
}
});
return Response.success(res, 200, res.__('Trips statistics'), {
totalTripsNumber: createdTrips,
upCommingTrips,
previousMonths,
pastYears,
statusStatistics: { openRequests, approvedRequests, rejectedRequets }
});
}
}

0 comments on commit 9c0f6d8

Please sign in to comment.