2020-10-22 09:24:53 +00:00
|
|
|
/**
|
|
|
|
* Translates to a readable values
|
|
|
|
* @param {Object} instance - The model or context instance
|
|
|
|
* @param {Object} changes - Object containing changes
|
2021-05-14 17:21:31 +00:00
|
|
|
* @param {Object} options - Object containing transaction
|
2020-10-22 09:24:53 +00:00
|
|
|
*/
|
2021-03-19 18:20:19 +00:00
|
|
|
exports.translateValues = async(instance, changes, options = {}) => {
|
2020-10-22 09:24:53 +00:00
|
|
|
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', {
|
2021-01-05 14:03:35 +00:00
|
|
|
year: 'numeric',
|
|
|
|
month: 'numeric',
|
|
|
|
day: 'numeric',
|
2020-10-22 09:24:53 +00:00
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
|
|
|
second: '2-digit'
|
|
|
|
}).format(date);
|
|
|
|
}
|
|
|
|
|
2020-12-28 13:53:26 +00:00
|
|
|
if (changes instanceof instance)
|
|
|
|
changes = changes.__data;
|
|
|
|
|
2020-10-22 09:24:53 +00:00
|
|
|
const properties = Object.assign({}, changes);
|
|
|
|
for (let property in properties) {
|
2021-03-19 18:20:19 +00:00
|
|
|
const firstChar = property.substring(0, 1);
|
|
|
|
const isPrivate = firstChar == '$';
|
|
|
|
if (isPrivate) {
|
|
|
|
delete properties[property];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:24:53 +00:00
|
|
|
const relation = getRelation(instance, property);
|
|
|
|
const value = properties[property];
|
2021-03-22 08:08:27 +00:00
|
|
|
const hasValue = value != null && value != undefined;
|
2020-11-27 10:56:41 +00:00
|
|
|
|
2021-03-19 18:20:19 +00:00
|
|
|
let finalValue = value;
|
2021-03-22 08:08:27 +00:00
|
|
|
if (relation && hasValue) {
|
2021-03-19 18:20:19 +00:00
|
|
|
let fieldsToShow = ['nickname', 'name', 'code', 'description'];
|
2020-12-28 13:53:26 +00:00
|
|
|
const modelName = relation.model;
|
|
|
|
const model = models[modelName];
|
|
|
|
const log = model.definition.settings.log;
|
2020-11-27 10:56:41 +00:00
|
|
|
|
|
|
|
if (log && log.showField)
|
2020-12-28 13:53:26 +00:00
|
|
|
fieldsToShow = [log.showField];
|
2020-11-27 10:56:41 +00:00
|
|
|
|
2020-12-28 13:53:26 +00:00
|
|
|
const row = await model.findById(value, {
|
2020-11-27 10:56:41 +00:00
|
|
|
fields: fieldsToShow
|
2021-03-19 18:20:19 +00:00
|
|
|
}, options);
|
2020-10-22 09:24:53 +00:00
|
|
|
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
|
2020-10-22 10:01:45 +00:00
|
|
|
* @param {Object} original - Original object
|
|
|
|
* @param {Object} changes - New changes
|
2020-10-22 09:24:53 +00:00
|
|
|
* @return {Object} Old and new values
|
|
|
|
*/
|
|
|
|
exports.getChanges = (original, changes) => {
|
|
|
|
const oldChanges = {};
|
|
|
|
const newChanges = {};
|
2021-03-22 08:08:27 +00:00
|
|
|
|
2020-10-22 09:24:53 +00:00
|
|
|
for (let property in changes) {
|
2021-03-19 18:20:19 +00:00
|
|
|
const firstChar = property.substring(0, 1);
|
|
|
|
const isPrivate = firstChar == '$';
|
2021-03-22 08:08:27 +00:00
|
|
|
if (isPrivate) return;
|
|
|
|
|
2023-01-11 12:24:55 +00:00
|
|
|
const hasChanges = original[property] instanceof Date ?
|
|
|
|
changes[property]?.getTime() != original[property]?.getTime() :
|
|
|
|
changes[property] != original[property];
|
|
|
|
|
|
|
|
if (hasChanges) {
|
2020-10-22 09:24:53 +00:00
|
|
|
newChanges[property] = changes[property];
|
|
|
|
|
|
|
|
if (original[property] != undefined)
|
|
|
|
oldChanges[property] = original[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
old: oldChanges,
|
|
|
|
new: newChanges
|
|
|
|
};
|
|
|
|
};
|