-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cooment): Add user/manager comment on trip request
- add POST user comment on trip request - add GET comments on a specific trip request [Start #169258644]
- Loading branch information
1 parent
fe8689d
commit e964364
Showing
15 changed files
with
471 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { comments, users } from '../database/models'; | ||
import Customize from '../helpers/Customize'; | ||
|
||
|
||
/** | ||
* @exports | ||
* @class CommentController | ||
*/ | ||
class CommentController { | ||
/** | ||
* users or managers can comment on a travel request | ||
* @static | ||
* @description POST /api/users/:tripRequestId/comment | ||
* @param {object} req request object | ||
* @param {object} res response object | ||
* @memberof CommentController | ||
* @returns {object} data | ||
*/ | ||
static async createComment(req, res) { | ||
try { | ||
const { tripRequestId } = req.params; | ||
const { id } = req.user; | ||
const { comment } = req.body; | ||
const userComment = await comments.create({ | ||
userId: id, | ||
tripRequestId, | ||
comment | ||
|
||
}); | ||
if (userComment) { | ||
return Customize.successMessage(req, res, 'Your comment was posted successfully', comment, 201); | ||
} | ||
} catch (e) { | ||
return Customize.errorMessage(req, res, 'Server error', 500); | ||
} | ||
} | ||
|
||
/** | ||
* users or managers can view comment thread | ||
* @static | ||
* @description POST /api/users/:tripRequestId/comments | ||
* @param {object} req request object | ||
* @param {object} res response object | ||
* @memberof CommentController | ||
* @returns {object} data | ||
*/ | ||
static async getComments(req, res) { | ||
try { | ||
const { tripRequestId } = req.params; | ||
const tripComments = await comments.findAll({ | ||
attributes: ['comment', 'updatedAt'], | ||
include: [{ | ||
model: users, | ||
attributes: ['firstName', 'lastName'] | ||
}], | ||
where: { | ||
tripRequestId, | ||
} | ||
}); | ||
if (tripComments[0]) { | ||
return Customize.successMessage(req, res, 'All comments about this trip request have been retrieved successfuly!', tripComments, 200); | ||
} | ||
return Customize.errorMessage(req, res, 'No comments for this trip yet!', 200); | ||
} catch (e) { | ||
Customize.errorMessage(req, res, 'Server error', 500); | ||
} | ||
} | ||
} | ||
export default CommentController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('comments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
comment: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'users', | ||
key: 'id', | ||
} | ||
}, | ||
tripRequestId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'tripRequests', | ||
key: 'id', | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('comments'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const comments = sequelize.define('comments', { | ||
comment: DataTypes.STRING, | ||
userId: DataTypes.INTEGER, | ||
tripRequestId: DataTypes.INTEGER, | ||
}, { freezeTableName: true }); | ||
comments.associate = function (models) { | ||
comments.belongsTo(models.users, { | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
}); | ||
|
||
comments.belongsTo(models.tripRequests,{ | ||
foreignKey: 'tripRequestId', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
} | ||
); | ||
}; | ||
return comments; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import express from 'express'; | ||
import Validate from '../../middlewares/Validate'; | ||
import checkInputDataError from '../../middlewares/checkInputDataError'; | ||
import { commentAccess } from '../../middlewares/findUsers'; | ||
import Exists from '../../middlewares/Exists'; | ||
import isUserVerified from '../../middlewares/isUserVerified'; | ||
import AuthenticateToken from '../../helpers/AuthenticateToken'; | ||
import CommentController from '../../controllers/CommentController'; | ||
|
||
const commentRoute = express.Router(); | ||
|
||
/** | ||
* @swagger | ||
* | ||
* /trips/{tripRequestId}/comment: | ||
* post: | ||
* summary: User or manager can post a comment | ||
* description: user/manager post comment | ||
* tags: | ||
* - Comment | ||
* parameters: | ||
* - name: token | ||
* in: header | ||
* required: true | ||
* description: user token | ||
* schema: | ||
* type: string | ||
* example: xxxxxxxxx.xxxxxxxxx.xxxxxxxxxx | ||
* minimum: 1 | ||
* - name: tripRequestId | ||
* in: path | ||
* required: true | ||
* description: Trip request id | ||
* schema: | ||
* type: integer | ||
* example: 1 | ||
* minimum: 1 | ||
* requestBody: | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* comment: | ||
* type: string | ||
* responses: | ||
* 201: | ||
* description: Comment posted successfully | ||
* 400: | ||
* description: Unable to post comment | ||
* 401: | ||
* description: Unauthorized | ||
* 403: | ||
* description: Only requesters and managers can post comments | ||
* 500: | ||
* description: Internal server error | ||
*/ | ||
commentRoute.post('/:tripRequestId/comment', | ||
AuthenticateToken.verifyToken, | ||
isUserVerified, | ||
Validate.commentPostRules(), | ||
checkInputDataError, | ||
Exists.isTripRequestExist, | ||
commentAccess, | ||
CommentController.createComment); | ||
|
||
|
||
/** | ||
* @swagger | ||
* | ||
* /trips/{tripRequestId}/comments: | ||
* get: | ||
* summary: User or manager can get a comment | ||
* description: user/manager get comment | ||
* tags: | ||
* - Comment | ||
* parameters: | ||
* - name: token | ||
* in: header | ||
* required: true | ||
* description: user token | ||
* schema: | ||
* type: string | ||
* example: xxxxxxxxx.xxxxxxxxx.xxxxxxxxxx | ||
* minimum: 1 | ||
* - name: tripRequestId | ||
* in: path | ||
* required: true | ||
* description: Trip request id | ||
* schema: | ||
* type: integer | ||
* example: 1 | ||
* minimum: 1 | ||
* responses: | ||
* 200: | ||
* description: Comments retrieved successfully | ||
* 401: | ||
* description: Unauthorized | ||
* 403: | ||
* description: Only requesters and managers can get comments | ||
* 500: | ||
* description: Internal server error | ||
*/ | ||
commentRoute.get('/:tripRequestId/comments', | ||
AuthenticateToken.verifyToken, | ||
isUserVerified, | ||
Validate.getCommentsRules(), | ||
checkInputDataError, | ||
Exists.isTripRequestExist, | ||
commentAccess, | ||
CommentController.getComments); | ||
|
||
export default commentRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.