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) {
|
|
|
|
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 09:26:28 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|