diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 5ed777ab5..89bda7aba 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -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,22 +454,26 @@ 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 = { - originFk: oldData.id, - userFk: userId, - action: 'update', - changedModel: 'Account', - oldInstance: {name: oldData.name, active: oldData.active}, - newInstance: {name: changes.name, active: changes.active} - }; - await Self.app.models.ClientLog.create(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', + changedModel: 'Account', + oldInstance: {name: oldData.name, active: oldData.active}, + newInstance: {name: changes.name, active: changes.active} + }; + + await Self.app.models.ClientLog.create(logRecord); + } } }); });