Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow super admins to edit product comments #1324

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/modules/products/products.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ export class ProductsController {
return this.productsService.addCommentToProduct(productId, commentDto, user.sub);
}

@ApiBearerAuth()
@UseGuards(SuperAdminGuard)
@Post('organisations/:productId/comments/:commentId')
@ApiBearerAuth()
@ApiOperation({ summary: 'Edits a comment for a product' })
@ApiParam({ name: 'id', description: 'organisation ID', example: '870ccb14-d6b0-4a50-b459-9895af803i89' })
@ApiParam({ name: 'productId', description: 'product ID', example: '126ccb14-d6b0-4a50-b459-9895af803h6y' })
@ApiBody({ type: AddCommentDto, description: 'Comment to be edited' })
@ApiResponse({ status: 201, description: 'Comment updated successfully' })
@ApiResponse({ status: 400, description: 'Bad request' })
@ApiResponse({ status: 404, description: 'Not found' })
@ApiResponse({ status: 500, description: 'Internal server error' })
async editProductComment(
@Param('productId') productId: string,
@Param('commentId') commentId: string,
@Body() commentDto: AddCommentDto,
@Req() req: any
) {
const user = req.user;
return this.productsService.editProductComment(productId, commentId, commentDto, user.sub);
}

@ApiBearerAuth()
@UseGuards(OwnershipGuard)
@Get('organisations/:productId/stock')
Expand Down
35 changes: 35 additions & 0 deletions src/modules/products/products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,41 @@ export class ProductsService {
};
}

async editProductComment(productId: string, commentId: string, commentDto: AddCommentDto, userId: string) {
const c = commentDto;

const product = await this.productRepository.findOne({ where: { id: productId } });

if (!product) {
throw new CustomHttpException(SYS_MSG.PRODUCT_NOT_FOUND, HttpStatus.NOT_FOUND);
}

const comment = await this.commentRepository.findOne({ where: { id: commentId } });

if (!comment) {
throw new CustomHttpException(SYS_MSG.COMMENT_NOT_FOUND, HttpStatus.NOT_FOUND);
}

if (comment.user.id !== userId) {
throw new CustomHttpException(SYS_MSG.COMMENT_NOT_FOUND, HttpStatus.NOT_FOUND);
}

await this.commentRepository.update(commentId, { comment: c.comment });

const responsePayload = {
id: comment.id,
product_id: product.id,
comment: c.comment,
user_id: userId,
created_at: new Date(),
};

return {
message: SYS_MSG.COMMENT_EDITED,
data: responsePayload,
};
}

async getProductStock(productId: string) {
const product = await this.productRepository.findOne({ where: { id: productId } });
if (!product) {
Expand Down
2 changes: 2 additions & 0 deletions src/shared/constants/SystemMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export const REQUEST_SUCCESSFUL = 'Request completed successfully';
export const PAYMENT_NOTFOUND = 'Payment plan not found';
export const PRODUCT_NOT_FOUND = 'Product not found!';
export const COMMENT_CREATED = 'Comment added successfully';
export const COMMENT_NOT_FOUND = 'Comment not found';
export const COMMENT_EDITED = 'Comment edited successfully';
export const ORG_UPDATE = 'Organisation updated successfully';
export const ORG_MEMBER_NOT_FOUND = 'Member not found';
export const ORG_MEMBER_DOES_NOT_BELONG = 'Member does not belong to the specified organisation';
Expand Down