From 978c4c5f61cefaf7381f1fb6288fbeef7125bbce Mon Sep 17 00:00:00 2001 From: Junyi Date: Wed, 20 Dec 2023 23:18:02 +0800 Subject: [PATCH] refactor(plugin-audit-log): remove useless function wrap (#3237) --- .../src/server/hooks/after-create.ts | 82 ++++++++-------- .../src/server/hooks/after-destroy.ts | 82 ++++++++-------- .../src/server/hooks/after-update.ts | 96 +++++++++---------- .../plugin-audit-logs/src/server/index.ts | 6 +- 4 files changed, 128 insertions(+), 138 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-create.ts b/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-create.ts index 0cafdfad9..4f6f480c2 100644 --- a/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-create.ts +++ b/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-create.ts @@ -1,47 +1,45 @@ -import Application from '@nocobase/server'; import { LOG_TYPE_CREATE } from '../constants'; -export function afterCreate(app: Application) { - return async (model, options) => { - if (options.logging === false) { - return; - } - 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 = []; - const changed = model.changed(); - if (changed) { - changed.forEach((key: string) => { - const field = collection.findField((field) => { - return field.name === key || field.options.field === key; - }); - if (field && !field.options.hidden) { - changes.push({ - field: field.options, - after: model.get(key), - }); - } +export async function afterCreate(model, options) { + if (options.logging === false) { + return; + } + const { collection } = model.constructor; + if (!collection || !collection.options.logging) { + return; + } + const transaction = options.transaction; + const AuditLog = model.constructor.database.getCollection('auditLogs'); + const currentUserId = options?.context?.state?.currentUser?.id; + try { + const changes = []; + const changed = model.changed(); + if (changed) { + changed.forEach((key: string) => { + const field = collection.findField((field) => { + return field.name === key || field.options.field === key; }); - } - await AuditLog.repository.create({ - values: { - type: LOG_TYPE_CREATE, - collectionName: model.constructor.name, - recordId: model.get(model.constructor.primaryKeyAttribute), - createdAt: model.get('createdAt'), - userId: currentUserId, - changes, - }, - transaction, - hooks: false, + if (field && !field.options.hidden) { + changes.push({ + field: field.options, + after: model.get(key), + }); + } }); - } catch (error) {} - }; + } + await AuditLog.repository.create({ + values: { + type: LOG_TYPE_CREATE, + collectionName: model.constructor.name, + recordId: model.get(model.constructor.primaryKeyAttribute), + createdAt: model.get('createdAt'), + userId: currentUserId, + changes, + }, + transaction, + hooks: false, + }); + } catch (error) { + // console.error(error); + } } diff --git a/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-destroy.ts b/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-destroy.ts index 42677f933..4a7b8fa59 100644 --- a/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-destroy.ts +++ b/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-destroy.ts @@ -1,47 +1,43 @@ -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; +export async function afterDestroy(model, options) { + const { collection } = model.constructor; + if (!collection || !collection.options.logging) { + return; + } + const transaction = options.transaction; + const AuditLog = model.constructor.database.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), }); - 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(); - // } - } - }; + } + }); + 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(); + // } + } } diff --git a/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-update.ts b/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-update.ts index 693b4630a..72407e63d 100644 --- a/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-update.ts +++ b/packages/plugins/@nocobase/plugin-audit-logs/src/server/hooks/after-update.ts @@ -1,56 +1,52 @@ -import Application from '@nocobase/server'; import { LOG_TYPE_UPDATE } from '../constants'; -export function afterUpdate(app: Application) { - return async (model, options) => { - const db = app.db; - const collection = db.getCollection(model.constructor.name); - if (!collection || !collection.options.logging) { - return; - } - const changed = model.changed(); - if (!changed) { - return; - } - const transaction = options.transaction; - const AuditLog = db.getCollection('auditLogs'); - const currentUserId = options?.context?.state?.currentUser?.id; - const changes = []; - changed.forEach((key: string) => { - const field = collection.findField((field) => { - return field.name === key || field.options.field === key; - }); - if (field && !field.options.hidden) { - changes.push({ - field: field.options, - after: model.get(key), - before: model.previous(key), - }); - } +export async function afterUpdate(model, options) { + const { collection } = model.constructor; + if (!collection || !collection.options.logging) { + return; + } + const changed = model.changed(); + if (!changed) { + return; + } + const transaction = options.transaction; + const AuditLog = model.constructor.database.getCollection('auditLogs'); + const currentUserId = options?.context?.state?.currentUser?.id; + const changes = []; + changed.forEach((key: string) => { + const field = collection.findField((field) => { + return field.name === key || field.options.field === key; }); - if (!changes.length) { - return; - } - try { - await AuditLog.repository.create({ - values: { - type: LOG_TYPE_UPDATE, - collectionName: model.constructor.name, - recordId: model.get(model.constructor.primaryKeyAttribute), - createdAt: model.get('updatedAt'), - userId: currentUserId, - changes, - }, - transaction, - hooks: false, + if (field && !field.options.hidden) { + changes.push({ + field: field.options, + after: model.get(key), + before: model.previous(key), }); - // if (!options.transaction) { - // await transaction.commit(); - // } - } catch (error) { - // if (!options.transaction) { - // await transaction.rollback(); - // } } - }; + }); + if (!changes.length) { + return; + } + try { + await AuditLog.repository.create({ + values: { + type: LOG_TYPE_UPDATE, + collectionName: model.constructor.name, + recordId: model.get(model.constructor.primaryKeyAttribute), + createdAt: model.get('updatedAt'), + userId: currentUserId, + changes, + }, + transaction, + hooks: false, + }); + // if (!options.transaction) { + // await transaction.commit(); + // } + } catch (error) { + // if (!options.transaction) { + // await transaction.rollback(); + // } + } } diff --git a/packages/plugins/@nocobase/plugin-audit-logs/src/server/index.ts b/packages/plugins/@nocobase/plugin-audit-logs/src/server/index.ts index 9b91143fa..ce127e91f 100644 --- a/packages/plugins/@nocobase/plugin-audit-logs/src/server/index.ts +++ b/packages/plugins/@nocobase/plugin-audit-logs/src/server/index.ts @@ -4,9 +4,9 @@ import { afterCreate, afterDestroy, afterUpdate } from './hooks'; export default class PluginActionLogs extends Plugin { async beforeLoad() { - this.db.on('afterCreate', afterCreate(this.app)); - this.db.on('afterUpdate', afterUpdate(this.app)); - this.db.on('afterDestroy', afterDestroy(this.app)); + this.db.on('afterCreate', afterCreate); + this.db.on('afterUpdate', afterUpdate); + this.db.on('afterDestroy', afterDestroy); } async load() {