Skip to content

Commit

Permalink
Merge 723caec into f13568e
Browse files Browse the repository at this point in the history
  • Loading branch information
erickyvand committed Mar 30, 2020
2 parents f13568e + 723caec commit 0ba4003
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/controllers/trip.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@ class TripController {
ResponseService.setSuccess(200, 'Trip Updated successfully', updatedTrip);
ResponseService.send(res);
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @memberof TripController
* @returns {object} this function returns the statics of the trip
*/
static async getStats(req, res) {
const stats = await TripService.findAndCountAllTrips({ userId: req.userData.id });
ResponseService.setSuccess(200, 'Your created trip stats', stats);
ResponseService.send(res);
}
}

export default TripController;
1 change: 1 addition & 0 deletions src/routes/trip.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ router.patch('/:tripId/edit',
TripMiddleware.checkTripDestination,
TripMiddleware.checkRequestStatus,
TripController.updateOpenTripRequest);
router.get('/stats', authMiddleware.checkUserLoggedIn, TripController.getStats);

export default router;
19 changes: 19 additions & 0 deletions src/services/trip.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ class TripService {
returning: true
});
}

/**
* A method to querry with condition
* and count all records
* @static
* @param {object} property - The condion as an object
* @param {number} offset - The offset to be used
* @param {number} limit - The limit to be used
* @returns {number} The data retrieved
* @memberof TripService
*/
static findAndCountAllTrips(property) {
return Trip.count({
where: {
...property
},
group: 'tripType'
});
}
}


Expand Down
36 changes: 36 additions & 0 deletions src/swagger/trip.swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,39 @@
* description: Not comments found for you
*
*/

/**
* @swagger
* definitions:
* tripStats:
* type: object
*/

/**
* @swagger
* /api/trips/stats:
* get:
* tags:
* - trips
* name: Trip Stats
* summary: User should be able to view trip stats
* produces:
* - application/json
* consumes:
* - application/json
* parameters:
* - in: header
* name: Authorization
* required: true
* type: string
* schema:
* $ref: '#/definitions/tripStats'
* type: object
* responses:
* '200':
* description: Your created trip stats
* '401':
* description: Unauthorized access. Invalid token,
* Unauthorized access. Invalid token for this user,
* No Token supplied
*/
21 changes: 21 additions & 0 deletions src/tests/trip/stats.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../../app';
import { loggedInToken } from '../fixtures/users.fixture';

chai.should();
chai.use(chaiHttp);

describe('/Get view stats trips ', () => {
it('Should get trip stats', (done) => {
chai.request(app)
.get('/api/trips/stats')
.set('Authorization', loggedInToken)
.end((err, res) => {
res.body.should.be.an('object');
res.status.should.be.equal(200);
res.body.should.have.property('message').equal('Your created trip stats');
done();
});
});
});

0 comments on commit 0ba4003

Please sign in to comment.