refs #5816 Remove duplicated logs #1634

Merged
guillermo merged 14 commits from 5900-removeDuplicatedLogs into test 2023-06-29 13:35:50 +00:00
16 changed files with 12 additions and 305 deletions
Showing only changes of commit e32a29fc09 - Show all commits

View File

@ -19,9 +19,6 @@ module.exports = Self => {
}); });
Self.confirmTransaction = async(ctx, id, options) => { Self.confirmTransaction = async(ctx, id, options) => {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
let tx; let tx;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
@ -34,29 +31,8 @@ module.exports = Self => {
} }
try { try {
const oldTpvTransaction = await models.TpvTransaction.findById(id, null, myOptions);
const confirm = await Self.rawSql('CALL hedera.tpvTransaction_confirmById(?)', [id], myOptions); const confirm = await Self.rawSql('CALL hedera.tpvTransaction_confirmById(?)', [id], myOptions);
const tpvTransaction = await models.TpvTransaction.findById(id, null, myOptions);
const oldInstance = {status: oldTpvTransaction.status};
const newInstance = {status: tpvTransaction.status};
const logRecord = {
originFk: tpvTransaction.clientFk,
userFk: userId,
action: 'update',
changedModel: 'TpvTransaction',
changedModelId: id,
oldInstance: oldInstance,
newInstance: newInstance
};
await models.ClientLog.create(logRecord, myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();
return confirm; return confirm;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();

View File

@ -30,34 +30,9 @@ module.exports = Self => {
} }
}); });
Self.sendSms = async(ctx, id, destination, message, options) => { Self.sendSms = async(ctx, id, destination, message) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const userId = ctx.req.accessToken.userId;
const sms = await models.Sms.send(ctx, destination, message); const sms = await models.Sms.send(ctx, destination, message);
const logRecord = {
juan marked this conversation as resolved Outdated

Esto lo pidieron para saber que a un cliente que info se le ha enviado, sobre todo durante el mismo dia

Esto lo pidieron para saber que a un cliente que info se le ha enviado, sobre todo durante el mismo dia
Outdated
Review

Se solucionará en la tarea https://redmine.verdnatura.es/issues/5934

Se solucionará en la tarea https://redmine.verdnatura.es/issues/5934
originFk: id,
userFk: userId,
action: 'insert',
changedModel: 'sms',
newInstance: {
destinationFk: id,
destination: destination,
message: message,
statusCode: sms.statusCode,
status: sms.status
}
};
const clientLog = await models.ClientLog.create(logRecord, myOptions);
sms.logId = clientLog.id;
return sms; return sms;
}; };
}; };

View File

@ -13,10 +13,7 @@ describe('client sendSms()', () => {
const sms = await models.Client.sendSms(ctx, id, destination, message, options); const sms = await models.Client.sendSms(ctx, id, destination, message, options);
const createdLog = await models.ClientLog.findById(sms.logId, null, options); expect(sms).toBeDefined();
const json = JSON.parse(JSON.stringify(createdLog.newInstance));
expect(json.message).toEqual(message);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {

View File

@ -31,18 +31,6 @@ describe('route updateVolume()', () => {
const updatedRoute = await app.models.Route.findById(routeId, null, options); const updatedRoute = await app.models.Route.findById(routeId, null, options);
expect(updatedRoute.m3).not.toEqual(route.m3); expect(updatedRoute.m3).not.toEqual(route.m3);
const logs = await app.models.RouteLog.find({
fields: ['id', 'newInstance']
}, options);
const m3Log = logs.filter(log => {
if (log.newInstance)
return log.newInstance.m3 === updatedRoute.m3;
});
expect(m3Log.length).toEqual(1);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();

View File

@ -35,24 +35,9 @@ module.exports = Self => {
} }
try { try {
const originalRoute = await models.Route.findById(id, null, myOptions);
await Self.rawSql(`CALL vn.routeUpdateM3(?)`, [id], myOptions); await Self.rawSql(`CALL vn.routeUpdateM3(?)`, [id], myOptions);
const updatedRoute = await models.Route.findById(id, null, myOptions); const updatedRoute = await models.Route.findById(id, null, myOptions);
await models.RouteLog.create({
juan marked this conversation as resolved Outdated

este se puso porque ellos necesitan saber si alguien ha ejecutado esa accion. Cual es la alternativa?

este se puso porque ellos necesitan saber si alguien ha ejecutado esa accion. Cual es la alternativa?
Outdated
Review

El procedimiento vn.routeUpdateM3 ya actualiza las rutas y ese registro ya se inserta con el log binario, además no se comprueba que si el nuevo valor es distinto del antiguo. Actualmente el log aparece duplicado.

El procedimiento `vn.routeUpdateM3` ya actualiza las rutas y ese registro ya se inserta con el log binario, además no se comprueba que si el nuevo valor es distinto del antiguo. Actualmente el log aparece duplicado.
originFk: id,
userFk: userId,
action: 'update',
changedModel: 'Route',
changedModelId: id,
oldInstance: {m3: originalRoute.m3},
newInstance: {m3: updatedRoute.m3}
}, myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();
return updatedRoute; return updatedRoute;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();

View File

@ -97,22 +97,6 @@ module.exports = Self => {
}); });
await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions); await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions);
const logRecord = {
juan marked this conversation as resolved Outdated

En este caso he hecho una prueba y no logea nada..

En este caso he hecho una prueba y no logea nada..
Outdated
Review

Esto ya se registra con el log binario y aparece duplicado.

Esto ya se registra con el log binario y aparece duplicado.
originFk: sale.ticketFk,
userFk: userId,
action: 'update',
changedModel: 'ticketRequest',
newInstance: {
destinationFk: sale.ticketFk,
quantity: sale.quantity,
concept: sale.concept,
itemId: sale.itemFk,
ticketId: sale.ticketFk,
}
};
await Self.app.models.TicketLog.create(logRecord, myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();
return sale; return sale;

View File

@ -242,16 +242,6 @@ module.exports = Self => {
const oldProperties = await loggable.translateValues(Self, changes.old); const oldProperties = await loggable.translateValues(Self, changes.old);
const newProperties = await loggable.translateValues(Self, changes.new); const newProperties = await loggable.translateValues(Self, changes.new);
await models.TicketLog.create({
originFk: args.id,
userFk: userId,
action: 'update',
changedModel: 'Ticket',
changedModelId: args.id,
oldInstance: oldProperties,
newInstance: newProperties
}, myOptions);
const salesPersonId = originalTicket.client().salesPersonFk; const salesPersonId = originalTicket.client().salesPersonFk;
if (salesPersonId) { if (salesPersonId) {
const origin = ctx.req.headers.origin; const origin = ctx.req.headers.origin;

View File

@ -52,23 +52,10 @@ module.exports = Self => {
}); });
if (!ticket.originId || !ticket.destinationId) continue; if (!ticket.originId || !ticket.destinationId) continue;
const ticketDestinationLogRecord = {
originFk: ticket.destinationId,
userFk: ctx.req.accessToken.userId,
action: 'update',
changedModel: 'Ticket',
changedModelId: ticket.destinationId,
changedModelValue: ticket.destinationId,
oldInstance: {},
newInstance: {mergedTicket: ticket.originId}
};
await models.Sale.updateAll({ticketFk: ticket.originId}, {ticketFk: ticket.destinationId}, myOptions); await models.Sale.updateAll({ticketFk: ticket.originId}, {ticketFk: ticket.destinationId}, myOptions);
if (await models.Ticket.setDeleted(ctx, ticket.originId, myOptions)) { if (await models.Ticket.setDeleted(ctx, ticket.originId, myOptions))
await models.TicketLog.create(ticketDestinationLogRecord, myOptions);
await models.Chat.sendCheckingPresence(ctx, ticket.workerFk, message); await models.Chat.sendCheckingPresence(ctx, ticket.workerFk, message);
} }
}
if (tx) if (tx)
await tx.commit(); await tx.commit();
} catch (e) { } catch (e) {

View File

@ -30,54 +30,12 @@ module.exports = Self => {
} }
}); });
Self.sendSms = async(ctx, id, destination, message, options) => { Self.sendSms = async(ctx, id, destination, message) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const userId = ctx.req.accessToken.userId;
try {
const sms = await models.Sms.send(ctx, destination, message); const sms = await models.Sms.send(ctx, destination, message);
await models.TicketSms.create({
const newTicketSms = {
ticketFk: id, ticketFk: id,
smsFk: sms.id smsFk: sms.id
}; });
await models.TicketSms.create(newTicketSms);
const logRecord = {
originFk: id,
userFk: userId,
action: 'insert',
changedModel: 'sms',
newInstance: {
destinationFk: id,
destination: destination,
message: message,
statusCode: sms.statusCode,
status: sms.status
}
};
const ticketLog = await models.TicketLog.create(logRecord, myOptions);
sms.logId = ticketLog.id;
if (tx) await tx.commit();
return sms;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };

View File

@ -43,12 +43,10 @@ describe('ticket merge()', () => {
await models.Ticket.merge(ctx, [tickets], options); await models.Ticket.merge(ctx, [tickets], options);
const createdTicketLog = await models.TicketLog.find({where: {originFk: tickets.destinationId}}, options);
const deletedTicket = await models.Ticket.findOne({where: {id: tickets.originId}}, options); const deletedTicket = await models.Ticket.findOne({where: {id: tickets.originId}}, options);
const salesTicketFuture = await models.Sale.find({where: {ticketFk: tickets.destinationId}}, options); const salesTicketFuture = await models.Sale.find({where: {ticketFk: tickets.destinationId}}, options);
const chatNotificationAfterMerge = await models.Chat.find(null, options); const chatNotificationAfterMerge = await models.Chat.find(null, options);
expect(createdTicketLog.length).toEqual(1);
expect(deletedTicket.isDeleted).toEqual(true); expect(deletedTicket.isDeleted).toEqual(true);
expect(salesTicketFuture.length).toEqual(2); expect(salesTicketFuture.length).toEqual(2);
expect(chatNotificationBeforeMerge.length).toEqual(chatNotificationAfterMerge.length - 2); expect(chatNotificationBeforeMerge.length).toEqual(chatNotificationAfterMerge.length - 2);

View File

@ -12,19 +12,14 @@ describe('ticket sendSms()', () => {
const destination = 222222222; const destination = 222222222;
const message = 'this is the message created in a test'; const message = 'this is the message created in a test';
const sms = await models.Ticket.sendSms(ctx, id, destination, message, options); await models.Ticket.sendSms(ctx, id, destination, message, options);
const createdLog = await models.TicketLog.findById(sms.logId, null, options);
const filter = { const filter = {
ticketFk: createdLog.originFk ticketFk: id
}; };
const ticketSms = await models.TicketSms.findOne(filter, options); const ticketSms = await models.TicketSms.findOne(filter, options);
const json = JSON.parse(JSON.stringify(createdLog.newInstance)); expect(ticketSms.ticketFk).toEqual(id);
expect(json.message).toEqual(message);
expect(ticketSms.ticketFk).toEqual(createdLog.originFk);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {

View File

@ -84,13 +84,6 @@ module.exports = Self => {
for (const sale of sales) { for (const sale of sales) {
const originalSale = map.get(sale.id); const originalSale = map.get(sale.id);
const originalSaleData = { // <-- Loopback modifies original instance on save
itemFk: originalSale.itemFk,
quantity: originalSale.quantity,
concept: originalSale.concept,
ticketFk: originalSale.ticketFk
};
if (sale.quantity == originalSale.quantity) { if (sale.quantity == originalSale.quantity) {
query = `UPDATE sale query = `UPDATE sale
SET ticketFk = ? SET ticketFk = ?
@ -100,48 +93,6 @@ module.exports = Self => {
await transferPartialSale( await transferPartialSale(
ticketId, originalSale, sale, myOptions); ticketId, originalSale, sale, myOptions);
} }
// Log to original ticket
await models.TicketLog.create({
originFk: id,
userFk: userId,
action: 'update',
changedModel: 'Sale',
changedModelId: sale.id,
oldInstance: {
item: originalSaleData.itemFk,
quantity: originalSaleData.quantity,
concept: originalSaleData.concept,
ticket: originalSaleData.ticketFk
},
newInstance: {
item: sale.itemFk,
quantity: sale.quantity,
concept: sale.concept,
ticket: ticketId
}
}, myOptions);
// Log to destination ticket
await models.TicketLog.create({
originFk: ticketId,
userFk: userId,
action: 'update',
changedModel: 'Sale',
changedModelId: sale.id,
oldInstance: {
item: originalSaleData.itemFk,
quantity: originalSaleData.quantity,
concept: originalSaleData.concept,
ticket: originalSaleData.ticketFk
},
newInstance: {
item: sale.itemFk,
quantity: sale.quantity,
concept: sale.concept,
ticket: ticketId
}
}, myOptions);
} }
const isTicketEmpty = await models.Ticket.isEmpty(id, myOptions); const isTicketEmpty = await models.Ticket.isEmpty(id, myOptions);

View File

@ -1,30 +1,4 @@
const LoopBackContext = require('loopback-context');
module.exports = Self => { module.exports = Self => {
// Methods // Methods
require('./ticket-methods')(Self); require('./ticket-methods')(Self);
Self.observe('before save', async function(ctx) {
const loopBackContext = LoopBackContext.getCurrentContext();
const httpCtx = loopBackContext.active;
if (ctx.isNewInstance) return;
let changes = ctx.data || ctx.instance;
if (changes.routeFk === null && ctx.currentInstance.routeFk != null) {
let instance = JSON.parse(JSON.stringify(ctx.currentInstance));
let userId = httpCtx.accessToken.userId;
let logRecord = {
originFk: ctx.currentInstance.routeFk,
userFk: userId,
action: 'delete',
changedModel: 'Route',
oldInstance: {ticket: instance.id},
newInstance: null
};
await Self.app.models.RouteLog.create(logRecord);
}
});
}; };

View File

@ -25,9 +25,7 @@ module.exports = Self => {
}); });
Self.cloneWithEntries = async(ctx, id) => { Self.cloneWithEntries = async(ctx, id) => {
const userId = ctx.req.accessToken.userId;
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const models = Self.app.models;
const travel = await Self.findById(id, { const travel = await Self.findById(id, {
fields: [ fields: [
'id', 'id',
@ -81,18 +79,6 @@ module.exports = Self => {
] ]
}); });
const oldProperties = await loggable.translateValues(Self, travel);
const newProperties = await loggable.translateValues(Self, newTravel);
await models.TravelLog.create({
originFk: newTravel.id,
userFk: userId,
action: 'insert',
changedModel: 'Travel',
changedModelId: newTravel.id,
oldInstance: oldProperties,
newInstance: newProperties
});
return newTravel.id; return newTravel.id;
}; };
}; };

View File

@ -30,24 +30,8 @@ module.exports = Self => {
SET travelFk = NULL, dmsFk = NULL SET travelFk = NULL, dmsFk = NULL
WHERE id = ?`, [id], {userId}); WHERE id = ?`, [id], {userId});
const oldInstance = {
travelFk: travelThermograph.travelFk,
dmsFk: travelThermograph.dmsFk
};
await models.TravelLog.create({
originFk: travelThermograph.travelFk,
userFk: userId,
action: 'delete',
changedModel: 'TravelThermograph',
changedModelId: id,
oldInstance: oldInstance,
newInstance: {}
});
travelThermograph.travelFk = null; travelThermograph.travelFk = null;
travelThermograph.dmsFk = null; travelThermograph.dmsFk = null;
return travelThermograph; return travelThermograph;
}; };
}; };

