refs #2687 feat: remove procedure argument
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Javier Segarra 2023-12-12 07:34:20 +01:00
parent 30dbc8a587
commit 8bd9becb74
3 changed files with 62 additions and 132 deletions

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,8 +25,13 @@ module.exports = Self => {
}
});
Self.cloneWithEntries = async(ctx, id) => {
Self.cloneWithEntries = async(ctx, id, options) => {
try {
const conn = Self.dataSource.connector;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const travel = await Self.findById(id, {
fields: [
'id',
@ -45,24 +51,23 @@ module.exports = Self => {
let stmts = [];
let stmt;
let tx = await Self.beginTransaction({});
let tx = options.transaction;
stmt = new ParameterizedSQL(
`CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [
`CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [
id,
started,
ended,
travel.warehouseOutFk,
travel.warehouseInFk,
travel.ref,
travel.agencyModeFk,
!!tx.id
travel.agencyModeFk
]
);
stmts.push(stmt);
const newTravelIndex = stmts.push('SELECT @vTravelFk AS id') - 1;
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql);
const result = await conn.executeStmt(sql, myOptions);
const [lastInsert] = result[newTravelIndex];
if (!lastInsert.id)
@ -81,5 +86,9 @@ module.exports = Self => {
});
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;