From d9ffca8a3596514e2da1626b80a487c02abc4f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Tue, 30 Mar 2021 14:52:40 +0100 Subject: [PATCH] fix: use db tx to chain queries --- lib/cashbox-batches.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/cashbox-batches.js b/lib/cashbox-batches.js index a6177cc1..67cd027c 100644 --- a/lib/cashbox-batches.js +++ b/lib/cashbox-batches.js @@ -9,10 +9,12 @@ function createCashboxBatch (rec) { cash_in_txs.device_id = $2 AND bills.cashbox_batch_id IS NULL ` - return db.oneOrNone(sql, [rec.deviceId]) - .then(res => { - return db.oneOrNone(sql2, [res.id, res.device_id]) - }) + return db.tx(async t => { + const newBatch = await t.oneOrNone(sql, [rec.deviceId]) + return t.oneOrNone(sql2, [newBatch.id, newBatch.device_id]) + }) + .then(data => data) + .catch(error => error) } module.exports = { createCashboxBatch }