Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 16x 16x 16x 12x 4x 4x | import { Injectable } from '@nestjs/common';
import { PrismaService } from '@app/modules/prisma/prisma.service';
import { Prisma } from '@prisma/client';
@Injectable()
export class AuditService {
constructor(private prisma: PrismaService) {}
async createAuditLog(params: {
entityType: string;
entityId: number;
action: 'CREATE' | 'UPDATE' | 'DELETE';
oldValue?: any;
newValue?: any;
userId?: number;
}) {
const { entityType, entityId, action, oldValue, newValue, userId } = params;
return this.prisma.client.auditLog.create({
data: {
entityType,
entityId,
action,
oldValue: oldValue ? JSON.parse(JSON.stringify(oldValue)) : null,
newValue: newValue ? JSON.parse(JSON.stringify(newValue)) : null,
userId,
timestamp: new Date(),
},
});
}
async getAuditLogs(params: {
entityType?: string;
entityId?: number;
startDate?: Date;
endDate?: Date;
userId?: number | string;
page?: number;
limit?: number;
}) {
const { entityType, entityId, startDate, endDate, userId, page = 1, limit = 10 } = params;
const where: Prisma.AuditLogWhereInput = {};
Iif (entityType) where.entityType = entityType;
Iif (entityId) where.entityId = entityId;
Iif (userId !== undefined) (where as any).userId = typeof userId === 'string' ? Number(userId) : userId;
Iif (startDate || endDate) {
where.timestamp = {};
Iif (startDate) where.timestamp.gte = startDate;
Iif (endDate) where.timestamp.lte = endDate;
}
const [total, logs] = await Promise.all([
this.prisma.client.auditLog.count({ where }),
this.prisma.client.auditLog.findMany({
where,
orderBy: { timestamp: 'desc' },
skip: (page - 1) * limit,
take: limit,
}),
]);
return {
logs,
pagination: {
total,
page,
limit,
totalPages: Math.ceil(total / limit),
},
};
}
} |