Files
butcem/src/models/audit-log.ts
T

55 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { model, models, Schema, type InferSchemaType, type Model } from "mongoose";
/**
* Denetim kaydı (yalnızca eklenir, güncellenmez). Bölüm 7.2: hane ayarları, üye değişiklikleri,
* silmeler, aynî borç kapatma ve mutabakat düzeltmeleri; ayrıca admin kullanıcı işlemleri.
*/
export const AUDIT_ACTIONS = [
"household.create",
"household.settings.update",
"household.member.add",
"household.member.role",
"household.member.remove",
"user.create",
"user.update",
"user.role",
"user.password.reset",
"user.ban",
"user.unban",
"user.remove",
"account.create",
"account.update",
"account.archive",
"account.unarchive",
"account.reconcile",
"category.archive",
"category.unarchive",
"category.move",
"transaction.delete",
"transaction.restore",
] as const;
export type AuditAction = (typeof AUDIT_ACTIONS)[number];
const auditLogSchema = new Schema(
{
/** Hane kapsamındaki işlemlerde dolu; sistem (admin) işlemlerinde null. */
householdId: { type: Schema.Types.ObjectId, default: null },
actorId: { type: Schema.Types.ObjectId, required: true },
action: { type: String, enum: AUDIT_ACTIONS, required: true },
targetType: { type: String, required: true },
targetId: { type: String, required: true },
/** Değişen alanlar: { alan: { from, to } } veya özet bilgi. Şifre vb. sır asla yazılmaz. */
diff: { type: Schema.Types.Mixed, default: null },
at: { type: Date, default: () => new Date() },
},
{ collection: "auditLogs", versionKey: false },
);
auditLogSchema.index({ householdId: 1, at: -1 });
auditLogSchema.index({ actorId: 1, at: -1 });
export type AuditLog = InferSchemaType<typeof auditLogSchema>;
export const AuditLogModel: Model<AuditLog> =
(models.AuditLog as Model<AuditLog> | undefined) ?? model<AuditLog>("AuditLog", auditLogSchema);