tachybase_todo/packages/plugins/audit-logs/src/server/hooks/after-destroy.ts
金昶 b91ca4420b
feat: association field block (#493)
* feat: association field block

* feat: association details block

* feat: template add resource name

* feat: add association calendar

* fix: update yarn.lock

* fix: remove useAssociationNames

* fix: restore useFilterByTk logic

* feat: client doc

* fix: resolveNocobasePackagesAlias

* fix: input textarea readpretty

* feat: styling

* fix: oho & obo

* fix: field-summary component remove to collection manager

* fix: translation

* feat: improve code

* fix(audit-logs): skip when collection does not exist

* feat: m2m

* fix: improve code

* fix: title field

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-06-14 15:46:48 +08:00

48 lines
1.4 KiB
TypeScript

import Application from '@nocobase/server';
import { LOG_TYPE_DESTROY } from '../constants';
export function afterDestroy(app: Application) {
return async (model, options) => {
const db = app.db;
const collection = db.getCollection(model.constructor.name);
if (!collection || !collection.options.logging) {
return;
}
const transaction = options.transaction;
const AuditLog = db.getCollection('auditLogs');
const currentUserId = options?.context?.state?.currentUser?.id;
try {
const changes = [];
Object.keys(model.get()).forEach((key: string) => {
const field = collection.findField((field) => {
return field.name === key || field.options.field === key;
});
if (field) {
changes.push({
field: field.options,
before: model.get(key),
});
}
});
await AuditLog.repository.create({
values: {
type: LOG_TYPE_DESTROY,
collectionName: model.constructor.name,
recordId: model.get(model.constructor.primaryKeyAttribute),
userId: currentUserId,
changes,
},
transaction,
hooks: false,
});
// if (!options.transaction) {
// await transaction.commit();
// }
} catch (error) {
// if (!options.transaction) {
// await transaction.rollback();
// }
}
};
}