salix/modules/entry/back/methods/entry/importBuysPreview.js

60 lines
1.6 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('importBuysPreview', {
description: 'Calculates the preview buys for an entry import',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The entry id',
http: {source: 'path'}
},
{
arg: 'buys',
type: ['Object'],
description: 'The buys',
}],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/:id/importBuysPreview`,
verb: 'POST'
}
});
Self.importBuysPreview = async(id, buys, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
for (let buy of buys) {
const packaging = await models.Packaging.findOne({
fields: ['id'],
where: {volume: {gte: buy.volume}},
order: 'volume ASC'
}, myOptions);
if (packaging)
buy.packageFk = packaging.id;
const reference = await models.ItemMatchProperties.findOne({
fields: ['itemFk'],
where: {
name: buy.description,
producer: buy.companyName,
size: buy.size
}
}, myOptions);
if (reference)
buy.itemFk = reference.itemFk;
}
return buys;
};
};