51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { Prisma } from "@/app/generated/prisma/client";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
/**
|
|
* Helper untuk catat aksi admin ke `AdminActionLog`. Append-only, idempotent
|
|
* terhadap kegagalan: kalau insert log gagal, jangan blok action — cuma
|
|
* console.error supaya monitoring bisa pick up.
|
|
*
|
|
* Pemakaian (di akhir admin action, setelah success):
|
|
* ```ts
|
|
* await auditLog.record({
|
|
* admin: { id: session.user.id, email: session.user.email },
|
|
* action: "REFUND_APPROVE",
|
|
* entityType: "Refund",
|
|
* entityId: refundId,
|
|
* payload: { adminNote },
|
|
* });
|
|
* ```
|
|
*/
|
|
export const auditLog = {
|
|
async record(input: {
|
|
admin: { id: string; email: string };
|
|
action: string;
|
|
entityType: string;
|
|
entityId: string;
|
|
payload?: Record<string, unknown>;
|
|
}): Promise<void> {
|
|
try {
|
|
await prisma.adminActionLog.create({
|
|
data: {
|
|
adminId: input.admin.id,
|
|
adminEmail: input.admin.email,
|
|
action: input.action,
|
|
entityType: input.entityType,
|
|
entityId: input.entityId,
|
|
payload: input.payload
|
|
? (input.payload as Prisma.InputJsonValue)
|
|
: Prisma.JsonNull,
|
|
},
|
|
});
|
|
} catch (err) {
|
|
console.error("[audit-log] gagal record action", {
|
|
action: input.action,
|
|
entityType: input.entityType,
|
|
entityId: input.entityId,
|
|
err,
|
|
});
|
|
}
|
|
},
|
|
};
|