View File

@ -40,7 +40,6 @@ module.exports = Self => {
Self.updateWorkerTimeControlMail = async(ctx, options) => { Self.updateWorkerTimeControlMail = async(ctx, options) => {
const models = Self.app.models; const models = Self.app.models;
const args = ctx.args; const args = ctx.args;
const userId = ctx.req.accessToken.userId;
const myOptions = {}; const myOptions = {};
@ -57,9 +56,6 @@ module.exports = Self => {
if (!workerTimeControlMail) throw new UserError(`There aren't records for this week`); if (!workerTimeControlMail) throw new UserError(`There aren't records for this week`);
const oldState = workerTimeControlMail.state;
const oldReason = workerTimeControlMail.reason;
await workerTimeControlMail.updateAttributes({ await workerTimeControlMail.updateAttributes({
state: args.state, state: args.state,
reason: args.reason || null reason: args.reason || null
@ -70,22 +66,5 @@ module.exports = Self => {
sendedCounter: workerTimeControlMail.sendedCounter + 1 sendedCounter: workerTimeControlMail.sendedCounter + 1
}, myOptions); }, myOptions);
} }
const logRecord = {
originFk: args.workerId,
userFk: userId,
action: 'update',
changedModel: 'WorkerTimeControlMail',
oldInstance: {
state: oldState,
reason: oldReason
},
newInstance: {
state: args.state,
reason: args.reason
}
};
return models.WorkerLog.create(logRecord, myOptions);
}; };
}; };