prevented logs on non-existent clients

This commit is contained in:
Carlos Jimenez Ruiz 2022-05-17 15:37:52 +02:00
parent bf8e7eed57
commit ed4718922e
1 changed files with 18 additions and 14 deletions

View File

@ -446,7 +446,7 @@ module.exports = Self => {
const app = require('vn-loopback/server/server');
app.on('started', function() {
let account = app.models.Account;
const account = app.models.Account;
account.observe('before save', async ctx => {
if (ctx.isNewInstance) return;
@ -454,14 +454,16 @@ module.exports = Self => {
});
account.observe('after save', async ctx => {
let changes = ctx.data || ctx.instance;
const changes = ctx.data || ctx.instance;
if (!ctx.isNewInstance && changes) {
let oldData = ctx.hookState.oldInstance;
let hasChanges = oldData.name != changes.name || oldData.active != changes.active;
const oldData = ctx.hookState.oldInstance;
const hasChanges = oldData.name != changes.name || oldData.active != changes.active;
if (!hasChanges) return;
let userId = ctx.options.accessToken.userId;
let logRecord = {
const isClient = await Self.app.models.Client.findById(oldData.id);
if (isClient) {
const userId = ctx.options.accessToken.userId;
const logRecord = {
originFk: oldData.id,
userFk: userId,
action: 'update',
@ -469,8 +471,10 @@ module.exports = Self => {
oldInstance: {name: oldData.name, active: oldData.active},
newInstance: {name: changes.name, active: changes.active}
};
await Self.app.models.ClientLog.create(logRecord);
}
}
});
});
};