chore: add notif service for new blog and service comment
This commit is contained in:
@@ -9,10 +9,10 @@ import { BlogsService } from "./providers/blogs.service";
|
||||
import { BlogCategoriesRepository } from "./repositories/blog-categories.repository";
|
||||
import { BlogCommentsRepository } from "./repositories/blog-comments.repository";
|
||||
import { BlogsRepository } from "./repositories/blogs.repository";
|
||||
import { NotificationModule } from "../notifications/notifications.module";
|
||||
import { UsersModule } from "../users/users.module";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Blog, BlogComment, BlogCategory]), UsersModule],
|
||||
imports: [TypeOrmModule.forFeature([Blog, BlogComment, BlogCategory]), UsersModule, NotificationModule],
|
||||
controllers: [BlogsController],
|
||||
providers: [BlogsService, BlogsRepository, BlogCommentsRepository, BlogCategoriesRepository],
|
||||
exports: [BlogsService],
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { Not } from "typeorm";
|
||||
import { DataSource, IsNull, Not } from "typeorm";
|
||||
|
||||
import { AdminMessage, BlogMessage, CommonMessage } from "../../../common/enums/message.enum";
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { NotificationsService } from "../../notifications/providers/notifications.service";
|
||||
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";
|
||||
@@ -23,6 +25,9 @@ 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 notificationsService: NotificationsService,
|
||||
) {}
|
||||
|
||||
//*********************************** */
|
||||
@@ -233,23 +238,53 @@ export class BlogsService {
|
||||
//*********************************** */
|
||||
|
||||
async createComment(blogId: string, createCommentDto: CreateCommentDto, userId: string) {
|
||||
const blog = await this.findBlogById(blogId);
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
const { user } = await this.usersService.findOneById(userId);
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
const comment = this.blogCommentsRepository.create({
|
||||
...createCommentDto,
|
||||
blog,
|
||||
user,
|
||||
status: CommentStatus.PENDING,
|
||||
});
|
||||
const blog = await queryRunner.manager.findOne(this.blogsRepository.target, { where: { id: blogId, deletedAt: IsNull() } });
|
||||
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
|
||||
|
||||
await this.blogCommentsRepository.save(comment);
|
||||
const user = await this.usersService.findOneByIdWithQueryRunner(userId, queryRunner);
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
comment,
|
||||
};
|
||||
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();
|
||||
|
||||
for (const admin of superAdmin) {
|
||||
await this.notificationsService.createNewBlogCommentNotification(
|
||||
admin.id,
|
||||
{
|
||||
userPhone: user.phone,
|
||||
userEmail: user.email,
|
||||
blogTitle: blog.title,
|
||||
fullName: `${user.firstName} ${user.lastName}`,
|
||||
},
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
comment,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user