This commit is contained in:
parent
30dbc8a587
commit
8bd9becb74
|
@ -10,7 +10,6 @@ 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,
|
||||||
IN vTx BOOL,
|
|
||||||
OUT vNewTravelFk INT)
|
OUT vNewTravelFk INT)
|
||||||
BEGIN
|
BEGIN
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -11,8 +11,9 @@ module.exports = Self => {
|
||||||
type: 'number',
|
type: 'number',
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The original travel id',
|
description: 'The original travel id',
|
||||||
http: {source: 'path'}
|
http: {source: 'path'},
|
||||||
}],
|
},
|
||||||
|
],
|
||||||
returns: {
|
returns: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
description: 'The new cloned travel id',
|
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 conn = Self.dataSource.connector;
|
||||||
|
const myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
const travel = await Self.findById(id, {
|
const travel = await Self.findById(id, {
|
||||||
fields: [
|
fields: [
|
||||||
'id',
|
'id',
|
||||||
|
@ -45,24 +51,23 @@ module.exports = Self => {
|
||||||
|
|
||||||
let stmts = [];
|
let stmts = [];
|
||||||
let stmt;
|
let stmt;
|
||||||
let tx = await Self.beginTransaction({});
|
let tx = options.transaction;
|
||||||
stmt = new ParameterizedSQL(
|
stmt = new ParameterizedSQL(
|
||||||
`CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [
|
`CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [
|
||||||
id,
|
id,
|
||||||
started,
|
started,
|
||||||
ended,
|
ended,
|
||||||
travel.warehouseOutFk,
|
travel.warehouseOutFk,
|
||||||
travel.warehouseInFk,
|
travel.warehouseInFk,
|
||||||
travel.ref,
|
travel.ref,
|
||||||
travel.agencyModeFk,
|
travel.agencyModeFk
|
||||||
!!tx.id
|
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
stmts.push(stmt);
|
stmts.push(stmt);
|
||||||
const newTravelIndex = stmts.push('SELECT @vTravelFk AS id') - 1;
|
const newTravelIndex = stmts.push('SELECT @vTravelFk AS id') - 1;
|
||||||
|
|
||||||
const sql = ParameterizedSQL.join(stmts, ';');
|
const sql = ParameterizedSQL.join(stmts, ';');
|
||||||
const result = await conn.executeStmt(sql);
|
const result = await conn.executeStmt(sql, myOptions);
|
||||||
const [lastInsert] = result[newTravelIndex];
|
const [lastInsert] = result[newTravelIndex];
|
||||||
|
|
||||||
if (!lastInsert.id)
|
if (!lastInsert.id)
|
||||||
|
@ -81,5 +86,9 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
return newTravel.id;
|
return newTravel.id;
|
||||||
|
} catch (e) {
|
||||||
|
if (myOptions.transaction) await myOptions.transaction.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,76 +5,12 @@ describe('Travel cloneWithEntries()', () => {
|
||||||
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 travelBefore;
|
|
||||||
let newTravelId;
|
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() => {
|
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 {
|
||||||
// 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};
|
const options = {transaction: tx};
|
||||||
newTravelId = await models.Travel.cloneWithEntries(ctx, travelId, options);
|
newTravelId = await models.Travel.cloneWithEntries(ctx, travelId, options);
|
||||||
const travelEntries = await models.Entry.find({
|
const travelEntries = await models.Entry.find({
|
||||||
|
@ -94,21 +30,7 @@ describe('Travel cloneWithEntries()', () => {
|
||||||
travelFk: newTravelId
|
travelFk: newTravelId
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
// Destroy new travel
|
|
||||||
await models.Travel.destroyById(newTravelId, options);
|
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) {
|
} catch (e) {
|
||||||
if (tx) await tx.rollback();
|
if (tx) await tx.rollback();
|
||||||
throw e;
|
throw e;
|
||||||
|
|
Loading…
Reference in New Issue