salix/modules/invoiceOut/back/methods/invoiceOut/delete.js

46 lines
1.2 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('delete', {
description: 'Delete a invoiceOut',
accessType: 'WRITE',
accepts: {
arg: 'id',
type: 'string',
required: true,
description: 'The invoiceOut id',
http: {source: 'path'}
},
returns: {
type: 'object',
root: true
},
http: {
path: '/:id/delete',
verb: 'POST'
}
});
Self.delete = async id => {
const tx = await Self.beginTransaction({});
try {
let options = {transaction: tx};
let invoiceOut = await Self.findById(id);
let tickets = await Self.app.models.Ticket.find({where: {refFk: invoiceOut.ref}});
const promises = [];
tickets.forEach(ticket => {
promises.push(ticket.updateAttribute('refFk', null, options));
});
await Promise.all(promises);
await invoiceOut.destroy(options);
await tx.commit();
return tickets;
} catch (e) {
await tx.rollback();
throw e;
}
};
};