80 lines
2.2 KiB
JavaScript
80 lines
2.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, 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 models = Self.app.models;
|
|
const tickets = await models.Ticket.find({
|
|
where: {refFk: invoiceOut.ref}
|
|
}, myOptions);
|
|
|
|
const [bookEntry] = await models.Xdiario.find({
|
|
where: {
|
|
SERIE: invoiceOut.ref[0],
|
|
FACTURA: invoiceOut.ref.slice(1)
|
|
}
|
|
}, myOptions);
|
|
const promises = [];
|
|
|
|
for (let ticket of tickets)
|
|
promises.push(ticket.updateAttribute('refFk', null, myOptions));
|
|
|
|
await Promise.all(promises);
|
|
await invoiceOut.destroy(myOptions);
|
|
|
|
if (bookEntry) {
|
|
if (bookEntry.enlazadoSage) {
|
|
const params = {
|
|
bookEntry: bookEntry.ASIEN,
|
|
invoiceOutRef: invoiceOut.ref
|
|
};
|
|
await Self.rawSql(`SELECT util.notification_send('book-entry-deleted', ?, NULL)`,
|
|
[JSON.stringify(params)],
|
|
myOptions);
|
|
}
|
|
|
|
await models.Xdiario.destroyAll({
|
|
ASIEN: bookEntry.ASIEN
|
|
});
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return tickets;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|