95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Body,
|
|
Query,
|
|
UseGuards,
|
|
Param,
|
|
} from "@nestjs/common";
|
|
import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger";
|
|
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
|
|
import { RolesGuard } from "../common/guards/roles.guard";
|
|
import { Roles } from "../common/decorators/roles.decorator";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
|
|
@ApiTags("Admin - System Audit & History")
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles("Admin", "SUPER_ADMIN")
|
|
@Controller("admin/audit-logs")
|
|
export class AuditLogsController {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: "لیست لاگهای تغییرات دیتابیس و سیستم" })
|
|
async findAll(@Query() query: any) {
|
|
const page = Number(query.page) || 1;
|
|
const limit = Number(query.limit) || 30;
|
|
const skip = (page - 1) * limit;
|
|
|
|
const where: any = {};
|
|
if (query.entity) where.entity = query.entity;
|
|
if (query.action) where.action = query.action;
|
|
if (query.department) where.department = query.department;
|
|
if (query.search) {
|
|
where.OR = [
|
|
{ summary: { contains: query.search, mode: "insensitive" } },
|
|
{ actor: { contains: query.search, mode: "insensitive" } },
|
|
{ entityId: { contains: query.search, mode: "insensitive" } },
|
|
];
|
|
}
|
|
|
|
const [total, items] = await Promise.all([
|
|
(this.prisma as any).auditLog.count({ where }),
|
|
(this.prisma as any).auditLog.findMany({
|
|
where,
|
|
skip,
|
|
take: limit,
|
|
orderBy: { createdAt: "desc" },
|
|
}),
|
|
]);
|
|
|
|
return {
|
|
success: true,
|
|
data: items,
|
|
meta: {
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(total / limit),
|
|
},
|
|
};
|
|
}
|
|
|
|
@Get("recent")
|
|
@ApiOperation({ summary: "آخرین رخدادهای سیستم برای ویجت داشبورد" })
|
|
async getRecent(@Query("limit") limitParam?: string) {
|
|
const limit = Number(limitParam) || 10;
|
|
const items = await (this.prisma as any).auditLog.findMany({
|
|
take: limit,
|
|
orderBy: { createdAt: "desc" },
|
|
});
|
|
return { success: true, data: items };
|
|
}
|
|
|
|
@Post()
|
|
@ApiOperation({ summary: "ثبت لاگ جدید در تاریخچه سیستم" })
|
|
async create(@Body() body: any) {
|
|
const item = await (this.prisma as any).auditLog.create({
|
|
data: {
|
|
entity: body.entity,
|
|
entityId: body.entityId ? String(body.entityId) : null,
|
|
action: body.action || "UPDATE",
|
|
department: body.department || "system",
|
|
actor: body.actor || "ADMIN",
|
|
summary: body.summary,
|
|
oldValues: body.oldValues || null,
|
|
newValues: body.newValues || null,
|
|
metadata: body.metadata || null,
|
|
},
|
|
});
|
|
return { success: true, data: item };
|
|
}
|
|
}
|