2018-12-27 11:54:16 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2018-09-17 12:30:39 +00:00
|
|
|
|
|
|
|
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(?, ?, ?, ?)`,
|
2018-10-15 08:48:06 +00:00
|
|
|
[params.orderFk, item.warehouseFk, item.itemFk, item.quantity]
|
2018-09-17 12:30:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|