52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('deleteBuys', {
|
|
description: 'Deletes the selected buys',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'buys',
|
|
type: ['object'],
|
|
required: true,
|
|
description: 'The buys to delete'
|
|
}],
|
|
returns: {
|
|
type: ['object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/deleteBuys`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.deleteBuys = async(ctx, options) => {
|
|
const models = Self.app.models;
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const promises = [];
|
|
for (let buy of ctx.args.buys) {
|
|
const buysToDelete = models.Buy.destroyById(buy.id, myOptions);
|
|
promises.push(buysToDelete);
|
|
}
|
|
|
|
const deleted = await Promise.all(promises);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return deleted;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|