Skip to content

Commit

Permalink
feature(view comments): add view comments
Browse files Browse the repository at this point in the history
- add view comments functionality
- add view comments test
- add swagger documentation of the feature

[Starts #170766760]
  • Loading branch information
erickyvand committed Mar 10, 2020
1 parent 37ec342 commit 2339be6
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 9 deletions.
34 changes: 33 additions & 1 deletion src/controllers/comment.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CommentService from '../services/comment.service';
import ResponseService from '../services/response.service';
import { paginationHelper } from '../helpers';

/**
* class
Expand All @@ -25,7 +26,6 @@ class CommentController {
return ResponseService.send(res);
}


/**
*
*
Expand All @@ -41,6 +41,38 @@ class CommentController {
ResponseService.setSuccess(200, 'Comment was deleted successfully');
return ResponseService.send(res);
}

/**
*
*
* @static
* @param {req} req
* @param {res} res
* @returns {object} This function returns list of comments posted on thread
* @memberof CommentController
*/
static async viewCommentPostedOnTripRequest(req, res) {
const { page = 1, limit = 10 } = req.query;
const offset = (page - 1) * limit;
const results = await CommentService
.findByPropertyAndCountAll(
{
userId: req.userData.id,
subjectId: req.params.tripId
},
{
offset,
limit
}
);
ResponseService.setSuccess(200, 'List all comments', {
pageMeta: paginationHelper({
count: results.count, rows: results.rows, offset, limit
}),
rows: results.rows
});
return ResponseService.send(res);
}
}

export default CommentController;
23 changes: 23 additions & 0 deletions src/middlewares/comment.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ class CommentMiddleware {
}
next();
}

/**
*
*
* @static
* @param {req} req
* @param {res} res
* @param {next} next
* @returns {validation} This function validate view comments route
* @memberof UserValidation
*/
static async validateViewComment(req, res, next) {
const comment = await CommentService.findCommentByProperty({
userId: req.userData.id,
subjectId: req.params.tripId
});

if (!comment) {
ResponseService.setError(404, 'Comments not found or does not belong to this trip');
return ResponseService.send(res);
}
next();
}
}

export default CommentMiddleware;
9 changes: 1 addition & 8 deletions src/migrations/20200303174302-alter-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,4 @@ export function up(queryInterface, Sequelize) {
* @param {object} queryInterface
* @param {object} Sequelize
*/
export function down(queryInterface) {
return queryInterface.sequelize.transaction(t => Promise.all([
queryInterface.removeColumn('Requests', 'requesterFname', { transaction: t }),
queryInterface.removeColumn('Requests', 'frequesterLname', { transaction: t }),
queryInterface.removeColumn('Requests', 'requesterPicture', { transaction: t }),
queryInterface.removeColumn('Requests', 'tripType', { transaction: t })
]));
}
export function down(queryInterface) { return queryInterface.dropTable('Requests'); }
1 change: 1 addition & 0 deletions src/routes/trip.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ router.get('/requests', authMiddleware.checkUserLoggedIn, requestValidation, Tri
router.get('/locations', authMiddleware.checkUserLoggedIn, TripController.viewAvailableLocations);
router.post('/requests/:tripId/comments', authMiddleware.checkUserLoggedIn, UserValidation.validateUserComment, CommentController.addCommentOnTripRequest); // user comment on request trip route
router.delete('/:tripId/comments/:commentId', authMiddleware.checkUserLoggedIn, UserValidation.validateDeleteTripComment, TripMiddleware.checkTripExist, CommentMiddleware.checkCommentExist, CommentController.deleteComment); // user deletes comment route
router.get('/request/:tripId/comments', authMiddleware.checkUserLoggedIn, CommentMiddleware.validateViewComment, CommentController.viewCommentPostedOnTripRequest); // user view comment posted on the thread

export default router;
21 changes: 21 additions & 0 deletions src/services/comment.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ class CommentService {
where: property,
});
}

/**
* 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 findByPropertyAndCountAll(property, { offset, limit }) {
return Comments.findAndCountAll({
where: property,
order: [
['id', 'DESC']
],
offset,
limit
});
}
}

export default CommentService;
42 changes: 42 additions & 0 deletions src/swagger/trip.swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,45 @@
* description: Trip ID doesn't exists, Comment ID doesn't exists
*
*/

/**
* @swagger
* /api/trips/request/{tripId}/comments:
* get:
* tags:
* - trips
* name: View all comments
* summary: User should be able to view his comments posted on requested trip
* produces:
* - application/json
* consumes:
* - application/json
* parameters:
* - in: header
* name: Authorization
* required: true
* type: string
* - in: path
* name: tripId
* - name: page
* in: query
* type: integer
* - name: limit
* in: query
* type: integer
* schema:
* $ref: '#/definitions/viewComments'
* type: object
* responses:
* '200':
* description: List all comments
* '400':
* description: Page must be greater than 0,
* Limit must be a number,
* Page must be a number,
* Page is required,
* Limit is required
* '404':
* description: Not comments found for you
*
*/
11 changes: 11 additions & 0 deletions src/tests/fixtures/comments.fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export const commentToDelete = {
comment: faker.lorem.sentence()
};

export const commentToView = {
id: faker.random.number({ min: 30, max: 35 }),
userId: loggedInUser.id,
subjectId: newTripComment.id,
subjectType: 'Trip',
comment: faker.lorem.sentence(),
createdAt: '',
updatedAt: '',
};

export const commentOfOtherUser = {
...commentToDelete, subjectId: faker.random.number({ min: 60, max: 65 })
};
Expand All @@ -40,4 +50,5 @@ export const subjectType = {
export const createComment = async () => {
await Comments.destroy({ where: {} });
await Comments.create(commentToDelete);
await Comments.create(commentToView);
};
File renamed without changes.
70 changes: 70 additions & 0 deletions src/tests/trip/02-view-comments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../../app';
import { loggedInToken, userWithNoTripToken } from '../fixtures/users.fixture';
import { createTrip } from '../fixtures/trip.fixture';

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

describe('/Get view comments posted on trips requests', () => {
let trip;
before(async () => {
trip = await createTrip();
});
it('Should get comments posted on trip requests', (done) => {
chai.request(app)
.get(`/api/trips/request/${trip.id}/comments?page=1&limit=1`)
.set('Authorization', loggedInToken)
.end((err, res) => {
res.body.should.be.an('object');
res.status.should.be.equal(200);
res.body.data.should.have.property('pageMeta');
res.body.data.should.have.property('rows');
res.body.data.rows.should.be.an('array');
res.body.data.rows[0].should.have.property('id');
res.body.data.rows[0].should.have.property('userId');
res.body.data.rows[0].should.have.property('subjectId');
res.body.data.rows[0].should.have.property('subjectType');
res.body.data.rows[0].should.have.property('comment');
res.body.data.rows[0].should.have.property('createdAt');
res.body.data.rows[0].should.have.property('updatedAt');
res.body.should.have.property('message').equal('List all comments');
done();
});
});

it('Should get comments posted on trip requests', (done) => {
chai.request(app)
.get(`/api/trips/request/${trip.id}/comments`)
.set('Authorization', loggedInToken)
.end((err, res) => {
res.body.should.be.an('object');
res.status.should.be.equal(200);
res.body.data.should.have.property('pageMeta');
res.body.data.should.have.property('rows');
res.body.data.rows.should.be.an('array');
res.body.data.rows[0].should.have.property('id');
res.body.data.rows[0].should.have.property('userId');
res.body.data.rows[0].should.have.property('subjectId');
res.body.data.rows[0].should.have.property('subjectType');
res.body.data.rows[0].should.have.property('comment');
res.body.data.rows[0].should.have.property('createdAt');
res.body.data.rows[0].should.have.property('updatedAt');
res.body.should.have.property('message').equal('List all comments');
done();
});
});

it('Should not show requested trip comments when no comment found or does not belong to the trip', (done) => {
chai.request(app)
.get(`/api/trips/request/${trip.id}/comments?page=1&limit=1`)
.set('Authorization', userWithNoTripToken)
.end((err, res) => {
res.body.should.be.an('object');
res.status.should.be.equal(404);
res.body.should.have.property('message').equal('Comments not found or does not belong to this trip');
done();
});
});
});
File renamed without changes.

0 comments on commit 2339be6

Please sign in to comment.