From d156cb1c30beb5f58be0cd21f2e54eeca3aeba75 Mon Sep 17 00:00:00 2001 From: joan Date: Mon, 2 May 2022 13:52:57 +0200 Subject: [PATCH] Queue transaction --- print/core/router.js | 3 --- print/methods/schedule/invoice.js | 11 +++++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/print/core/router.js b/print/core/router.js index 99f2b3ed66..cd64ba07e1 100644 --- a/print/core/router.js +++ b/print/core/router.js @@ -15,7 +15,6 @@ module.exports = app => { WHERE at.id = ?`; const auth = await db.findOne(query, [token]); - console.log(auth); if (!auth || isTokenExpired(auth.created, auth.ttl)) throw new Error('Invalid authorization token'); @@ -32,8 +31,6 @@ module.exports = app => { locale: auth.lang }; - console.log(response.locals.auth); - next(); } catch (error) { next(error); diff --git a/print/methods/schedule/invoice.js b/print/methods/schedule/invoice.js index df4fe17b01..c76ca85b56 100644 --- a/print/methods/schedule/invoice.js +++ b/print/methods/schedule/invoice.js @@ -29,10 +29,14 @@ module.exports = async function(request, response, next) { LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk WHERE status = ''`); + let connection; let invoiceId; for (const invoiceOut of invoices) { try { invoiceId = invoiceOut.id; + connection = await db.getConnection(); + connection.query('START TRANSACTION'); + const args = Object.assign({ invoiceId: invoiceOut.id, recipientId: invoiceOut.clientFk, @@ -57,7 +61,7 @@ module.exports = async function(request, response, next) { fileName: fileName }); - await db.rawSql('UPDATE invoiceOut SET hasPdf = true WHERE id = ?', [invoiceOut.id]); + connection.query('UPDATE invoiceOut SET hasPdf = true WHERE id = ?', [invoiceOut.id]); const isToBeMailed = invoiceOut.recipient && invoiceOut.salesPersonFk && invoiceOut.isToBeMailed; @@ -93,8 +97,11 @@ module.exports = async function(request, response, next) { SET status = "printed", printed = NOW() WHERE invoiceFk = ?`; - await db.rawSql(sql, [invoiceOut.id]); + connection.query(sql, [invoiceOut.id]); + connection.query('COMMIT'); } catch (error) { + connection.query('ROLLBACK'); + connection.release(); sql = `UPDATE invoiceOut_queue SET status = ? WHERE invoiceFk = ?`;