60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|||
|
|
import { Prisma } from '@prisma/client';
|
||
|
|
import { PrismaService } from '../prisma/prisma.service';
|
||
|
|
|
||
|
|
type AuditInput = {
|
||
|
|
action: string;
|
||
|
|
entityType: string;
|
||
|
|
entityId?: string;
|
||
|
|
tenantId?: string;
|
||
|
|
userId?: string;
|
||
|
|
before?: Prisma.InputJsonValue;
|
||
|
|
after?: Prisma.InputJsonValue;
|
||
|
|
meta?: Prisma.InputJsonValue;
|
||
|
|
};
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class AuditService {
|
||
|
|
constructor(private readonly prisma: PrismaService) {}
|
||
|
|
|
||
|
|
log(input: AuditInput) {
|
||
|
|
return this.prisma.auditLog.create({
|
||
|
|
data: {
|
||
|
|
action: input.action,
|
||
|
|
entityType: input.entityType,
|
||
|
|
entityId: input.entityId,
|
||
|
|
tenantId: input.tenantId,
|
||
|
|
userId: input.userId,
|
||
|
|
before: input.before,
|
||
|
|
after: input.after,
|
||
|
|
meta: input.meta,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
findAll(limit = 50) {
|
||
|
|
return this.prisma.auditLog.findMany({
|
||
|
|
take: Math.min(limit, 200),
|
||
|
|
orderBy: { createdAt: 'desc' },
|
||
|
|
include: {
|
||
|
|
user: { select: { id: true, name: true, email: true } },
|
||
|
|
tenant: { select: { id: true, slug: true, nameFa: true } },
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async findOne(id: string) {
|
||
|
|
const row = await this.prisma.auditLog.findUnique({
|
||
|
|
where: { id },
|
||
|
|
include: {
|
||
|
|
user: { select: { id: true, name: true, email: true } },
|
||
|
|
tenant: { select: { id: true, slug: true, nameFa: true } },
|
||
|
|
},
|
||
|
|
});
|
||
|
|
if (!row) {
|
||
|
|
throw new NotFoundException('لاگ یافت نشد');
|
||
|
|
}
|
||
|
|
return row;
|
||
|
|
}
|
||
|
|
}
|