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

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
const UserError = require('vn-loopback/util/user-error');
2018-08-07 13:48:55 +00:00
module.exports = Self => {
Self.remoteMethod('removes', {
description: 'Delete an Order Row',
accessType: '',
accepts: [{
arg: 'params',
type: 'object',
required: true,
description: '[Row IDs], actualOrderId',
http: {source: 'body'}
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/removes`,
verb: 'post'
}
});
Self.removes = async params => {
if (!params.rows || !params.rows.length)
throw new UserError('There is nothing to delete');
2018-08-07 13:48:55 +00:00
let isEditable = await Self.app.models.Order.isEditable(params.actualOrderId);
if (!isEditable)
throw new UserError('This order is not editable');
2018-08-07 13:48:55 +00:00
let promises = [];
2018-12-27 11:54:16 +00:00
for (let i = 0; i < params.rows.length; i++)
2018-08-07 13:48:55 +00:00
promises.push(Self.app.models.OrderRow.destroyById(params.rows[i]));
2018-12-27 11:54:16 +00:00
2018-08-07 13:48:55 +00:00
return await Promise.all(promises);
};
};