refs #5811 feat: al borrar expedicion borrar de viaexpress #1754
|
@ -1,4 +1,6 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
require('../methods/viaexpress-config/internationalExpedition')(Self);
|
require('../methods/viaexpress-config/internationalExpedition')(Self);
|
||||||
require('../methods/viaexpress-config/renderer')(Self);
|
require('../methods/viaexpress-config/renderer')(Self);
|
||||||
|
require('../methods/viaexpress-config/deleteShipment')(Self);
|
||||||
|
require('../methods/viaexpress-config/deleteShipmentRenderer')(Self);
|
||||||
};
|
};
|
||||||
|
|
|
@ -199,5 +199,6 @@
|
||||||
"You can only add negative amounts in refund tickets": "You can only add negative amounts in refund tickets",
|
"You can only add negative amounts in refund tickets": "You can only add negative amounts in refund tickets",
|
||||||
"Try again": "Try again",
|
"Try again": "Try again",
|
||||||
"keepPrice": "keepPrice",
|
"keepPrice": "keepPrice",
|
||||||
"Cannot past travels with entries": "Cannot past travels with entries"
|
"Cannot past travels with entries": "Cannot past travels with entries",
|
||||||
|
"It was not able to remove the next expeditions:": "It was not able to remove the next expeditions: {{expeditions}}"
|
||||||
}
|
}
|
|
@ -328,5 +328,6 @@
|
||||||
"User disabled": "Usuario desactivado",
|
"User disabled": "Usuario desactivado",
|
||||||
"The amount cannot be less than the minimum": "La cantidad no puede ser menor que la cantidad mínima",
|
"The amount cannot be less than the minimum": "La cantidad no puede ser menor que la cantidad mínima",
|
||||||
"quantityLessThanMin": "La cantidad no puede ser menor que la cantidad mínima",
|
"quantityLessThanMin": "La cantidad no puede ser menor que la cantidad mínima",
|
||||||
"Cannot past travels with entries": "No se pueden pasar envíos con entradas"
|
"Cannot past travels with entries": "No se pueden pasar envíos con entradas",
|
||||||
|
"It was not able to remove the next expeditions:": "No se pudo eliminar las siguientes expediciones: {{expeditions}}"
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('deleteExpeditions', {
|
Self.remoteMethodCtx('deleteExpeditions', {
|
||||||
description: 'Delete the selected expeditions',
|
description: 'Delete the selected expeditions',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
|
@ -9,44 +10,51 @@ module.exports = Self => {
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The expeditions ids to delete'
|
description: 'The expeditions ids to delete'
|
||||||
}],
|
}],
|
||||||
returns: {
|
|
||||||
type: ['object'],
|
|
||||||
root: true
|
|
||||||
},
|
|
||||||
http: {
|
http: {
|
||||||
path: `/deleteExpeditions`,
|
path: `/deleteExpeditions`,
|
||||||
verb: 'POST'
|
verb: 'POST'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.deleteExpeditions = async(expeditionIds, options) => {
|
Self.deleteExpeditions = async(ctx, expeditionIds) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const myOptions = {};
|
const $t = ctx.req.__;
|
||||||
let tx;
|
const notDeletedExpeditions = [];
|
||||||
|
|
||||||
if (typeof options == 'object')
|
for (let expeditionId of expeditionIds) {
|
||||||
Object.assign(myOptions, options);
|
const filter = {
|
||||||
|
fields: [],
|
||||||
|
where: {
|
||||||
|
id: expeditionId
|
||||||
|
},
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
relation: 'agencyMode',
|
||||||
|
scope: {
|
||||||
|
fields: ['code'],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
if (!myOptions.transaction) {
|
const expedition = await models.Expedition.findOne(filter);
|
||||||
tx = await Self.beginTransaction({});
|
const {code} = expedition.agencyMode();
|
||||||
myOptions.transaction = tx;
|
|
||||||
|
if (code && code.toLowerCase().substring(0, 10) == 'viaexpress') {
|
||||||
|
const isDeleted = await models.ViaexpressConfig.deleteShipment(expeditionId);
|
||||||
|
|
||||||
|
if (isDeleted === 'true')
|
||||||
|
await models.Expedition.destroyById(expeditionId);
|
||||||
|
|
||||||
|
else if (!isDeleted)
|
||||||
|
notDeletedExpeditions.push(expeditionId);
|
||||||
|
} else
|
||||||
|
await models.Expedition.destroyById(expeditionId);
|
||||||
}
|
}
|
||||||
|
if (notDeletedExpeditions.length) {
|
||||||
try {
|
throw new UserError(
|
||||||
const promises = [];
|
$t(`It was not able to remove the next expeditions:`, {expeditions: notDeletedExpeditions.join()})
|
||||||
for (let expeditionId of expeditionIds) {
|
);
|
||||||
const deletedExpedition = models.Expedition.destroyById(expeditionId, myOptions);
|
|
||||||
promises.push(deletedExpedition);
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletedExpeditions = await Promise.all(promises);
|
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
|
||||||
|
|
||||||
return deletedExpeditions;
|
|
||||||
} catch (e) {
|
|
||||||
if (tx) await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
},
|
},
|
||||||
"agencyMode": {
|
"agencyMode": {
|
||||||
"type": "belongsTo",
|
"type": "belongsTo",
|
||||||
"model": "agency-mode",
|
"model": "AgencyMode",
|
||||||
"foreignKey": "agencyModeFk"
|
"foreignKey": "agencyModeFk"
|
||||||
},
|
},
|
||||||
"worker": {
|
"worker": {
|
||||||
|
|
Loading…
Reference in New Issue