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,47 +1,45 @@
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 { collection } = model.constructor;
const db = app.db; if (!collection || !collection.options.logging) {
const collection = db.getCollection(model.constructor.name); return;
if (!collection || !collection.options.logging) { }
return; const transaction = options.transaction;
} const AuditLog = model.constructor.database.getCollection('auditLogs');
const transaction = options.transaction; const currentUserId = options?.context?.state?.currentUser?.id;
const AuditLog = db.getCollection('auditLogs'); try {
const currentUserId = options?.context?.state?.currentUser?.id; const changes = [];
try { const changed = model.changed();
const changes = []; if (changed) {
const changed = model.changed(); changed.forEach((key: string) => {
if (changed) { const field = collection.findField((field) => {
changed.forEach((key: string) => { return field.name === key || field.options.field === key;
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),
});
}
}); });
} if (field && !field.options.hidden) {
await AuditLog.repository.create({ changes.push({
values: { field: field.options,
type: LOG_TYPE_CREATE, after: model.get(key),
collectionName: model.constructor.name, });
recordId: model.get(model.constructor.primaryKeyAttribute), }
createdAt: model.get('createdAt'),
userId: currentUserId,
changes,
},
transaction,
hooks: false,
}); });
} 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);
}
} }

View File

@ -1,47 +1,43 @@
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; if (!collection || !collection.options.logging) {
const collection = db.getCollection(model.constructor.name); return;
if (!collection || !collection.options.logging) { }
return; const transaction = options.transaction;
} const AuditLog = model.constructor.database.getCollection('auditLogs');
const transaction = options.transaction; const currentUserId = options?.context?.state?.currentUser?.id;
const AuditLog = db.getCollection('auditLogs'); try {
const currentUserId = options?.context?.state?.currentUser?.id; const changes = [];
try { Object.keys(model.get()).forEach((key: string) => {
const changes = []; const field = collection.findField((field) => {
Object.keys(model.get()).forEach((key: string) => { return field.name === key || field.options.field === key;
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, await AuditLog.repository.create({
before: model.get(key), values: {
}); type: LOG_TYPE_DESTROY,
} collectionName: model.constructor.name,
}); recordId: model.get(model.constructor.primaryKeyAttribute),
await AuditLog.repository.create({ userId: currentUserId,
values: { changes,
type: LOG_TYPE_DESTROY, },
collectionName: model.constructor.name, transaction,
recordId: model.get(model.constructor.primaryKeyAttribute), hooks: false,
userId: currentUserId, });
changes, // if (!options.transaction) {
}, // await transaction.commit();
transaction, // }
hooks: false, } catch (error) {
}); // if (!options.transaction) {
// if (!options.transaction) { // await transaction.rollback();
// await transaction.commit(); // }
// } }
} catch (error) {
// if (!options.transaction) {
// await transaction.rollback();
// }
}
};
} }

View File

@ -1,56 +1,52 @@
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; if (!collection || !collection.options.logging) {
const collection = db.getCollection(model.constructor.name); return;
if (!collection || !collection.options.logging) { }
return; const changed = model.changed();
} if (!changed) {
const changed = model.changed(); return;
if (!changed) { }
return; const transaction = options.transaction;
} const AuditLog = model.constructor.database.getCollection('auditLogs');
const transaction = options.transaction; const currentUserId = options?.context?.state?.currentUser?.id;
const AuditLog = db.getCollection('auditLogs'); const changes = [];
const currentUserId = options?.context?.state?.currentUser?.id; changed.forEach((key: string) => {
const changes = []; const field = collection.findField((field) => {
changed.forEach((key: string) => { return field.name === key || field.options.field === key;
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),
});
}
}); });
if (!changes.length) { if (field && !field.options.hidden) {
return; changes.push({
} field: field.options,
try { after: model.get(key),
await AuditLog.repository.create({ before: model.previous(key),
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();
// }
} }
}; });
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();
// }
}
} }

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