salix/modules/item/back/methods/item/getSummary.js

110 lines
3.3 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('getSummary', {
description: 'return the item information',
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(ctx, id, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const promises = [];
const summary = {};
// Item basic data and taxes
let filter = {
include: [
{relation: 'itemType',
scope: {
fields: ['id', 'name', 'workerFk'],
include: [{
relation: 'worker',
scope: {
fields: ['id'],
include: {
relation: 'user',
scope: {
fields: ['name']
}
}
}
}]
}
},
{relation: 'intrastat'},
{relation: 'itemBarcode'},
{relation: 'expense'},
{relation: 'origin'},
{relation: 'taxes',
scope: {
fields: ['id', 'countryFk', 'taxClassFk'],
include: [{
relation: 'country',
scope: {
fields: ['id', 'name']
}
}, {
relation: 'taxClass',
scope: {
fields: ['id', 'description']
}
}]
}
}
]
};
promises.push(models.Item.findById(id, filter, myOptions));
// Tags
filter = {
where: {
itemFk: id
},
order: 'priority',
include: {
relation: 'tag'
}
};
promises.push(models.ItemTag.find(filter, myOptions));
// Botanical
filter = {
where: {itemFk: id},
include: [{relation: 'genus'}, {relation: 'specie'}]
};
promises.push(models.ItemBotanical.find(filter, myOptions));
let res = await Promise.all(promises);
summary.item = res[0];
summary.tags = res[1];
[summary.botanical] = res[2];
const itemConfig = await models.ItemConfig.findOne(null, myOptions);
res = await models.Item.getVisibleAvailable(summary.item.id, itemConfig.warehouseFk, undefined, myOptions);
summary.available = res.available;
summary.visible = res.visible;
return summary;
};
};