module.exports = Self => { Self.remoteMethod('createIntrastat', { description: 'Creates a new item intrastat', accessType: 'WRITE', accepts: [ { arg: 'id', type: 'number', required: true, description: 'The item id', http: {source: 'path'} }, { arg: 'intrastatId', type: 'number', required: true }, { arg: 'description', type: 'string', required: true } ], returns: { type: 'boolean', root: true }, http: { path: `/:id/createIntrastat`, verb: 'PATCH' } }); Self.createIntrastat = async(id, intrastatId, description, options) => { const models = Self.app.models; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const country = await models.Country.findOne({ where: {code: 'ES'} }, myOptions); const itemTaxCountry = await models.ItemTaxCountry.findOne({ where: { itemFk: id, countryFk: country.id }, order: 'effectived DESC' }, myOptions); const taxClassCode = await models.TaxClassCode.findOne({ where: { taxClassFk: itemTaxCountry.taxClassFk }, order: 'effectived DESC' }, myOptions); const intrastat = await models.Intrastat.create({ id: intrastatId, description: description, taxClassFk: itemTaxCountry.taxClassFk, taxCodeFk: taxClassCode.taxCodeFk }, myOptions); return intrastat; }; };