chore: add comment reply for review and blog comment
This commit is contained in:
@@ -1,20 +1,14 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { DataSource, IsNull, Not } from "typeorm";
|
||||
import { Not } from "typeorm";
|
||||
|
||||
import { AdminMessage, BlogMessage, CommonMessage } from "../../../common/enums/message.enum";
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { NotificationQueue } from "../../notifications/queue/notification.queue";
|
||||
import { AdminsService } from "../../users/providers/admins.service";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { BlogCommentQueryDto } from "../DTO/blog-comment-query.dto";
|
||||
import { CreateBlogDto } from "../DTO/create-blog.dto";
|
||||
import { CreateBlogCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateCommentDto } from "../DTO/create-comment.dto";
|
||||
import { BlogListSearchQueryDto, BlogSearchQueryDto } from "../DTO/search-blog-query.dto";
|
||||
import { UpdateBlogDto } from "../DTO/update-blog.dto";
|
||||
import { UpdateBlogCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateCommentStatusDto } from "../DTO/update-comment-status.dto";
|
||||
import { CommentStatus } from "../enums/comment-status.enum";
|
||||
import { BlogCategoriesRepository } from "../repositories/blog-categories.repository";
|
||||
import { BlogCommentsRepository } from "../repositories/blog-comments.repository";
|
||||
import { BlogsRepository } from "../repositories/blogs.repository";
|
||||
@@ -25,9 +19,6 @@ export class BlogsService {
|
||||
private readonly blogCommentsRepository: BlogCommentsRepository,
|
||||
private readonly blogCategoriesRepository: BlogCategoriesRepository,
|
||||
private readonly usersService: UsersService,
|
||||
private readonly adminsService: AdminsService,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly notificationQueue: NotificationQueue,
|
||||
) {}
|
||||
|
||||
//*********************************** */
|
||||
@@ -235,80 +226,6 @@ export class BlogsService {
|
||||
const [blogs, count] = await this.blogsRepository.getBlogListForAdmin(queryDto);
|
||||
return { blogs, count, paginate: true };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async createComment(blogId: string, createCommentDto: CreateCommentDto, userId: string) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
const blog = await queryRunner.manager.findOne(this.blogsRepository.target, { where: { id: blogId, deletedAt: IsNull() } });
|
||||
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
|
||||
|
||||
const user = await this.usersService.findOneByIdWithQueryRunner(userId, queryRunner);
|
||||
|
||||
const comment = queryRunner.manager.create(this.blogCommentsRepository.target, {
|
||||
...createCommentDto,
|
||||
blog,
|
||||
user,
|
||||
status: CommentStatus.PENDING,
|
||||
});
|
||||
|
||||
await queryRunner.manager.save(this.blogCommentsRepository.target, comment);
|
||||
|
||||
const superAdmin = await this.adminsService.getSuperAdmins(queryRunner);
|
||||
|
||||
for (const admin of superAdmin) {
|
||||
await this.notificationQueue.addNewBlogCommentNotification(admin.id, {
|
||||
userPhone: admin.phone,
|
||||
userEmail: admin.email,
|
||||
blogTitle: blog.title,
|
||||
fullName: `${user.firstName} ${user.lastName}`,
|
||||
});
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
comment,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async updateCommentStatus(commentId: string, updateCommentStatusDto: UpdateCommentStatusDto) {
|
||||
const comment = await this.blogCommentsRepository.findOneById(commentId);
|
||||
if (!comment) throw new BadRequestException(BlogMessage.COMMENT_NOT_FOUND);
|
||||
|
||||
comment.status = updateCommentStatusDto.status;
|
||||
await this.blogCommentsRepository.save(comment);
|
||||
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
comment,
|
||||
};
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getBlogCommentsForAdmin(blogId: string) {
|
||||
const comments = await this.blogCommentsRepository.findBlogCommentsForAdmin(blogId);
|
||||
return { comments };
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
|
||||
async getAllCommentForAdmin(queryDto: BlogCommentQueryDto) {
|
||||
const [comments, count] = await this.blogCommentsRepository.getAllCommentsForAdmin(queryDto);
|
||||
return { comments, count, paginate: true };
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private generateSlug(slug: string): string {
|
||||
|
||||
Reference in New Issue
Block a user