You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { Prisma } from "@prisma/client";
import { prisma } from "@/lib/prisma";
import { writeAlert } from "@/modules/alert/service";
/**
* 审计日志写入task-094
* 失败仅 log不阻断主流程
*/
export async function writeAudit(
action: string,
actorId: string,
resource?: string,
detail?: Record<string, unknown>,
): Promise<void> {
try {
await prisma.auditLog.create({
data: {
action,
actorId,
resource: resource ?? null,
detailJson: detail
? (detail as Prisma.InputJsonValue)
: undefined,
},
});
} catch (error) {
console.error(`[audit] 写入 ${action} 失败:`, error);
}
}
/** 高危安全事件audit + SECURITY 预警task-105 */
export async function recordSecurityEvent(
action: string,
actorId: string,
resource: string | undefined,
detail: Record<string, unknown>,
): Promise<void> {
await writeAudit(action, actorId, resource, detail);
await writeAlert("SECURITY", {
quoteId: typeof detail.quote_id === "string" ? detail.quote_id : undefined,
detail: { ...detail, audit_action: action },
});
}