2023-11-23 09:26:28 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2022-10-07 08:47:54 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
2023-11-23 09:26:28 +00:00
|
|
|
Self.remoteMethodCtx('deleteExpeditions', {
|
2022-10-07 08:47:54 +00:00
|
|
|
description: 'Delete the selected expeditions',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2022-10-18 09:47:37 +00:00
|
|
|
arg: 'expeditionIds',
|
2022-10-07 08:47:54 +00:00
|
|
|
type: ['number'],
|
|
|
|
required: true,
|
|
|
|
description: 'The expeditions ids to delete'
|
|
|
|
}],
|
|
|
|
http: {
|
|
|
|
path: `/deleteExpeditions`,
|
|
|
|
verb: 'POST'
|
2023-11-23 10:21:56 +00:00
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
2022-10-07 08:47:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-23 09:26:28 +00:00
|
|
|
Self.deleteExpeditions = async(ctx, expeditionIds) => {
|
2022-10-07 08:47:54 +00:00
|
|
|
const models = Self.app.models;
|
2023-11-23 09:26:28 +00:00
|
|
|
const $t = ctx.req.__;
|
|
|
|
const notDeletedExpeditions = [];
|
2023-11-23 10:21:56 +00:00
|
|
|
const deletedExpeditions = [];
|
2023-11-23 09:26:28 +00:00
|
|
|
|
|
|
|
for (let expeditionId of expeditionIds) {
|
2024-07-17 08:14:41 +00:00
|
|
|
try {
|
|
|
|
const expedition = await models.Expedition.findById(expeditionId, {
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'agencyMode',
|
|
|
|
scope: {
|
|
|
|
fields: ['code'],
|
|
|
|
}
|
2023-11-23 09:26:28 +00:00
|
|
|
}
|
2024-07-17 08:14:41 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
const {code} = expedition.agencyMode();
|
2023-11-23 09:26:28 +00:00
|
|
|
|
2024-07-17 08:14:41 +00:00
|
|
|
if (code?.toLowerCase()?.includes('mrw') && expedition.externalId) {
|
|
|
|
const result = await models.MrwConfig.cancelShipment(expeditionId);
|
|
|
|
if (!result) throw new Error('not deleted');
|
|
|
|
}
|
2024-07-16 11:26:20 +00:00
|
|
|
|
2024-07-17 08:14:41 +00:00
|
|
|
if (code?.toLowerCase()?.substring(0, 10) == 'viaexpress') {
|
|
|
|
const result = await models.ViaexpressConfig.deleteExpedition(expeditionId);
|
|
|
|
if (result !== 'true') throw new Error('not deleted');
|
|
|
|
}
|
2023-11-23 09:26:28 +00:00
|
|
|
|
2024-07-22 10:49:50 +00:00
|
|
|
const deletedExpedition = await models.Expedition.destroyById(expeditionId);
|
|
|
|
deletedExpeditions.push(deletedExpedition);
|
2024-07-17 08:14:41 +00:00
|
|
|
} catch (e) {
|
2024-07-16 11:26:20 +00:00
|
|
|
notDeletedExpeditions.push(expeditionId);
|
2023-11-23 10:21:56 +00:00
|
|
|
}
|
2022-10-07 08:47:54 +00:00
|
|
|
}
|
2023-11-23 10:21:56 +00:00
|
|
|
|
2023-11-23 09:26:28 +00:00
|
|
|
if (notDeletedExpeditions.length) {
|
|
|
|
throw new UserError(
|
|
|
|
$t(`It was not able to remove the next expeditions:`, {expeditions: notDeletedExpeditions.join()})
|
|
|
|
);
|
2022-10-07 08:47:54 +00:00
|
|
|
}
|
2023-11-23 10:21:56 +00:00
|
|
|
return deletedExpeditions;
|
2022-10-07 08:47:54 +00:00
|
|
|
};
|
|
|
|
};
|