04a17d2775
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
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",
|
||
"recurring.delete",
|
||
] 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);
|