Skip to content

Commit

Permalink
Chore(stats): view past trip stats only
Browse files Browse the repository at this point in the history
- adds possibility to only view stats from the past
- adds possibility to view actual trips
[Finishes#169649132]
  • Loading branch information
alainmateso committed Nov 13, 2019
1 parent ad1e557 commit 727c49b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
22 changes: 20 additions & 2 deletions src/controllers/requestController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-irregular-whitespace */
/* eslint-disable require-jsdoc */
import Sequelize from 'sequelize';
import moment from 'moment';
import models from '../database/models';
import strings from '../utils/stringsUtil';
import text from '../utils/strings';
Expand All @@ -15,6 +16,8 @@ import findOneRequest from '../helpers/findOneRequest';
import findUser from '../helpers/findUser';
import requestHelper from '../helpers/requestHelper';
import notifSender from '../helpers/notifSender';
import dateValidator from '../helpers/datesValidator';


const { Op } = Sequelize;
const { SUCCESSFULLY_RETRIEVED_REQUESTS } = strings.requests;
Expand Down Expand Up @@ -160,8 +163,23 @@ export default class requestController {
}

static async getStats(req, res) {
const requestResults = await requestHelper.findStatRequest(req, res);
return responseUtil(res, 200, strings.request.success.RESULT, requestResults.count);
const { startDate, endDate } = req.query;
const checkDate = dateValidator(startDate, endDate);

if (checkDate) {
return responseUtil(res, 400, strings.request.error.DATE_ERROR);
}

if (endDate > moment().format('YYYY-MM-DD')) {
return responseUtil(res, 400, strings.request.error.OUT_OF_BOUND);
}

const requestResults = await requestHelper.findStatRequest(req);
const tripsNumber = requestResults.length;

return responseUtil(res, 200, (!tripsNumber)
? NO_REQUESTS
: strings.request.success.RESULT, { NumberOfTrips: tripsNumber, Trips: requestResults });

}

Expand Down
23 changes: 12 additions & 11 deletions src/helpers/requestHelper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import sequelize from 'sequelize';
import models from '../database/models';
import dateValidator from './datesValidator';
import responseUtil from '../utils/responseUtil';
import strings from '../utils/stringsUtil';

const findOneRequest = properties => {
const request = models.requests.findOne({
Expand All @@ -12,21 +9,25 @@ const findOneRequest = properties => {
return request;
};

const findStatRequest = async (req, res) => {
const { startDate, endDate } = req.query;
const findStatRequest = async req => {
const { Op } = sequelize;
const checkDate = dateValidator(startDate, endDate);
if (checkDate === true) {
responseUtil(res, 400, strings.request.error.DATE_ERROR);
}
const requestResult = await models.requests.findAndCountAll({
const { startDate, endDate } = req.query;

const requestResult = await models.requests.findAll({
where: {
userId: req.user.payload.id,
statusId: 3,
departureDate: {
[Op.between]: [startDate, endDate],
}
}
},
attributes: { exclude: ['typeId', 'userId', 'statusId', 'locationId'] },
include: [
{ model: models.destinations, attributes: ['id', 'arrivalDate', 'departureDate', 'reasons'], include: [{ model: models.locations, as: 'location', attributes: ['name'] }] },
{ model: models.locations, as: 'origin', attributes: ['id', 'name', 'country'] },
{ model: models.requestStatus, as: 'status', attributes: ['id', 'name'] },
{ model: models.tripTypes, as: 'type', attributes: ['id', 'name'] },
]
});
return requestResult;
};
Expand Down
34 changes: 34 additions & 0 deletions src/tests/RequestStats.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ chai.use(chaiHttp);

let userToken;
let supplierToken;
let requesterToken;

describe('stats Requests Tests', () => {
before(done => {
Expand All @@ -23,6 +24,12 @@ describe('stats Requests Tests', () => {
.send(mockData.supplier)
.end((err, res) => {
supplierToken = res.body.data.token;
});
chai.request(app)
.post('/api/v1/users/login')
.send(mockData.requester)
.end((err, res) => {
requesterToken = res.body.data.token;
done();
});
});
Expand All @@ -33,6 +40,8 @@ describe('stats Requests Tests', () => {
.get('/api/v1/requests/stats?startDate=2019-03-03&endDate=2019-11-07')
.set('Authorization', `Bearer ${userToken}`)
.end((err, res) => {
console.log(res.body);

res.should.have.property('status').eql(200);
res.body.should.have.property('message').eql('Your number of trips are:');
done();
Expand All @@ -49,6 +58,19 @@ describe('stats Requests Tests', () => {
done();
});
});

it('Should tell the user that Either startDate or endDate must not be greater than today\'s date', done => {
chai.request(app)
.get('/api/v1/requests/stats?startDate=2019-12-07&endDate=2020-11-03')
.set('Authorization', `Bearer ${userToken}`)
.end((err, res) => {
console.log(res.body);

res.should.have.property('status').eql(400);
res.body.should.have.property('message').eql('Either startDate or endDate must not be greater than today\'s date');
done();
});
});

it('Should return 403 status, Supplier not allowed', done => {
chai.request(app)
Expand All @@ -69,4 +91,16 @@ describe('stats Requests Tests', () => {
done();
});
});

it('Should tell the user that they have no trips', done => {
chai.request(app)
.get('/api/v1/requests/stats?startDate=2019-01-07&endDate=2019-11-12')
.set('Authorization', `Bearer ${requesterToken}`)
.end((err, res) => {
console.log(res.body);

res.should.have.property('status').eql(200);
done();
});
});
});
1 change: 1 addition & 0 deletions src/utils/stringsUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const strings = {
TRAVEL_ADMINS_ONLY: 'Access denied! Only travel administrators can access this part of the system!',
NOT_YOUR_REQUEST: 'You are not the owner of the request or the line manager of the user who placed it!',
NO_LOCATION: 'Location does not exist on the system!',
OUT_OF_BOUND: 'Either startDate or endDate must not be greater than today\'s date',
},
},
token: {
Expand Down

0 comments on commit 727c49b

Please sign in to comment.