#2687 - Travel CloneWithEntries #1887

Merged
jsegarra merged 20 commits from 2687_travel_cloneWithEntries into dev 2024-01-24 10:55:20 +00:00
3 changed files with 44 additions and 23 deletions
Showing only changes of commit d9ffdaf855 - Show all commits

View File

@ -10,7 +10,8 @@ CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_cloneWithEntries`(
IN vWarehouseInFk INT, IN vWarehouseInFk INT,
IN vRef VARCHAR(255), IN vRef VARCHAR(255),
IN vAgencyModeFk INT, IN vAgencyModeFk INT,
OUT vNewTravelFk INT) OUT vNewTravelFk INT,
OUT vXTx BOOLEAN )
BEGIN BEGIN
/** /**
* Clona un travel junto con sus entradas y compras * Clona un travel junto con sus entradas y compras
@ -38,11 +39,12 @@ BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN BEGIN

He modificado ROLLBACK por esto

He modificado ROLLBACK por esto
ROLLBACK; CALL util.tx_rollback(vTx);
RESIGNAL; RESIGNAL;
END; END;

He modificado START TRANSACTION por esto

He modificado START TRANSACTION por esto
START TRANSACTION; -- START TRANSACTION;
CALL util.tx_start(vTx);
INSERT INTO travel (shipped, landed, warehouseInFk, warehouseOutFk, agencyModeFk, `ref`, isDelivered, isReceived, m3, cargoSupplierFk, kg,clonedFrom) 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 SELECT vDateStart, vDateEnd, vWarehouseInFk, vWarehouseOutFk, vAgencyModeFk, vRef, isDelivered, isReceived, m3,cargoSupplierFk, kg,vTravelFk
@ -64,10 +66,13 @@ BEGIN
LEAVE l; LEAVE l;
END IF; END IF;
CALL util.tx_start(vTx); -- CALL util.tx_start(vTx);
CALL entry_cloneHeader(vAuxEntryFk, vNewEntryFk, vNewTravelFk); CALL entry_cloneHeader(vAuxEntryFk, vNewEntryFk, vNewTravelFk);
-- CALL util.tx_commit(vTx);
-- CALL util.tx_start(vTx);
CALL entry_copyBuys(vAuxEntryFk, vNewEntryFk); CALL entry_copyBuys(vAuxEntryFk, vNewEntryFk);
CALL util.tx_commit(vTx); -- CALL util.tx_commit(vTx);
SELECT evaNotes INTO vEvaNotes SELECT evaNotes INTO vEvaNotes
FROM entry FROM entry
@ -82,6 +87,8 @@ BEGIN
SET @isModeInventory = FALSE; SET @isModeInventory = FALSE;
CLOSE vRsEntry; CLOSE vRsEntry;
COMMIT; -- COMMIT;
SET vXTx = vTx;
CALL util.tx_commit(vTx);
END$$ END$$
DELIMITER ; DELIMITER ;

View File

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

View File

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