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