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

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-11-30 15:25:13 +00:00
module.exports = Self => {
Self.remoteMethod('getAlternative', {
2023-11-30 15:25:13 +00:00
description: 'Returns a list of items and possible alternative locations',
accessType: 'READ',
accepts: [{
arg: 'shelvingFk',
type: 'string',
required: true,
}],
returns: {
2023-12-01 09:14:32 +00:00
type: ['object'],
2023-11-30 15:25:13 +00:00
root: true
},
http: {
path: `/getAlternative`,
2023-12-01 10:38:30 +00:00
verb: 'GET'
2023-11-30 15:25:13 +00:00
}
});
Self.getAlternative = async(shelvingFk, options) => {
2023-11-30 15:25:13 +00:00
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const filterItemShelvings = {
fields: ['id', 'visible', 'itemFk', 'shelvingFk'],
2023-11-30 15:25:13 +00:00
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 => {
2023-11-30 15:25:13 +00:00
const item = itemShelving.item();
const shelvings = alternatives.filter(alternative => alternative.itemFk == itemShelving.itemFk);
2023-12-01 09:14:32 +00:00
2023-11-30 15:25:13 +00:00
return {
2023-12-01 09:14:32 +00:00
id: itemShelving.id,
2024-01-03 11:50:53 +00:00
itemFk: itemShelving.itemFk,
2024-02-27 09:29:49 +00:00
name: item.name,
size: item.size,
longName: item.longName,
2023-12-01 09:14:32 +00:00
quantity: itemShelving.visible,
shelvings
2023-11-30 15:25:13 +00:00
};
});
}
};
};