Added loggable utils
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
b5c5d78d3a
commit
f87a7664e5
|
@ -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() => {
|
||||
|
|
|
@ -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 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}*`;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue