2951 - Added transaction to updateVolume() method
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
e3484750ba
commit
be791d1f00
|
@ -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);
|
||||
|
||||
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);
|
||||
|
||||
const ticket = await app.models.Ticket.findById(14);
|
||||
await ticket.updateAttributes({routeFk: routeId});
|
||||
await app.models.Route.updateVolume(ctx, routeId);
|
||||
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 updatedRoute = await app.models.Route.findById(routeId);
|
||||
const updatedRoute = await app.models.Route.findById(routeId, null, options);
|
||||
|
||||
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 => {
|
||||
if (log.newInstance)
|
||||
return log.newInstance.m3 === updatedRoute.m3;
|
||||
});
|
||||
const logIdToDestroy = m3Log[0].id;
|
||||
|
||||
expect(m3Log.length).toEqual(1);
|
||||
|
||||
// restores
|
||||
await ticket.updateAttributes({routeFk: null});
|
||||
await route.updateAttributes({m3: 1.8});
|
||||
await app.models.RouteLog.destroyById(logIdToDestroy);
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -19,15 +19,29 @@ 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 = {
|
||||
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,
|
||||
userFk: userId,
|
||||
action: 'update',
|
||||
|
@ -35,8 +49,14 @@ module.exports = Self => {
|
|||
changedModelId: id,
|
||||
oldInstance: {m3: originalRoute.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;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue