salix/modules/item/back/methods/item-shelving/getAlternative.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-03-14 07:36:19 +00:00
module.exports = Self => {
Self.remoteMethod('getAlternative', {
description: 'Returns a list of items and possible alternative locations',
accessType: 'READ',
accepts: [{
arg: 'shelvingFk',
type: 'string',
required: true,
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/getAlternative`,
verb: 'GET'
}
});
Self.getAlternative = async(shelvingFk, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const filterItemShelvings = {
fields: ['id', 'visible', 'itemFk', 'shelvingFk'],
where: {shelvingFk},
include: [
{
relation: 'item',
scope: {
fields: ['longName', 'name', 'size']
}
},
]
};
let itemShelvings = await models.ItemShelving.find(filterItemShelvings, myOptions);
if (itemShelvings) {
const [alternatives] = await models.ItemShelving.rawSql('CALL vn.itemShelving_getAlternatives(?)',
[shelvingFk], myOptions
);
return itemShelvings.map(itemShelving => {
const item = itemShelving.item();
const shelvings = alternatives.filter(alternative => alternative.itemFk == itemShelving.itemFk);
return {
id: itemShelving.id,
itemFk: itemShelving.itemFk,
name: item.name,
size: item.size,
longName: item.longName,
quantity: itemShelving.visible,
shelvings
};
});
}
};
};