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()', () => {
|
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);
|
|
||||||
|
|
||||||
expect(route.m3).toEqual(1.8);
|
const tx = await app.models.Ticket.beginTransaction({});
|
||||||
|
|
||||||
const ticket = await app.models.Ticket.findById(14);
|
try {
|
||||||
await ticket.updateAttributes({routeFk: routeId});
|
const options = {transaction: tx};
|
||||||
await app.models.Route.updateVolume(ctx, routeId);
|
|
||||||
|
|
||||||
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 => {
|
const updatedRoute = await app.models.Route.findById(routeId, null, options);
|
||||||
if (log.newInstance)
|
|
||||||
return log.newInstance.m3 === updatedRoute.m3;
|
|
||||||
});
|
|
||||||
const logIdToDestroy = m3Log[0].id;
|
|
||||||
|
|
||||||
expect(m3Log.length).toEqual(1);
|
expect(updatedRoute.m3).not.toEqual(route.m3);
|
||||||
|
|
||||||
// restores
|
const logs = await app.models.RouteLog.find({
|
||||||
await ticket.updateAttributes({routeFk: null});
|
fields: ['id', 'newInstance']
|
||||||
await route.updateAttributes({m3: 1.8});
|
}, options);
|
||||||
await app.models.RouteLog.destroyById(logIdToDestroy);
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -19,24 +19,44 @@ 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')
|
||||||
originFk: id,
|
Object.assign(myOptions, options);
|
||||||
userFk: userId,
|
|
||||||
action: 'update',
|
|
||||||
changedModel: 'Route',
|
|
||||||
changedModelId: id,
|
|
||||||
oldInstance: {m3: originalRoute.m3},
|
|
||||||
newInstance: {m3: updatedRoute.m3}
|
|
||||||
};
|
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue