refactor(plugin-audit-log): remove useless function wrap (#3237)

This commit is contained in:
Junyi 2023-12-20 23:18:02 +08:00 committed by GitHub
parent e7b9737920
commit 978c4c5f61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 128 additions and 138 deletions

View File

@ -1,18 +1,15 @@
import Application from '@nocobase/server';
import { LOG_TYPE_CREATE } from '../constants';
export function afterCreate(app: Application) {
return async (model, options) => {
export async function afterCreate(model, options) {
if (options.logging === false) {
return;
}
const db = app.db;
const collection = db.getCollection(model.constructor.name);
const { collection } = model.constructor;
if (!collection || !collection.options.logging) {
return;
}
const transaction = options.transaction;
const AuditLog = db.getCollection('auditLogs');
const AuditLog = model.constructor.database.getCollection('auditLogs');
const currentUserId = options?.context?.state?.currentUser?.id;
try {
const changes = [];
@ -42,6 +39,7 @@ export function afterCreate(app: Application) {
transaction,
hooks: false,
});
} catch (error) {}
};
} catch (error) {
// console.error(error);
}
}

View File

@ -1,15 +1,12 @@
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);
export async function afterDestroy(model, options) {
const { collection } = model.constructor;
if (!collection || !collection.options.logging) {
return;
}
const transaction = options.transaction;
const AuditLog = db.getCollection('auditLogs');
const AuditLog = model.constructor.database.getCollection('auditLogs');
const currentUserId = options?.context?.state?.currentUser?.id;
try {
const changes = [];
@ -43,5 +40,4 @@ export function afterDestroy(app: Application) {
// await transaction.rollback();
// }
}
};
}

View File

@ -1,10 +1,7 @@
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);
export async function afterUpdate(model, options) {
const { collection } = model.constructor;
if (!collection || !collection.options.logging) {
return;
}
@ -13,7 +10,7 @@ export function afterUpdate(app: Application) {
return;
}
const transaction = options.transaction;
const AuditLog = db.getCollection('auditLogs');
const AuditLog = model.constructor.database.getCollection('auditLogs');
const currentUserId = options?.context?.state?.currentUser?.id;
const changes = [];
changed.forEach((key: string) => {
@ -52,5 +49,4 @@ export function afterUpdate(app: Application) {
// await transaction.rollback();
// }
}
};
}

View File

@ -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() {