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

110 lines
3.3 KiB
JavaScript
Raw Normal View History

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'
}
});
2021-07-12 10:54:59 +00:00
Self.getSummary = async(ctx, id, options) => {
2020-05-26 08:37:52 +00:00
const models = Self.app.models;
2021-07-12 10:54:59 +00:00
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: {
2022-07-12 09:52:47 +00:00
fields: ['id', 'name', 'workerFk'],
include: [{
relation: 'worker',
scope: {
2023-08-30 10:48:57 +00:00
fields: ['id'],
include: {
relation: 'user',
scope: {
2020-09-03 13:11:16 +00:00
fields: ['name']
}
}
}
}]
}
},
{relation: 'intrastat'},
2018-07-24 10:56:07 +00:00
{relation: 'itemBarcode'},
{relation: 'expense'},
{relation: 'origin'},
{relation: 'taxes',
scope: {
fields: ['id', 'countryFk', 'taxClassFk'],
include: [{
relation: 'country',
scope: {
2024-06-26 07:03:06 +00:00
fields: ['id', 'name']
}
}, {
relation: 'taxClass',
scope: {
fields: ['id', 'description']
}
}]
}
}
]
};
2021-07-12 10:54:59 +00:00
promises.push(models.Item.findById(id, filter, myOptions));
// Tags
filter = {
where: {
itemFk: id
},
2018-12-17 13:15:39 +00:00
order: 'priority',
include: {
relation: 'tag'
}
};
2021-07-12 10:54:59 +00:00
promises.push(models.ItemTag.find(filter, myOptions));
// Botanical
filter = {
where: {itemFk: id},
include: [{relation: 'genus'}, {relation: 'specie'}]
};
2021-07-12 10:54:59 +00:00
promises.push(models.ItemBotanical.find(filter, myOptions));
let res = await Promise.all(promises);
2021-07-12 10:54:59 +00:00
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);
2020-05-26 08:37:52 +00:00
summary.available = res.available;
summary.visible = res.visible;
2021-07-12 10:54:59 +00:00
return summary;
};
};