Added loggable utils #422
|
@ -38,7 +38,7 @@ describe('chat sendCheckingPresence()', () => {
|
||||||
|
|
||||||
expect(response.statusCode).toEqual(200);
|
expect(response.statusCode).toEqual(200);
|
||||||
expect(response.message).toEqual('Fake notification sent');
|
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() => {
|
it(`should call to send() method with the worker username when the worker is working`, async() => {
|
||||||
|
|
|
@ -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
|
||||||
|
};
|
||||||
|
};
|
|
@ -1,5 +1,5 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
const diff = require('object-diff');
|
const loggable = require('vn-loopback/util/log');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('componentUpdate', {
|
Self.remoteMethodCtx('componentUpdate', {
|
||||||
|
@ -105,8 +105,6 @@ module.exports = Self => {
|
||||||
|
|
||||||
// Force to unroute ticket
|
// Force to unroute ticket
|
||||||
const hasToBeUnrouted = true;
|
const hasToBeUnrouted = true;
|
||||||
const changedProperties = diff(originalTicket, updatedTicket);
|
|
||||||
|
|
||||||
const query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
const query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||||
const res = await Self.rawSql(query, [
|
const res = await Self.rawSql(query, [
|
||||||
id,
|
id,
|
||||||
|
@ -123,14 +121,18 @@ module.exports = Self => {
|
||||||
option
|
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({
|
await models.TicketLog.create({
|
||||||
originFk: id,
|
originFk: id,
|
||||||
userFk: userId,
|
userFk: userId,
|
||||||
action: 'update',
|
action: 'update',
|
||||||
changedModel: 'Ticket',
|
changedModel: 'Ticket',
|
||||||
changedModelId: id,
|
changedModelId: id,
|
||||||
oldInstance: originalTicket,
|
oldInstance: oldProperties,
|
||||||
newInstance: changedProperties
|
newInstance: newProperties
|
||||||
});
|
});
|
||||||
|
|
||||||
const salesPersonId = originalTicket.client().salesPersonFk;
|
const salesPersonId = originalTicket.client().salesPersonFk;
|
||||||
|
@ -138,30 +140,9 @@ module.exports = Self => {
|
||||||
const origin = ctx.req.headers.origin;
|
const origin = ctx.req.headers.origin;
|
||||||
|
|
||||||
let changesMade = '';
|
let changesMade = '';
|
||||||
for (let change in changedProperties) {
|
for (let change in newProperties) {
|
||||||
let value = changedProperties[change];
|
let value = newProperties[change];
|
||||||
let oldValue = originalTicket[change];
|
let oldValue = oldProperties[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);
|
|
||||||
}
|
|
||||||
|
|
||||||
changesMade += `\r\n~${$t(change)}: ${oldValue}~ ➔ *${$t(change)}: ${value}*`;
|
changesMade += `\r\n~${$t(change)}: ${oldValue}~ ➔ *${$t(change)}: ${value}*`;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue