Skip to content

Commit

Permalink
Merge pull request #1324 from victoribironke/feat/allow-super-admin-edit
Browse files Browse the repository at this point in the history
feat: allow super admins to edit product comments
  • Loading branch information
TheCodeGhinux authored Mar 1, 2025
2 parents 05b3be2 + 9231952 commit 72c6a26
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/modules/products/products.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,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/:orgId/products/: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 @@ -239,6 +239,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 @@ -47,6 +47,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

0 comments on commit 72c6a26

Please sign in to comment.