module.exports = Self => { Self.remoteMethod('getSummary', { description: 'Updates the item taxes', accessType: 'READ', accepts: [{ arg: 'id', type: 'number', required: true, description: 'The item id', http: {source: 'path'} }], returns: { type: 'object', root: true }, http: { path: `/:id/getSummary`, verb: 'GET' } }); Self.getSummary = async id => { let promises = []; let summary = {}; // Item basic data and taxes let filter = { where: {id: id}, include: [ {relation: 'itemType', scope: { fields: ['id', 'name', 'workerFk', 'warehouseFk'], include: [{ relation: 'worker', scope: { fields: ['id', 'name', 'firstName'] } }] } }, {relation: 'intrastat'}, {relation: 'itemBarcode'}, {relation: 'expence'}, {relation: 'origin'}, {relation: 'taxes', scope: { fields: ['id', 'countryFk', 'taxClassFk'], include: [{ relation: 'country', scope: { fields: ['id', 'country'] } }, { relation: 'taxClass', scope: { fields: ['id', 'description'] } }] } } ] }; promises.push(Self.app.models.Item.find(filter)); // Tags filter = { where: { itemFk: id }, include: { relation: 'tag' } }; promises.push(Self.app.models.ItemTag.find(filter)); // Botanical filter = { where: {itemFk: id}, include: [{relation: 'genus'}, {relation: 'specie'}] }; promises.push(Self.app.models.ItemBotanical.find(filter)); // Niches filter = { where: {itemFk: id}, include: {relation: 'warehouse'} }; promises.push(Self.app.models.ItemNiche.find(filter)); let res = await Promise.all(promises); [summary.item] = res[0]; summary.tags = res[1]; [summary.botanical] = res[2]; summary.niches = res[3]; // Visible Avaible let query = ` CALL vn.getItemVisibleAvailable(?,curdate(),?,?)`; let options = [summary.item.id, summary.item.itemType().warehouseFk, false]; [res] = await Self.rawSql(query, options); summary.available = res[0].available ? res[0].available : '-'; summary.visible = res[0].visible ? res[0].visible : '-'; return summary; }; };