This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(stats): add a get all stats functionality
- create get all trips stats route for manager - create a get all trips stats route for requester - add get all stats functions for both requesters and managers - add documentation - add tests [Finishes #170947579]
- Loading branch information
Jean Bien Aime Byiringiro
authored and
Jean Bien Aime Byiringiro
committed
Mar 23, 2020
1 parent
07cdf03
commit 6a178e6
Showing
11 changed files
with
2,489 additions
and
2,251 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,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 | ||
*/ |
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,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); | ||
}); | ||
}); |
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,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 } | ||
}); | ||
} | ||
} |