salix/modules/order/back/methods/order-row/addToOrder.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

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'
}
});
2021-12-15 14:29:22 +00:00
Self.addToOrder = async(params, options) => {
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const isEditable = await Self.app.models.Order.isEditable(params.orderFk, myOptions);
2018-09-17 12:30:39 +00:00
if (!isEditable)
throw new UserError('This order is not editable');
2021-12-15 14:29:22 +00:00
const promises = [];
for (const item of params.items) {
2018-09-17 12:30:39 +00:00
promises.push(
Self.rawSql(
2019-05-30 06:41:08 +00:00
`CALL hedera.order_addItem(?, ?, ?, ?)`,
2021-12-15 14:29:22 +00:00
[params.orderFk, item.warehouseFk, item.itemFk, item.quantity],
myOptions
2018-09-17 12:30:39 +00:00
)
);
2021-12-15 14:29:22 +00:00
}
2018-09-17 12:30:39 +00:00
await Promise.all(promises);
2021-12-15 14:29:22 +00:00
2018-09-17 12:30:39 +00:00
return true;
};
};