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

73 lines
2.1 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: [{
2024-11-13 10:51:23 +00:00
arg: 'shelvingCode',
2024-03-14 07:36:19 +00:00
type: 'string',
required: true,
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/getAlternative`,
verb: 'GET'
}
});
2024-11-13 10:51:23 +00:00
Self.getAlternative = async(shelvingCode, options) => {
2024-03-14 07:36:19 +00:00
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2024-11-13 10:51:23 +00:00
const shelving = await models.Shelving.findOne({
where: {
code: shelvingCode
}
});
if (!shelving) return [];
const {id: shelvingFk} = shelving;
2024-03-14 07:36:19 +00:00
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(?)',
2024-11-13 10:51:23 +00:00
[shelvingCode], myOptions
2024-03-14 07:36:19 +00:00
);
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
};
});
}
};
};