From f87a7664e5312a291284f26c50f312311bd63e1e Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 22 Oct 2020 11:24:53 +0200 Subject: [PATCH 1/2] Added loggable utils --- .../chat/spec/sendCheckingPresence.spec.js | 2 +- loopback/util/log.js | 83 +++++++++++++++++++ .../back/methods/ticket/componentUpdate.js | 39 +++------ 3 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 loopback/util/log.js diff --git a/back/methods/chat/spec/sendCheckingPresence.spec.js b/back/methods/chat/spec/sendCheckingPresence.spec.js index 1523cb1d0..4fbd368d4 100644 --- a/back/methods/chat/spec/sendCheckingPresence.spec.js +++ b/back/methods/chat/spec/sendCheckingPresence.spec.js @@ -38,7 +38,7 @@ describe('chat sendCheckingPresence()', () => { expect(response.statusCode).toEqual(200); expect(response.message).toEqual('Fake notification sent'); - expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', '@HankPym => I changed something'); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', '@HankPym ➔ I changed something'); }); it(`should call to send() method with the worker username when the worker is working`, async() => { diff --git a/loopback/util/log.js b/loopback/util/log.js new file mode 100644 index 000000000..59a4e4679 --- /dev/null +++ b/loopback/util/log.js @@ -0,0 +1,83 @@ +/** + * Translates to a readable values + * @param {Object} instance - The model or context instance + * @param {Object} changes - Object containing changes + */ +exports.translateValues = async(instance, changes) => { + const models = instance.app.models; + + function getRelation(instance, property) { + const relations = instance.definition.settings.relations; + for (let relationName in relations) { + const relation = relations[relationName]; + if (relation.foreignKey == property) + return relation; + } + + return; + } + + function getValue(rawData) { + const row = JSON.parse(JSON.stringify(rawData)); + for (column in row) + return row[column]; + } + + function formatDate(date) { + return new Intl.DateTimeFormat('es', { + year: '2-digit', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit' + }).format(date); + } + + const properties = Object.assign({}, changes); + for (let property in properties) { + const relation = getRelation(instance, property); + const value = properties[property]; + + let finalValue = value; + if (relation) { + const model = relation.model; + const row = await models[model].findById(value, { + fields: ['alias', 'name', 'code', 'description'] + }); + const newValue = getValue(row); + if (newValue) finalValue = newValue; + } + + if (finalValue instanceof Date) + finalValue = formatDate(finalValue); + + properties[property] = finalValue; + } + + return properties; +}; + +/** + * Returns the changes between two objects + * @param {*} original - Original object + * @param {*} changes - New changes + * @return {Object} Old and new values + */ +exports.getChanges = (original, changes) => { + const oldChanges = {}; + const newChanges = {}; + for (let property in changes) { + if (changes[property] != original[property]) { + newChanges[property] = changes[property]; + + if (original[property] != undefined) + oldChanges[property] = original[property]; + } + } + + return { + old: oldChanges, + new: newChanges + }; +}; diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 6947cdc3b..557a76e2b 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -1,5 +1,5 @@ const UserError = require('vn-loopback/util/user-error'); -const diff = require('object-diff'); +const loggable = require('vn-loopback/util/log'); module.exports = Self => { Self.remoteMethodCtx('componentUpdate', { @@ -105,8 +105,6 @@ module.exports = Self => { // Force to unroute ticket const hasToBeUnrouted = true; - const changedProperties = diff(originalTicket, updatedTicket); - const query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; const res = await Self.rawSql(query, [ id, @@ -123,14 +121,18 @@ module.exports = Self => { option ]); + const changes = loggable.getChanges(originalTicket, updatedTicket); + const oldProperties = await loggable.translateValues(Self, changes.old); + const newProperties = await loggable.translateValues(Self, changes.new); + await models.TicketLog.create({ originFk: id, userFk: userId, action: 'update', changedModel: 'Ticket', changedModelId: id, - oldInstance: originalTicket, - newInstance: changedProperties + oldInstance: oldProperties, + newInstance: newProperties }); const salesPersonId = originalTicket.client().salesPersonFk; @@ -138,30 +140,9 @@ module.exports = Self => { const origin = ctx.req.headers.origin; let changesMade = ''; - for (let change in changedProperties) { - let value = changedProperties[change]; - let oldValue = originalTicket[change]; - if (value instanceof Date) { - value = new Intl.DateTimeFormat('es', { - year: '2-digit', - month: '2-digit', - day: '2-digit', - hour: '2-digit', - minute: '2-digit', - second: '2-digit' - }).format(value); - } - - if (oldValue instanceof Date) { - oldValue = new Intl.DateTimeFormat('es', { - year: '2-digit', - month: '2-digit', - day: '2-digit', - hour: '2-digit', - minute: '2-digit', - second: '2-digit' - }).format(oldValue); - } + for (let change in newProperties) { + let value = newProperties[change]; + let oldValue = oldProperties[change]; changesMade += `\r\n~${$t(change)}: ${oldValue}~ ➔ *${$t(change)}: ${value}*`; } From 5e93e454529748cf8dbd91ad75461814ba6a4413 Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 22 Oct 2020 12:01:45 +0200 Subject: [PATCH 2/2] Updated doc --- loopback/util/log.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loopback/util/log.js b/loopback/util/log.js index 59a4e4679..cf12dcb2f 100644 --- a/loopback/util/log.js +++ b/loopback/util/log.js @@ -60,8 +60,8 @@ exports.translateValues = async(instance, changes) => { /** * Returns the changes between two objects - * @param {*} original - Original object - * @param {*} changes - New changes + * @param {Object} original - Original object + * @param {Object} changes - New changes * @return {Object} Old and new values */ exports.getChanges = (original, changes) => {