const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('getPallet', { description: 'Get pallet', accessType: 'READ', accepts: [ { arg: 'expeditionFk', type: 'integer', }, ], http: { path: `/getPallet`, verb: 'GET' }, returns: { type: 'object', root: true }, }); Self.getPallet = async expeditionFk => { try { const pallet = await Self.findOne({ fields: ['truckFk'], where: { id: expeditionFk }, include: [ { relation: 'expeditionTruck', scope: { fields: ['eta', 'description'] } } ], }); if (pallet) { const truck = pallet.expeditionTruck(); return { truckFk: pallet.truckFk, eta: truck.eta, description: truck.description }; } throw new UserError('palletDoesNotExist'); } catch (e) { return {message: e.message}; } }; };