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'; import { LOG_TYPE_CREATE } from '../constants';
export function afterCreate(app: Application) { export async function afterCreate(model, options) {
return async (model, options) => {
if (options.logging === false) { if (options.logging === false) {
return; return;
} }
const db = app.db; const { collection } = model.constructor;
const collection = db.getCollection(model.constructor.name);
if (!collection || !collection.options.logging) { if (!collection || !collection.options.logging) {
return; return;
} }
const transaction = options.transaction; const transaction = options.transaction;
const AuditLog = db.getCollection('auditLogs'); const AuditLog = model.constructor.database.getCollection('auditLogs');
const currentUserId = options?.context?.state?.currentUser?.id; const currentUserId = options?.context?.state?.currentUser?.id;
try { try {
const changes = []; const changes = [];
@ -42,6 +39,7 @@ export function afterCreate(app: Application) {
transaction, transaction,
hooks: false, 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'; import { LOG_TYPE_DESTROY } from '../constants';
export function afterDestroy(app: Application) { export async function afterDestroy(model, options) {
return async (model, options) => { const { collection } = model.constructor;
const db = app.db;
const collection = db.getCollection(model.constructor.name);
if (!collection || !collection.options.logging) { if (!collection || !collection.options.logging) {
return; return;
} }
const transaction = options.transaction; const transaction = options.transaction;
const AuditLog = db.getCollection('auditLogs'); const AuditLog = model.constructor.database.getCollection('auditLogs');
const currentUserId = options?.context?.state?.currentUser?.id; const currentUserId = options?.context?.state?.currentUser?.id;
try { try {
const changes = []; const changes = [];
@ -43,5 +40,4 @@ export function afterDestroy(app: Application) {
// await transaction.rollback(); // await transaction.rollback();
// } // }
} }
};
} }

View File

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

View File

@ -4,9 +4,9 @@ import { afterCreate, afterDestroy, afterUpdate } from './hooks';
export default class PluginActionLogs extends Plugin { export default class PluginActionLogs extends Plugin {
async beforeLoad() { async beforeLoad() {
this.db.on('afterCreate', afterCreate(this.app)); this.db.on('afterCreate', afterCreate);
this.db.on('afterUpdate', afterUpdate(this.app)); this.db.on('afterUpdate', afterUpdate);
this.db.on('afterDestroy', afterDestroy(this.app)); this.db.on('afterDestroy', afterDestroy);
} }
async load() { async load() {