mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-03 12:47:44 +08:00
refactor(plugin-audit-log): remove useless function wrap (#3237)
This commit is contained in:
parent
e7b9737920
commit
978c4c5f61
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user