#2687 - Travel CloneWithEntries #1887
|
@ -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,62 +25,70 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.cloneWithEntries = async(ctx, id) => {
|
Self.cloneWithEntries = async(ctx, id, options) => {
|
||||||
const conn = Self.dataSource.connector;
|
try {
|
||||||
const travel = await Self.findById(id, {
|
const conn = Self.dataSource.connector;
|
||||||
fields: [
|
const myOptions = {};
|
||||||
'id',
|
|
||||||
'shipped',
|
|
||||||
'landed',
|
|
||||||
'warehouseInFk',
|
|
||||||
'warehouseOutFk',
|
|
||||||
'agencyModeFk',
|
|
||||||
'ref'
|
|
||||||
]
|
|
||||||
});
|
|
||||||
const started = Date.vnNew();
|
|
||||||
const ended = Date.vnNew();
|
|
||||||
|
|
||||||
if (!travel)
|
if (typeof options == 'object')
|
||||||
throw new UserError('Travel not found');
|
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 = [];
|
if (!travel)
|
||||||
let stmt;
|
throw new UserError('Travel not found');
|
||||||
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;
|
|
||||||
|
|
||||||
const sql = ParameterizedSQL.join(stmts, ';');
|
let stmts = [];
|
||||||
const result = await conn.executeStmt(sql);
|
let stmt;
|
||||||
const [lastInsert] = result[newTravelIndex];
|
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)
|
const sql = ParameterizedSQL.join(stmts, ';');
|
||||||
throw new UserError('Unable to clone this travel');
|
const result = await conn.executeStmt(sql, myOptions);
|
||||||
|
const [lastInsert] = result[newTravelIndex];
|
||||||
|
|
||||||
const newTravel = await Self.findById(lastInsert.id, {
|
if (!lastInsert.id)
|
||||||
fields: [
|
throw new UserError('Unable to clone this travel');
|
||||||
'id',
|
|
||||||
'shipped',
|
|
||||||
'landed',
|
|
||||||
'warehouseInFk',
|
|
||||||
'warehouseOutFk',
|
|
||||||
'agencyModeFk',
|
|
||||||
'ref'
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -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