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) { 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') { const isDeleted = await models.ViaexpressConfig.deleteExpedition(expeditionId); 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); } } if (notDeletedExpeditions.length) { throw new UserError( $t(`It was not able to remove the next expeditions:`, {expeditions: notDeletedExpeditions.join()}) ); } return deletedExpeditions; }; };