Empty value not being logged in
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2021-03-22 09:08:27 +01:00
parent 6210e60896
commit 7ac4d67f97
3 changed files with 97 additions and 35 deletions

View File

@ -1,5 +1,5 @@
const pick = require('object.pick');
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
const log = require('vn-loopback/util/log');
module.exports = function(Self) { module.exports = function(Self) {
Self.setup = function() { Self.setup = function() {
@ -14,8 +14,6 @@ module.exports = function(Self) {
Self.observe('before save', async function(ctx) { Self.observe('before save', async function(ctx) {
const appModels = ctx.Model.app.models; const appModels = ctx.Model.app.models;
const definition = ctx.Model.definition; const definition = ctx.Model.definition;
const modelName = definition.name;
const model = appModels[modelName];
const options = {}; const options = {};
// Check for transactions // Check for transactions
@ -26,12 +24,13 @@ module.exports = function(Self) {
let newInstance; let newInstance;
if (ctx.data) { if (ctx.data) {
const changes = log.getChanges(ctx.currentInstance, ctx.data); const changes = pick(ctx.currentInstance, Object.keys(ctx.data));
oldInstance = await log.translateValues(model, changes.old, options); newInstance = await fkToValue(ctx.data, ctx);
newInstance = await log.translateValues(model, changes.new, options); oldInstance = await fkToValue(changes, ctx);
if (ctx.where && !ctx.currentInstance) { if (ctx.where && !ctx.currentInstance) {
const fields = Object.keys(ctx.data); const fields = Object.keys(ctx.data);
const modelName = definition.name;
ctx.oldInstances = await appModels[modelName].find({ ctx.oldInstances = await appModels[modelName].find({
where: ctx.where, where: ctx.where,
@ -42,42 +41,40 @@ module.exports = function(Self) {
// Get changes from created instance // Get changes from created instance
if (ctx.isNewInstance) if (ctx.isNewInstance)
newInstance = await log.translateValues(model, ctx.instance.__data, options); newInstance = await fkToValue(ctx.instance.__data, ctx);
ctx.hookState.oldInstance = oldInstance; ctx.hookState.oldInstance = oldInstance;
ctx.hookState.newInstance = newInstance; ctx.hookState.newInstance = newInstance;
}); });
Self.observe('before delete', async function(ctx) { Self.observe('before delete', async function(ctx) {
const models = ctx.Model.app.models; const appModels = ctx.Model.app.models;
const definition = ctx.Model.definition; const definition = ctx.Model.definition;
const relations = ctx.Model.relations; const relations = ctx.Model.relations;
const options = {}; let options = {};
if (ctx.options && ctx.options.transaction) if (ctx.options && ctx.options.transaction)
options.transaction = ctx.options.transaction; options.transaction = ctx.options.transaction;
if (ctx.where) { if (ctx.where) {
const modelName = definition.name; let affectedModel = definition.name;
const model = models[modelName]; let deletedInstances = await appModels[affectedModel].find({
const deletedRows = await model.find({
where: ctx.where where: ctx.where
}, options); }, options);
const relation = definition.settings.log.relation; let relation = definition.settings.log.relation;
if (relation) { if (relation) {
const primaryKey = relations[relation].keyFrom; let primaryKey = relations[relation].keyFrom;
const instances = []; let arrangedDeletedInstances = [];
for (let instance of deletedRows) { for (let i = 0; i < deletedInstances.length; i++) {
const translatedValues = await log.translateValues(model, instance, options);
if (primaryKey) if (primaryKey)
translatedValues.originFk = instance[primaryKey]; deletedInstances[i].originFk = deletedInstances[i][primaryKey];
instances.push(translatedValues); let arrangedInstance = await fkToValue(deletedInstances[i], ctx);
arrangedDeletedInstances[i] = arrangedInstance;
} }
ctx.hookState.oldInstance = arrangedDeletedInstances;
ctx.hookState.oldInstance = instances;
} }
} }
}); });
@ -119,6 +116,74 @@ module.exports = function(Self) {
}); });
} }
// Get log values from a foreign key
async function fkToValue(instance, ctx) {
const appModels = ctx.Model.app.models;
const relations = ctx.Model.relations;
let options = {};
// Check for transactions
if (ctx.options && ctx.options.transaction)
options.transaction = ctx.options.transaction;
const instanceCopy = JSON.parse(JSON.stringify(instance));
const result = {};
for (const key in instanceCopy) {
let value = instanceCopy[key];
if (value instanceof Object)
continue;
if (value === undefined) continue;
if (value) {
for (let relationName in relations) {
const relation = relations[relationName];
if (relation.keyFrom == key && key != 'id') {
const model = relation.modelTo;
const modelName = relation.modelTo.modelName;
const properties = model && model.definition.properties;
const settings = model && model.definition.settings;
const recordSet = await appModels[modelName].findById(value, null, options);
const hasShowField = settings.log && settings.log.showField;
let showField = hasShowField && recordSet
&& recordSet[settings.log.showField];
if (!showField) {
const showFieldNames = [
'name',
'description',
'code',
'nickname'
];
for (field of showFieldNames) {
const propField = properties && properties[field];
const recordField = recordSet && recordSet[field];
if (propField && recordField) {
showField = field;
break;
}
}
}
if (showField && recordSet && recordSet[showField]) {
value = recordSet[showField];
break;
}
value = recordSet && recordSet.id || value;
break;
}
}
}
result[key] = value;
}
return result;
}
async function logInModel(ctx, loopBackContext) { async function logInModel(ctx, loopBackContext) {
const appModels = ctx.Model.app.models; const appModels = ctx.Model.app.models;
const definition = ctx.Model.definition; const definition = ctx.Model.definition;
@ -262,8 +327,8 @@ module.exports = function(Self) {
} }
function setActionType(ctx) { function setActionType(ctx) {
const oldInstance = ctx.hookState.oldInstance; let oldInstance = ctx.hookState.oldInstance;
const newInstance = ctx.hookState.newInstance; let newInstance = ctx.hookState.newInstance;
if (oldInstance && newInstance) if (oldInstance && newInstance)
return 'update'; return 'update';

View File

@ -47,9 +47,10 @@ exports.translateValues = async(instance, changes, options = {}) => {
const relation = getRelation(instance, property); const relation = getRelation(instance, property);
const value = properties[property]; const value = properties[property];
const hasValue = value != null && value != undefined;
let finalValue = value; let finalValue = value;
if (relation) { if (relation && hasValue) {
let fieldsToShow = ['nickname', 'name', 'code', 'description']; let fieldsToShow = ['nickname', 'name', 'code', 'description'];
const modelName = relation.model; const modelName = relation.model;
const model = models[modelName]; const model = models[modelName];
@ -58,9 +59,6 @@ exports.translateValues = async(instance, changes, options = {}) => {
if (log && log.showField) if (log && log.showField)
fieldsToShow = [log.showField]; fieldsToShow = [log.showField];
console.log('Falla aqui? ', value);
console.log('property: ', property);
console.log('changes: ', changes);
const row = await model.findById(value, { const row = await model.findById(value, {
fields: fieldsToShow fields: fieldsToShow
}, options); }, options);
@ -86,10 +84,13 @@ exports.translateValues = async(instance, changes, options = {}) => {
exports.getChanges = (original, changes) => { exports.getChanges = (original, changes) => {
const oldChanges = {}; const oldChanges = {};
const newChanges = {}; const newChanges = {};
for (let property in changes) { for (let property in changes) {
const firstChar = property.substring(0, 1); const firstChar = property.substring(0, 1);
const isPrivate = firstChar == '$'; const isPrivate = firstChar == '$';
if (changes[property] != original[property] && !isPrivate) { if (isPrivate) return;
if (changes[property] != original[property]) {
newChanges[property] = changes[property]; newChanges[property] = changes[property];
if (original[property] != undefined) if (original[property] != undefined)

View File

@ -1,7 +1,7 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
fdescribe('ticket setDelivered()', () => { describe('ticket setDelivered()', () => {
const userId = 50; const userId = 50;
const activeCtx = { const activeCtx = {
accessToken: {userId: userId}, accessToken: {userId: userId},
@ -23,8 +23,6 @@ fdescribe('ticket setDelivered()', () => {
ticketOne = await app.models.Ticket.create(originalTicketOne); ticketOne = await app.models.Ticket.create(originalTicketOne);
ticketTwo = await app.models.Ticket.create(originalTicketTwo); ticketTwo = await app.models.Ticket.create(originalTicketTwo);
console.log(ticketOne);
console.log(ticketTwo);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
@ -34,10 +32,8 @@ fdescribe('ticket setDelivered()', () => {
afterAll(async done => { afterAll(async done => {
try { try {
console.log(ticketOne); await app.models.Ticket.destroyById(ticketOne.id);
console.log(ticketTwo); await app.models.Ticket.destroyById(ticketTwo.id);
// await app.models.Ticket.destroyById(ticketOne.id);
// await app.models.Ticket.destroyById(ticketTwo.id);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }