2951 - Added transaction to updateVolume() method
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2021-06-10 16:08:44 +02:00
parent e3484750ba
commit be791d1f00
2 changed files with 63 additions and 34 deletions

View File

@ -3,6 +3,7 @@ const LoopBackContext = require('loopback-context');
describe('route updateVolume()', () => { describe('route updateVolume()', () => {
const routeId = 1; const routeId = 1;
const ticketId = 14;
const userId = 50; const userId = 50;
const activeCtx = { const activeCtx = {
accessToken: {userId: userId}, accessToken: {userId: userId},
@ -13,31 +14,39 @@ describe('route updateVolume()', () => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx active: activeCtx
}); });
const route = await app.models.Route.findById(routeId);
const tx = await app.models.Ticket.beginTransaction({});
try {
const options = {transaction: tx};
const route = await app.models.Route.findById(routeId, null, options);
expect(route.m3).toEqual(1.8); expect(route.m3).toEqual(1.8);
const ticket = await app.models.Ticket.findById(14); const ticket = await app.models.Ticket.findById(ticketId, null, options);
await ticket.updateAttributes({routeFk: routeId}); await ticket.updateAttributes({routeFk: routeId}, options);
await app.models.Route.updateVolume(ctx, routeId); await app.models.Route.updateVolume(ctx, routeId, options);
const updatedRoute = await app.models.Route.findById(routeId); 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']}); const logs = await app.models.RouteLog.find({
fields: ['id', 'newInstance']
}, options);
const m3Log = logs.filter(log => { const m3Log = logs.filter(log => {
if (log.newInstance) if (log.newInstance)
return log.newInstance.m3 === updatedRoute.m3; return log.newInstance.m3 === updatedRoute.m3;
}); });
const logIdToDestroy = m3Log[0].id;
expect(m3Log.length).toEqual(1); expect(m3Log.length).toEqual(1);
// restores await tx.rollback();
await ticket.updateAttributes({routeFk: null}); } catch (e) {
await route.updateAttributes({m3: 1.8}); await tx.rollback();
await app.models.RouteLog.destroyById(logIdToDestroy); throw e;
}
}); });
}); });

View File

@ -19,15 +19,29 @@ module.exports = Self => {
} }
}); });
Self.updateVolume = async(ctx, id) => { Self.updateVolume = async(ctx, id, options) => {
let query = `CALL vn.routeUpdateM3(?)`; const userId = ctx.req.accessToken.userId;
let userId = ctx.req.accessToken.userId; const models = Self.app.models;
let originalRoute = await Self.app.models.Route.findById(id);
await Self.rawSql(query, [id]); let tx;
let updatedRoute = await Self.app.models.Route.findById(id); let myOptions = {};
let logRecord = { if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const originalRoute = await models.Route.findById(id, null, myOptions);
await Self.rawSql(`CALL vn.routeUpdateM3(?)`, [id], myOptions);
const updatedRoute = await models.Route.findById(id, null, myOptions);
await models.RouteLog.create({
originFk: id, originFk: id,
userFk: userId, userFk: userId,
action: 'update', action: 'update',
@ -35,8 +49,14 @@ module.exports = Self => {
changedModelId: id, changedModelId: id,
oldInstance: {m3: originalRoute.m3}, oldInstance: {m3: originalRoute.m3},
newInstance: {m3: updatedRoute.m3} newInstance: {m3: updatedRoute.m3}
}; }, myOptions);
return await Self.app.models.RouteLog.create(logRecord); if (tx) await tx.commit();
return updatedRoute;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };