refs #2687 test: use tx.rollback()

This commit is contained in:
Javier Segarra 2023-12-14 16:00:29 +01:00
parent 8bd9becb74
commit d9ffdaf855
3 changed files with 44 additions and 23 deletions

View File

@ -10,7 +10,8 @@ CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_cloneWithEntries`(
IN vWarehouseInFk INT,
IN vRef VARCHAR(255),
IN vAgencyModeFk INT,
OUT vNewTravelFk INT)
OUT vNewTravelFk INT,
OUT vXTx BOOLEAN )
BEGIN
/**
* Clona un travel junto con sus entradas y compras
@ -38,11 +39,12 @@ BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
CALL util.tx_rollback(vTx);
RESIGNAL;
END;
START TRANSACTION;
-- START TRANSACTION;
CALL util.tx_start(vTx);
INSERT INTO travel (shipped, landed, warehouseInFk, warehouseOutFk, agencyModeFk, `ref`, isDelivered, isReceived, m3, cargoSupplierFk, kg,clonedFrom)
SELECT vDateStart, vDateEnd, vWarehouseInFk, vWarehouseOutFk, vAgencyModeFk, vRef, isDelivered, isReceived, m3,cargoSupplierFk, kg,vTravelFk
@ -64,10 +66,13 @@ BEGIN
LEAVE l;
END IF;
CALL util.tx_start(vTx);
-- CALL util.tx_start(vTx);
CALL entry_cloneHeader(vAuxEntryFk, vNewEntryFk, vNewTravelFk);
-- CALL util.tx_commit(vTx);
-- CALL util.tx_start(vTx);
CALL entry_copyBuys(vAuxEntryFk, vNewEntryFk);
CALL util.tx_commit(vTx);
-- CALL util.tx_commit(vTx);
SELECT evaNotes INTO vEvaNotes
FROM entry
@ -82,6 +87,8 @@ BEGIN
SET @isModeInventory = FALSE;
CLOSE vRsEntry;
COMMIT;
-- COMMIT;
SET vXTx = vTx;
CALL util.tx_commit(vTx);
END$$
DELIMITER ;

View File

@ -1,6 +1,5 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const UserError = require('vn-loopback/util/user-error');
const loggable = require('vn-loopback/util/log');
module.exports = Self => {
Self.remoteMethodCtx('cloneWithEntries', {
@ -26,12 +25,19 @@ module.exports = Self => {
});
Self.cloneWithEntries = async(ctx, id, options) => {
try {
const conn = Self.dataSource.connector;
const myOptions = {};
let tx = options?.transaction;
try {
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
if (myOptions.transaction !== tx)
throw new Error('transaction ID not match');
const travel = await Self.findById(id, {
fields: [
'id',
@ -51,9 +57,8 @@ module.exports = Self => {
let stmts = [];
let stmt;
let tx = options.transaction;
stmt = new ParameterizedSQL(
`CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [
`CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk, @vNewTx)`, [
id,
started,
ended,
@ -68,6 +73,8 @@ module.exports = Self => {
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions);
// if (tx) await tx.commit();
const [lastInsert] = result[newTravelIndex];
if (!lastInsert.id)
@ -83,11 +90,10 @@ module.exports = Self => {
'agencyModeFk',
'ref'
]
});
}, myOptions);
return newTravel.id;
} catch (e) {
if (myOptions.transaction) await myOptions.transaction.rollback();
if (tx) await tx.rollback();
throw e;
}
};

View File

@ -1,13 +1,14 @@
const app = require('vn-loopback/server/server');
describe('Travel cloneWithEntries()', () => {
fdescribe('Travel cloneWithEntries()', () => {
const models = app.models;
const travelId = 5;
const currentUserId = 1102;
const ctx = {req: {accessToken: {userId: currentUserId}}};
let newTravelId;
it(`should clone the travel and the containing entries`, async() => {
const tx = await models.Travel.beginTransaction({});
const tx = await models.Travel.beginTransaction({
});
const warehouseThree = 3;
const agencyModeOne = 1;
try {
@ -18,19 +19,26 @@ describe('Travel cloneWithEntries()', () => {
travelFk: newTravelId
}
}, options);
const newTravel = await models.Travel.findById(travelId, options);
const newTravel = await models.Travel.findById(travelId);
expect(newTravelId).not.toEqual(travelId);
expect(newTravel.ref).toEqual('fifth travel');
expect(newTravel.warehouseInFk).toEqual(warehouseThree);
expect(newTravel.warehouseOutFk).toEqual(warehouseThree);
expect(newTravel.agencyModeFk).toEqual(agencyModeOne);
expect(travelEntries.length).toBeGreaterThan(0);
await models.Entry.destroyAll({
travelFk: newTravelId
}, options);
await models.Travel.destroyById(newTravelId, options);
expect(travelEntries.length).toBeGreaterThan(0);
await tx.rollback();
const travelRemoved = await models.Travel.findById(newTravelId, options);
expect(travelRemoved).toBeNull();
// await models.Entry.destroyAll({
// travelFk: newTravelId
// }, options);
// await models.Travel.destroyById(newTravelId, options);
} catch (e) {
if (tx) await tx.rollback();
throw e;