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

46 lines
1.2 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: 'GET'
}
});
Self.importBuysPreview = async(id, buys, options) => {
const models = Self.app.models;
let 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);
buy.packageFk = packaging.id;
}
return buys;
};
};