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

69 lines
2.2 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) {
const filter = {
fields: [],
where: {
id: expeditionId
},
include: [
{
relation: 'agencyMode',
scope: {
fields: ['code'],
}
}
]
};
const expedition = await models.Expedition.findOne(filter);
const {code} = expedition.agencyMode();
if (code && code.toLowerCase().substring(0, 10) == 'viaexpress') {
2023-11-24 09:53:38 +00:00
const isDeleted = await models.ViaexpressConfig.deleteExpedition(expeditionId);
2023-11-23 10:21:56 +00:00
if (isDeleted === 'true') {
const deletedExpedition = await models.Expedition.destroyById(expeditionId);
deletedExpeditions.push(deletedExpedition);
} else notDeletedExpeditions.push(expeditionId);
} else {
const deletedExpedition = await models.Expedition.destroyById(expeditionId);
deletedExpeditions.push(deletedExpedition);
}
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
};
};