// observers/AuditObserver.js
import AuditLog from '../models/AuditLog.js';
import { getCurrentUser, getClientIp } from '../lib/context.js';
export default class AuditObserver {
async created(model) {
await this._log(model, 'created', null, model.attributes);
}
async updated(model) {
await this._log(model, 'updated', model.getOriginal(), model.getDirty());
}
async deleted(model) {
await this._log(model, 'deleted', model.attributes, null);
}
async _log(model, event, oldValues, newValues) {
const user = getCurrentUser();
await AuditLog.create({
auditable_type: model.constructor.name,
auditable_id: model.getKey(),
event,
user_id: user?.id ?? null,
old_values: oldValues,
new_values: newValues,
ip_address: getClientIp(),
});
}
}