salix/modules/claim/back/methods/claim-end/deleteClamedSales.js

50 lines
1.3 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('deleteClamedSales', {
description: 'Deletes the claimed sales',
accessType: 'WRITE',
accepts: [{
arg: 'sales',
type: ['object'],
required: true,
description: 'The sales to remove'
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/deleteClamedSales`,
verb: 'POST'
}
});
Self.deleteClamedSales = async(ctx, sales, options) => {
const models = Self.app.models;
const myOptions = {};
const tx = await Self.beginTransaction({});
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction)
myOptions.transaction = tx;
try {
const promises = [];
for (let sale of sales) {
const deletedSale = models.ClaimEnd.destroyById(sale.id, myOptions);
promises.push(deletedSale);
}
const deletedSales = await Promise.all(promises);
if (tx) await tx.commit();
return deletedSales;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};