43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
|
const UserError = require('vn-loopback/common/helpers').UserError;
|
||
|
|
||
|
module.exports = Self => {
|
||
|
Self.remoteMethod('addToOrder', {
|
||
|
description: 'Creates rows (lines) for a order',
|
||
|
accessType: 'WRITE',
|
||
|
accepts: [{
|
||
|
arg: 'params',
|
||
|
type: 'object',
|
||
|
required: true,
|
||
|
description: 'order id, [items]',
|
||
|
http: {source: 'body'}
|
||
|
}],
|
||
|
returns: {
|
||
|
type: 'object',
|
||
|
root: true
|
||
|
},
|
||
|
http: {
|
||
|
path: `/addToOrder`,
|
||
|
verb: 'POST'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Self.addToOrder = async params => {
|
||
|
let isEditable = await Self.app.models.Order.isEditable(params.orderFk);
|
||
|
|
||
|
if (!isEditable)
|
||
|
throw new UserError('This order is not editable');
|
||
|
|
||
|
let promises = [];
|
||
|
params.items.forEach(item => {
|
||
|
promises.push(
|
||
|
Self.rawSql(
|
||
|
`CALL hedera.orderAddItem(?, ?, ?, ?)`,
|
||
|
[item.warehouseFk, item.itemFk, item.quantity, params.orderFk]
|
||
|
)
|
||
|
);
|
||
|
});
|
||
|
await Promise.all(promises);
|
||
|
return true;
|
||
|
};
|
||
|
};
|