59 lines
1.4 KiB
JavaScript
59 lines
1.4 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, options) => {
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const invoiceOut = await Self.findById(id, {}, myOptions);
|
|
const tickets = await Self.app.models.Ticket.find({
|
|
where: {refFk: invoiceOut.ref}
|
|
}, myOptions);
|
|
|
|
const promises = [];
|
|
|
|
for (let ticket of tickets)
|
|
promises.push(ticket.updateAttribute('refFk', null, myOptions));
|
|
|
|
await Promise.all(promises);
|
|
|
|
await invoiceOut.destroy(myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return tickets;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|