75 lines
2.1 KiB
JavaScript
75 lines
2.1 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethod('makeMulti', {
|
|
description: 'Add a record or update it if it already exists.',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'shelvingFk',
|
|
type: 'number',
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'items',
|
|
type: ['number'],
|
|
required: true,
|
|
description: 'array of item foreign keys'
|
|
},
|
|
{
|
|
arg: 'warehouseFk',
|
|
type: 'number',
|
|
required: true
|
|
}],
|
|
|
|
http: {
|
|
path: `/makeMulti`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.makeMulti = async(shelvingFk, items, warehouseFk, options) => {
|
|
const myOptions = {};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
const discardItems = [];
|
|
|
|
try {
|
|
for (let item of items) {
|
|
if (!discardItems.includes(item)) {
|
|
let quantity = items.reduce((acc, cur) => {
|
|
return acc + (cur === item ? 1 : 0);
|
|
}, 0);
|
|
discardItems.push(item);
|
|
|
|
let [result] = await Self.rawSql('SELECT vn.itemPacking(?, ?)', [item, warehouseFk]);
|
|
let packing;
|
|
|
|
if (result) packing = Object.values(result)[0];
|
|
if (!packing) packing = 1;
|
|
|
|
quantity = quantity * packing;
|
|
|
|
await Self.rawSql('CALL vn.itemShelving_add(?, ?, ?, NULL, NULL, ?, ?)',
|
|
[shelvingFk,
|
|
item,
|
|
quantity,
|
|
packing,
|
|
warehouseFk]
|
|
);
|
|
}
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|