chore: add new access log module
This commit is contained in:
@@ -2,6 +2,8 @@ import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { Not } from "typeorm";
|
||||
|
||||
import { AdminMessage, BlogMessage, CommonMessage } from "../../../common/enums/message.enum";
|
||||
import { UserType } from "../../access-logs/enums/user-type.enum";
|
||||
import { AccessLogService } from "../../access-logs/providers/access-log.service";
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { CreateBlogDto } from "../DTO/create-blog.dto";
|
||||
@@ -19,6 +21,7 @@ export class BlogsService {
|
||||
private readonly blogCommentsRepository: BlogCommentsRepository,
|
||||
private readonly blogCategoriesRepository: BlogCategoriesRepository,
|
||||
private readonly usersService: UsersService,
|
||||
private readonly accessLogService: AccessLogService,
|
||||
) {}
|
||||
|
||||
//*********************************** */
|
||||
@@ -121,6 +124,22 @@ export class BlogsService {
|
||||
|
||||
await this.blogsRepository.save(blog);
|
||||
|
||||
await this.accessLogService.logCreate(
|
||||
"Blog",
|
||||
blog.id,
|
||||
{ blog: createBlogDto.title },
|
||||
{
|
||||
user: { id: authorId },
|
||||
userType: UserType.ADMIN,
|
||||
actionDescription: `Admin created blog: ${createBlogDto.title}`,
|
||||
metadata: {
|
||||
categoryId: createBlogDto.categoryId,
|
||||
slug: slug,
|
||||
authorId: authorId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
blog,
|
||||
@@ -129,7 +148,7 @@ export class BlogsService {
|
||||
|
||||
//*********************************** */
|
||||
|
||||
async updateBlog(id: string, updateBlogDto: UpdateBlogDto) {
|
||||
async updateBlog(id: string, updateBlogDto: UpdateBlogDto, userId: string) {
|
||||
const blog = await this.findBlogById(id);
|
||||
|
||||
if (updateBlogDto.title) {
|
||||
@@ -153,6 +172,21 @@ export class BlogsService {
|
||||
slug: this.generateSlug(updateBlogDto.slug ?? blog.slug),
|
||||
});
|
||||
|
||||
await this.accessLogService.logUpdate(
|
||||
"Blog",
|
||||
blog.id,
|
||||
{ blog: blog.title },
|
||||
{ blog: updateBlogDto.title },
|
||||
{
|
||||
user: { id: userId },
|
||||
userType: UserType.ADMIN,
|
||||
actionDescription: `Admin updated blog: ${blog.title}`,
|
||||
metadata: {
|
||||
categoryId: updateBlogDto.categoryId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
blog,
|
||||
@@ -168,6 +202,20 @@ export class BlogsService {
|
||||
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
|
||||
|
||||
await this.blogsRepository.softDelete({ id });
|
||||
|
||||
await this.accessLogService.logDelete(
|
||||
"Blog",
|
||||
blog.id,
|
||||
{ blog: blog.title },
|
||||
{
|
||||
user: { id: userId },
|
||||
userType: UserType.ADMIN,
|
||||
actionDescription: `Admin deleted blog: ${blog.title}`,
|
||||
metadata: {
|
||||
categoryId: blog.category.id,
|
||||
},
|
||||
},
|
||||
);
|
||||
return {
|
||||
message: CommonMessage.DELETED,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user