#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 62 additions and 132 deletions
Showing only changes of commit 8bd9becb74 - Show all commits

View File

@ -10,7 +10,6 @@ CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_cloneWithEntries`(
IN vWarehouseInFk INT,
IN vRef VARCHAR(255),
IN vAgencyModeFk INT,
IN vTx BOOL,
OUT vNewTravelFk INT)
BEGIN
/**

View File

@ -11,8 +11,9 @@ module.exports = Self => {
type: 'number',
required: true,
description: 'The original travel id',
http: {source: 'path'}
}],
http: {source: 'path'},
},
],
returns: {
type: 'object',
description: 'The new cloned travel id',
@ -24,62 +25,70 @@ module.exports = Self => {
}
});
Self.cloneWithEntries = async(ctx, id) => {
const conn = Self.dataSource.connector;
const travel = await Self.findById(id, {
fields: [
'id',
'shipped',
'landed',
'warehouseInFk',
'warehouseOutFk',
'agencyModeFk',
'ref'
]
});
const started = Date.vnNew();
const ended = Date.vnNew();
Self.cloneWithEntries = async(ctx, id, options) => {
try {
const conn = Self.dataSource.connector;
const myOptions = {};
if (!travel)
throw new UserError('Travel not found');
if (typeof options == 'object')
Object.assign(myOptions, options);
const travel = await Self.findById(id, {
fields: [
'id',
'shipped',
'landed',
'warehouseInFk',
'warehouseOutFk',
'agencyModeFk',
'ref'
]
});
const started = Date.vnNew();
const ended = Date.vnNew();
let stmts = [];
let stmt;
let tx = await Self.beginTransaction({});
stmt = new ParameterizedSQL(
`CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [
id,
started,
ended,
travel.warehouseOutFk,
travel.warehouseInFk,
travel.ref,
travel.agencyModeFk,
!!tx.id
]
);
stmts.push(stmt);
const newTravelIndex = stmts.push('SELECT @vTravelFk AS id') - 1;
if (!travel)
throw new UserError('Travel not found');
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql);
const [lastInsert] = result[newTravelIndex];
let stmts = [];
let stmt;
let tx = options.transaction;
stmt = new ParameterizedSQL(
`CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [
id,
started,
ended,
travel.warehouseOutFk,
travel.warehouseInFk,
travel.ref,
travel.agencyModeFk
]
);
stmts.push(stmt);
const newTravelIndex = stmts.push('SELECT @vTravelFk AS id') - 1;
if (!lastInsert.id)
throw new UserError('Unable to clone this travel');
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions);
const [lastInsert] = result[newTravelIndex];
const newTravel = await Self.findById(lastInsert.id, {
fields: [
'id',
'shipped',
'landed',
'warehouseInFk',
'warehouseOutFk',
'agencyModeFk',
'ref'
]
});
if (!lastInsert.id)
throw new UserError('Unable to clone this travel');
return newTravel.id;
const newTravel = await Self.findById(lastInsert.id, {
fields: [
'id',
'shipped',
'landed',
'warehouseInFk',
'warehouseOutFk',
'agencyModeFk',
'ref'
]
});
return newTravel.id;
} catch (e) {
if (myOptions.transaction) await myOptions.transaction.rollback();
throw e;
}
};
};

View File

@ -5,76 +5,12 @@ describe('Travel cloneWithEntries()', () => {
const travelId = 5;
const currentUserId = 1102;
const ctx = {req: {accessToken: {userId: currentUserId}}};
let travelBefore;
let newTravelId;
// afterAll(async() => {
// try {
// const entries = await models.Entry.find({
// where: {
// travelFk: newTravelId
// }
// });
// const entriesId = entries.map(entry => entry.id);
// // Destroy all entries buys
// await models.Buy.destroyAll({
// where: {
// entryFk: {inq: entriesId}
// }
// });
// // Destroy travel entries
// await models.Entry.destroyAll({
// where: {
// travelFk: newTravelId
// }
// });
// // Destroy new travel
// await models.Travel.destroyById(newTravelId);
// // Restore original travel shipped & landed
// const travel = await models.Travel.findById(travelId);
// await travel.updateAttributes({
// shipped: travelBefore.shipped,
// landed: travelBefore.landed
// });
// } catch (error) {
// console.error(error);
// }
// });
it(`should clone the travel and the containing entries`, async() => {
const tx = await models.Travel.beginTransaction({});
const warehouseThree = 3;
const agencyModeOne = 1;
try {
// pending('#2687 - Cannot make a data rollback because of the triggers');
console.info('TRAVEL_CLONE_WITH_ENTRIES');
// const yesterday = Date.vnNew();
// yesterday.setDate(yesterday.getDate() - 1);
// travelBefore = await models.Travel.findById(travelId);
// await travelBefore.updateAttributes({
// shipped: yesterday,
// landed: yesterday
// });
// const filter = {
// order: 'landed ASC, shipped ASC, travelFk, loadPriority, agencyModeFk, evaNotes',
// };
// const agencyModeFk_BEFORE = await app.models.Travel.extraCommunityFilter({
// args: {
// agencyModeFk: 1
// }
// }, filter);
// const cargoSupplierFk_BEFORE = await app.models.Travel.extraCommunityFilter({
// args: {
// cargoSupplierFk: 1
// }
// }, filter);
const options = {transaction: tx};
newTravelId = await models.Travel.cloneWithEntries(ctx, travelId, options);
const travelEntries = await models.Entry.find({
@ -94,21 +30,7 @@ describe('Travel cloneWithEntries()', () => {
travelFk: newTravelId
}, options);
// Destroy new travel
await models.Travel.destroyById(newTravelId, options);
// const agencyModeFk_AFTER = await app.models.Travel.extraCommunityFilter({
// args: {
// agencyModeFk: 1
// }
// }, filter);
// const cargoSupplierFk_AFTER = await app.models.Travel.extraCommunityFilter({
// args: {
// cargoSupplierFk: 1
// }
// }, filter);
// expect(agencyModeFk_BEFORE.length).toEqual(agencyModeFk_AFTER.length);
// expect(cargoSupplierFk_BEFORE.length).toEqual(cargoSupplierFk_AFTER.length);
} catch (e) {
if (tx) await tx.rollback();
throw e;