salix/modules/ticket/back/methods/expedition/deleteExpeditions.js

69 lines
2.4 KiB
JavaScript
Raw Normal View History

const UserError = require('vn-loopback/util/user-error');
2022-10-07 08:47:54 +00:00
module.exports = Self => {
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
}
});
Self.deleteExpeditions = async(ctx, expeditionIds) => {
2022-10-07 08:47:54 +00:00
const models = Self.app.models;
const $t = ctx.req.__;
const notDeletedExpeditions = [];
2023-11-23 10:21:56 +00:00
const deletedExpeditions = [];
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'],
}
}
2024-07-17 08:14:41 +00:00
]
});
const {code} = expedition.agencyMode();
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-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 10:21:56 +00:00
const deletedExpedition = await models.Expedition.destroyById(expeditionId);
deletedExpeditions.push(deletedExpedition);
} catch (error) {
await Self.rawSql('CALL util.debugAdd(?,?);', ['deleteExpeditions', error]);
2024-07-17 08:14:41 +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
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
};
};