68 lines
2.3 KiB
JavaScript
68 lines
2.3 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('deleteExpeditions', {
|
|
description: 'Delete the selected expeditions',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'expeditionIds',
|
|
type: ['number'],
|
|
required: true,
|
|
description: 'The expeditions ids to delete'
|
|
}],
|
|
http: {
|
|
path: `/deleteExpeditions`,
|
|
verb: 'POST'
|
|
},
|
|
returns: {
|
|
type: ['object'],
|
|
root: true
|
|
}
|
|
});
|
|
|
|
Self.deleteExpeditions = async(ctx, expeditionIds) => {
|
|
const models = Self.app.models;
|
|
const $t = ctx.req.__;
|
|
const notDeletedExpeditions = [];
|
|
const deletedExpeditions = [];
|
|
|
|
for (let expeditionId of expeditionIds) {
|
|
try {
|
|
const expedition = await models.Expedition.findById(expeditionId, {
|
|
include: [
|
|
{
|
|
relation: 'agencyMode',
|
|
scope: {
|
|
fields: ['code'],
|
|
}
|
|
}
|
|
]
|
|
});
|
|
const {code} = expedition.agencyMode();
|
|
|
|
if (code?.toLowerCase()?.includes('mrw') && expedition.externalId) {
|
|
const result = await models.MrwConfig.cancelShipment(expeditionId);
|
|
if (!result) throw new Error('not deleted');
|
|
}
|
|
|
|
if (code?.toLowerCase()?.substring(0, 10) == 'viaexpress') {
|
|
const result = await models.ViaexpressConfig.deleteExpedition(expeditionId);
|
|
if (result !== 'true') throw new Error('not deleted');
|
|
}
|
|
|
|
const deletedExpedition = await models.Expedition.destroyById(expeditionId);
|
|
deletedExpeditions.push(deletedExpedition);
|
|
} catch (e) {
|
|
notDeletedExpeditions.push(expeditionId);
|
|
}
|
|
}
|
|
|
|
if (notDeletedExpeditions.length) {
|
|
throw new UserError(
|
|
$t(`It was not able to remove the next expeditions:`, {expeditions: notDeletedExpeditions.join()})
|
|
);
|
|
}
|
|
return deletedExpeditions;
|
|
};
|
|
};
|