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',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2018-08-07 13:48:55 +00:00
|
|
|
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 => {
|
2021-12-15 14:29:22 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
2021-12-16 09:56:10 +00:00
|
|
|
try {
|
|
|
|
if (!params.rows || !params.rows.length)
|
|
|
|
throw new UserError('There is nothing to delete');
|
2018-08-07 13:48:55 +00:00
|
|
|
|
2021-12-16 09:56:10 +00:00
|
|
|
const isEditable = await Self.app.models.Order.isEditable(params.actualOrderId, myOptions);
|
2018-10-24 12:10:08 +00:00
|
|
|
|
2021-12-16 09:56:10 +00:00
|
|
|
if (!isEditable)
|
|
|
|
throw new UserError('This order is not editable');
|
2018-08-07 13:48:55 +00:00
|
|
|
|
2021-12-16 09:56:10 +00:00
|
|
|
const promises = [];
|
|
|
|
for (let i = 0; i < params.rows.length; i++)
|
|
|
|
promises.push(Self.app.models.OrderRow.destroyById(params.rows[i], myOptions));
|
2018-12-27 11:54:16 +00:00
|
|
|
|
2021-12-16 09:56:10 +00:00
|
|
|
const deletions = await Promise.all(promises);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return deletions;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-08-07 13:48:55 +00:00
|
|
|
};
|
|
|
|
};
|