Skip to content

Commit

Permalink
Merge pull request #48 from andela/ft-delete-comment-posted-within-co…
Browse files Browse the repository at this point in the history
…mment-thread-167728034

#167728034 Complete endpoint for user can delete comment posted within comment thread
  • Loading branch information
chukwuemekachm authored Sep 11, 2019
2 parents d9a56ad + a8644b8 commit 757c131
Show file tree
Hide file tree
Showing 25 changed files with 433 additions and 141 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
],
"rules": {
"one-var": 0,
"arrow-parens": 0,
"one-var-declaration-per-line": 0,
"new-cap": 0,
"consistent-return": 0,
"linebreak-style": 0,
"no-param-reassign": 0,
"no-trailing-spaces": 0,
"comma-dangle": 0,
"camelcase": 0,
"no-unused-expressions": 0,
"arrow-parens": 0,
"class-methods-use-this": 0,
"curly": ["error", "multi-line"],
"import/no-unresolved": [2, { "commonjs": true }],
"no-shadow": ["error", { "allow": ["req", "res", "err"] }],
Expand Down
2 changes: 1 addition & 1 deletion .sequelizerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path');

module.exports = {
"config": path.resolve('./src/config', 'config.js'),
"models-path": path.resolve('./src/models'),
"models-path": path.resolve('./src/models'),
"seeders-path": path.resolve('./src/database/seeders'),
"migrations-path": path.resolve('./src/database/migrations')
};
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions public/docs/swaggerDoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,39 @@
}
}
}
},
"api/v1/comment/trips/:uuid": {
"delete": {
"tags": [
"Comment"
],
"summary": "Delete a user's comment",
"description": "Deletes a user's comment",
"consumes": "application/json",
"security": [
{
"bearerAuth": []
}
],
"parameters": [
{
"name": "uuid",
"in": "path",
"description": "uuid of comment to delete",
"required": true,
"type": "string"
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Comment deleted successful",
"name": "type"
}
}
}
}
},
"definitions": {
Expand Down
1 change: 0 additions & 1 deletion src/controllers/AdminController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable class-methods-use-this */
import UserRepository from '../repositories/UserRepository';
import { sendSuccessResponse, sendErrorResponse, successResponse } from '../utils/sendResponse';
import { inValidEmail } from '../modules/validator';
Expand Down
10 changes: 3 additions & 7 deletions src/controllers/AuthController.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
/* eslint-disable class-methods-use-this */
/* eslint-disable camelcase */
import UserRepository from '../repositories/UserRepository';
import { blackListThisToken } from '../utils';
import { createToken, verifyToken } from '../modules/tokenProcessor';
import { sendErrorResponse, successResponse, sendSuccessResponse } from '../utils/sendResponse';
import { inValidEmail, inValidPassword, magicTrimmer } from '../modules/validator';
import sendEmail from '../services/emails';
import { hashPassword, unhashPassword } from '../utils/hashPassword';
import userInfo from '../utils/getUserInfo';

import userInfo from '../utils/createAccessToken';

/**
* @description User controller
Expand Down Expand Up @@ -225,7 +222,7 @@ class AuthController {
}
return sendErrorResponse(res, 400, 'User not found');
} catch (error) {
next(error);
return next(error);
}
}

Expand Down Expand Up @@ -275,10 +272,9 @@ class AuthController {
return sendErrorResponse(res, 409, `User ${email} already exists`);
}
} catch (error) {
next(error);
return next(error);
}
}
}


export default new AuthController();
53 changes: 52 additions & 1 deletion src/controllers/CommentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import TripRequestRepository from '../repositories/TripRequestRepository';

import { sendSuccessResponse, sendErrorResponse } from '../utils/sendResponse';


/**
* @description CommentController handles all logic pertaining to comments
*/
Expand Down Expand Up @@ -33,6 +32,7 @@ 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');
Expand All @@ -53,6 +53,57 @@ class CommentController {
return next(err);
}
}

/**
* @description editTripRequestComment method handles the logic to get comment on a trip request
*
* @param {object} req is the request object
*
* @param {object} res is the response object
*
* @param {function} next forwards request to the next middleware in the czall stack
*
* @returns {object} it returns a response that is an object
*/
static async editTripRequestComment(req, res, next) {
try {
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);
} catch (err) {
return next(err);
}
}

/**
* @description deletes the users comment from display
*
* @param {object} req contains the comment details and requester
*
* @param {object} res is the response object
*
* @param {function} next forwards request to the next middleware in the call stack
*
* @returns {object} it returns a response that is an object
*/
static async delete(req, res, next) {
try {
const { uuid } = req.params;
const comment = await CommentRepository.getOne({ uuid });
if (!comment) return sendErrorResponse(res, 404, 'This comment does not exist');
if (comment.dataValues.user_uuid !== req.userData.uuid) return sendErrorResponse(res, 401, 'You are not allowed to perform this action');
await CommentRepository.delete({ uuid });
return sendSuccessResponse(res, 200, { message: 'Comment deleted successful' });
} catch (err) {
return next(err);
}
}
}

export default CommentController;
8 changes: 5 additions & 3 deletions src/controllers/TripRequestController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TripRequestController {
const userTrips = await TripRequestRepository.getAll({ user_uuid: user });
return sendSuccessResponse(res, 200, userTrips);
} catch (err) {
next(err);
return next(err);
}
}

Expand All @@ -46,6 +46,7 @@ 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 });
Expand Down Expand Up @@ -102,14 +103,15 @@ class TripRequestController {
*
* @param {object} res is the response object
*
* @param {function} next forwards request to the next middleware in the call stack
*
* @returns {object} it returns a response that is an object
*/
static oneWayTripCreator(req, res) {
static oneWayTripCreator(req, res, next) {
try {
return sendSuccessResponse(res, 200, 'oneWayTrip still in progress');
} catch (err) {
// return sendErrorResponse(res, 500, 'Internal Server Error');
return next(err);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/database/migrations/20190903210241-create-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ export default {
allowNull: false,
type: Sequelize.STRING
},
deletedAt: {
type: Sequelize.DATE,
field: 'deleted_at'
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
field: 'created_at'
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
field: 'updated_at'
}
}),
// eslint-disable-next-line arrow-parens
Expand Down
20 changes: 0 additions & 20 deletions src/database/seeders/20190829092037-create-trip-request.js

This file was deleted.

Loading

0 comments on commit 757c131

Please sign in to comment.