feat: implement audit logging system with service, controller, and interceptor to track user actions across core services

This commit is contained in:
pradeepkumar
2026-04-28 11:12:25 +05:30
parent 4eda556059
commit 80c6fd1d71
27 changed files with 895 additions and 29 deletions

View File

@@ -1,10 +1,15 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateCmsContentDto, UpdateCmsContentDto } from './dto';
import { AuditService } from '../audit/audit.service';
import { AuditAction } from '../audit/audit.constants';
@Injectable()
export class CmsService {
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private audit: AuditService,
) {}
async findByPage(pageSlug: string) {
return this.prisma.cmsContent.findMany({
@@ -49,10 +54,26 @@ export class CmsService {
if (!existing) {
throw new NotFoundException(`CMS content with ID ${id} not found`);
}
return this.prisma.cmsContent.update({
const updated = await this.prisma.cmsContent.update({
where: { id },
data: dto,
});
if (
typeof dto.isPublished === 'boolean' &&
existing.isPublished !== dto.isPublished
) {
this.audit.log({
action: dto.isPublished
? AuditAction.CMS_BLOG_PUBLISH
: AuditAction.CMS_BLOG_UNPUBLISH,
resourceType: 'CmsContent',
resourceId: id,
metadata: { pageSlug: existing.pageSlug, sectionKey: existing.sectionKey },
});
}
return updated;
}
async remove(id: string) {
@@ -60,6 +81,13 @@ export class CmsService {
if (!existing) {
throw new NotFoundException(`CMS content with ID ${id} not found`);
}
return this.prisma.cmsContent.delete({ where: { id } });
const deleted = await this.prisma.cmsContent.delete({ where: { id } });
this.audit.log({
action: AuditAction.CMS_BLOG_DELETE,
resourceType: 'CmsContent',
resourceId: id,
metadata: { pageSlug: existing.pageSlug, sectionKey: existing.sectionKey },
});
return deleted;
}
}