From be791d1f0067ebd87bcfa30c4881aac45d1882ef Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 10 Jun 2021 16:08:44 +0200 Subject: [PATCH] 2951 - Added transaction to updateVolume() method --- .../methods/route/specs/updateVolume.spec.js | 45 +++++++++------- .../route/back/methods/route/updateVolume.js | 52 +++++++++++++------ 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/modules/route/back/methods/route/specs/updateVolume.spec.js b/modules/route/back/methods/route/specs/updateVolume.spec.js index e50a2ba00a..fdcc1a6ec7 100644 --- a/modules/route/back/methods/route/specs/updateVolume.spec.js +++ b/modules/route/back/methods/route/specs/updateVolume.spec.js @@ -3,6 +3,7 @@ const LoopBackContext = require('loopback-context'); describe('route updateVolume()', () => { const routeId = 1; + const ticketId = 14; const userId = 50; const activeCtx = { accessToken: {userId: userId}, @@ -13,31 +14,39 @@ describe('route updateVolume()', () => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); - const route = await app.models.Route.findById(routeId); - expect(route.m3).toEqual(1.8); + const tx = await app.models.Ticket.beginTransaction({}); - const ticket = await app.models.Ticket.findById(14); - await ticket.updateAttributes({routeFk: routeId}); - await app.models.Route.updateVolume(ctx, routeId); + try { + const options = {transaction: tx}; - const updatedRoute = await app.models.Route.findById(routeId); + const route = await app.models.Route.findById(routeId, null, options); - expect(updatedRoute.m3).not.toEqual(route.m3); + expect(route.m3).toEqual(1.8); - const logs = await app.models.RouteLog.find({fields: ['id', 'newInstance']}); + const ticket = await app.models.Ticket.findById(ticketId, null, options); + await ticket.updateAttributes({routeFk: routeId}, options); + await app.models.Route.updateVolume(ctx, routeId, options); - const m3Log = logs.filter(log => { - if (log.newInstance) - return log.newInstance.m3 === updatedRoute.m3; - }); - const logIdToDestroy = m3Log[0].id; + const updatedRoute = await app.models.Route.findById(routeId, null, options); - expect(m3Log.length).toEqual(1); + expect(updatedRoute.m3).not.toEqual(route.m3); - // restores - await ticket.updateAttributes({routeFk: null}); - await route.updateAttributes({m3: 1.8}); - await app.models.RouteLog.destroyById(logIdToDestroy); + 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(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/route/back/methods/route/updateVolume.js b/modules/route/back/methods/route/updateVolume.js index 27e96ebab8..ce6e16b5c2 100644 --- a/modules/route/back/methods/route/updateVolume.js +++ b/modules/route/back/methods/route/updateVolume.js @@ -19,24 +19,44 @@ module.exports = Self => { } }); - Self.updateVolume = async(ctx, id) => { - let query = `CALL vn.routeUpdateM3(?)`; - let userId = ctx.req.accessToken.userId; - let originalRoute = await Self.app.models.Route.findById(id); + Self.updateVolume = async(ctx, id, options) => { + const userId = ctx.req.accessToken.userId; + const models = Self.app.models; - await Self.rawSql(query, [id]); - let updatedRoute = await Self.app.models.Route.findById(id); + let tx; + let myOptions = {}; - let logRecord = { - originFk: id, - userFk: userId, - action: 'update', - changedModel: 'Route', - changedModelId: id, - oldInstance: {m3: originalRoute.m3}, - newInstance: {m3: updatedRoute.m3} - }; + if (typeof options == 'object') + Object.assign(myOptions, options); - return await Self.app.models.RouteLog.create(logRecord); + 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, + userFk: userId, + action: 'update', + changedModel: 'Route', + changedModelId: id, + oldInstance: {m3: originalRoute.m3}, + newInstance: {m3: updatedRoute.m3} + }, myOptions); + + if (tx) await tx.commit(); + + return updatedRoute; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; };