Skip to content

Commit

Permalink
Merge pull request #47 from andela/ft-edit-comment-on-travel-request-…
Browse files Browse the repository at this point in the history
…168244900

#168244900 Implements user can edit comment on a trip request
  • Loading branch information
chukwuemekachm authored Sep 11, 2019
2 parents 757c131 + 5e3c9bb commit 788f084
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 37 deletions.
53 changes: 24 additions & 29 deletions public/docs/swaggerDoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@
"$ref": "#/definitions/Landing"
}
},
"400": {
"description": "Invalid"
"400": {
"description": "Invalid"
},
"security": [
{
"api_key": []
}
{
"api_key": []
}
]
}
}
}
},
"/api/v1/auth/signin": {
Expand Down Expand Up @@ -99,12 +99,11 @@
"responses": {
"200": {
"description": "Success",
"schema":{
"schema": {
"title": "Success",
"example": {
"status": "success",
"data": {
}
"data": {}
}
}
},
Expand Down Expand Up @@ -234,17 +233,11 @@
"get": {
"description": "Endpoint to enable users view profile details",
"summary": "View profile details",
"tags": [
"User"
],
"tags": ["User"],
"operationId": "UsersGet",
"deprecated": false,
"produces": [
"application/json"
],
"consumes": [
"application/x-www-form-urlencoded"
],
"produces": ["application/json"],
"consumes": ["application/x-www-form-urlencoded"],
"parameters": [
{
"name": "Authorization",
Expand All @@ -257,12 +250,11 @@
"responses": {
"200": {
"description": "Success",
"schema":{
"schema": {
"title": "Success",
"example": {
"status": "success",
"data": {
}
"data": {}
}
}
},
Expand Down Expand Up @@ -472,7 +464,14 @@
}
}
},
"/api/v1/comment/trips": {
"securityDefinitions": {
"api_key": {
"type": "apiKey",
"name": "token",
"in": "header"
}
},
"/api/v1/trips": {
"post": {
"tags": ["Comment"],
"summary": "Allow users to comment on a trip request",
Expand Down Expand Up @@ -606,9 +605,7 @@
},
"api/v1/comment/trips/:uuid": {
"delete": {
"tags": [
"Comment"
],
"tags": ["Comment"],
"summary": "Delete a user's comment",
"description": "Deletes a user's comment",
"consumes": "application/json",
Expand All @@ -626,9 +623,7 @@
"type": "string"
}
],
"produces": [
"application/json"
],
"produces": ["application/json"],
"responses": {
"200": {
"description": "Comment deleted successful",
Expand Down Expand Up @@ -727,4 +722,4 @@
}
}
}
}
}
8 changes: 4 additions & 4 deletions src/controllers/CommentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ class CommentController {
try {
const { uuid: userUuid, role: userRole } = req.userData;
const { trip_request_uuid: tripRequestUuid, message } = req.body;

// checking for trip request
const tripRequestDetails = await TripRequestRepository.findById({ uuid: tripRequestUuid });
if (!tripRequestDetails) return sendErrorResponse(res, 404, 'This trip request does not exist');
const { user_uuid: tripRequestOwner } = tripRequestDetails;
// ensuring ownership

const isAllowed = await checkrequestOwner(tripRequestOwner, userUuid, userRole);
if (!isAllowed && userRole === 'Manager') return sendErrorResponse(res, 403, 'You are not the manager of the user that created this trip request');
if (!isAllowed && userRole !== 'Manager') return sendErrorResponse(res, 403, 'You can\'t comment on a trip request you did not create');
// creating comment

const commentDetails = {
user_uuid: userUuid,
trip_request_uuid: tripRequestUuid,
Expand Down Expand Up @@ -70,9 +68,11 @@ class CommentController {
const { uuid: userUuid } = req.userData;
const { message } = req.body;
const { commentUuid } = req.params;

const comment = await CommentRepository.getOne({ uuid: commentUuid });
if (!comment) return sendErrorResponse(res, 404, 'This comment does not exist');
const { dataValues } = comment;

if (userUuid !== dataValues.user_uuid) return sendErrorResponse(res, 403, 'You can\'t edit a comment you didn\'t post');
const [editedComment] = await CommentRepository.updateOne({ message }, { uuid: commentUuid });
if (editedComment) return sendSuccessResponse(res, 200, ...editedComment);
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/TripRequestController.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ class TripRequestController {
const { manager } = await UserRepository.getOne({ uuid: req.userData.uuid }, ['manager']);
if (!manager) return sendErrorResponse(res, 403, 'You are not allowed to create a trip request because you don\'t have a manager');
req.userData.managerUuid = manager.dataValues.user_uuid;
// checking if the office locations exist

const tripDeparture = await OfficeLocationRepository.findById({ uuid: leavingFrom });
if (!tripDeparture) return sendErrorResponse(res, 404, 'The office location you are leaving from does not exist');
const tripDestination = await OfficeLocationRepository.findById({ uuid: destination });
if (!tripDestination) return sendErrorResponse(res, 404, 'The office location you are going to does not exist');

if (leavingFrom === destination) return sendErrorResponse(res, 422, 'your destination must be a location different from where you are leaving from');
return (requestType === 'oneWayTrip') ? TripRequestController.oneWayTripCreator(req, res, next)
: TripRequestController.returnTripCreator(req, res, next);
Expand Down Expand Up @@ -79,7 +80,7 @@ class TripRequestController {
trip_request_uuid: tripRequestUuid,
office_location_uuid: tripRequest.destination
};
// updating destination for trip request and creating notifications for manager

const { managerUuid } = req.userData;
const [destinated, managerNotified] = await Promise.all(
[TripDestinationRespository.create(tripDestinationDetails),
Expand Down
5 changes: 3 additions & 2 deletions tests/passwordReset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ describe('Password reset Tests', () => {
done();
});
});

it('"/api/v1/auth/forgot_password" Should fail if email is not provided', (done) => {
it('"/api/v1/auth/forgotPassword" Should fail if email is not provided', (done) => {
chai.request(app)
.post('/api/v1/auth/forgot_password')
.send()
Expand All @@ -57,6 +57,7 @@ describe('Password reset Tests', () => {
done();
});
});

it('"/api/v1/auth/forgot_password" Should fail if email is invalid', (done) => {
chai.request(app)
.post('/api/v1/auth/forgot_password')
Expand Down

0 comments on commit 788f084

Please sign in to comment.