From 4f12bcf4cf8dce611c0ec63df7438697e66919db Mon Sep 17 00:00:00 2001 From: csrapr <26280794+csrapr@users.noreply.github.com> Date: Thu, 15 Apr 2021 14:17:42 +0100 Subject: [PATCH] Chore: move tx code into atomic function --- lib/cash-in/cash-in-atomic.js | 6 +++++- lib/cash-in/cash-in-tx.js | 6 +----- lib/cash-out/cash-out-atomic.js | 8 +++++--- lib/cash-out/cash-out-tx.js | 6 +----- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/cash-in/cash-in-atomic.js b/lib/cash-in/cash-in-atomic.js index c4238bb3..8a23360c 100644 --- a/lib/cash-in/cash-in-atomic.js +++ b/lib/cash-in/cash-in-atomic.js @@ -1,6 +1,7 @@ const _ = require('lodash/fp') const pgp = require('pg-promise')() +const db = require('../db') const E = require('../error') const cashInLow = require('./cash-in-low') @@ -8,6 +9,9 @@ const cashInLow = require('./cash-in-low') module.exports = { atomic } function atomic (machineTx, pi) { + const TransactionMode = pgp.txMode.TransactionMode + const isolationLevel = pgp.txMode.isolationLevel + const mode = new TransactionMode({ tiLevel: isolationLevel.serializable }) function transaction (t) { const sql = 'select * from cash_in_txs where id=$1' const sql2 = 'select * from bills where cash_in_txs_id=$1' @@ -29,7 +33,7 @@ function atomic (machineTx, pi) { }) }) } - return transaction + return db.tx({ mode }, transaction) } function insertNewBills (t, billRows, machineTx) { diff --git a/lib/cash-in/cash-in-tx.js b/lib/cash-in/cash-in-tx.js index cde148cb..78339e9c 100644 --- a/lib/cash-in/cash-in-tx.js +++ b/lib/cash-in/cash-in-tx.js @@ -19,11 +19,7 @@ const MAX_PENDING = 10 module.exports = { post, monitorPending, cancel, PENDING_INTERVAL } function post (machineTx, pi) { - const TransactionMode = pgp.txMode.TransactionMode - const isolationLevel = pgp.txMode.isolationLevel - const mode = new TransactionMode({ tiLevel: isolationLevel.serializable }) - - return db.tx({ mode }, cashInAtomic.atomic(machineTx, pi)) + return cashInAtomic.atomic(machineTx, pi) .then(r => { const updatedTx = r.tx let blacklisted = false diff --git a/lib/cash-out/cash-out-atomic.js b/lib/cash-out/cash-out-atomic.js index a72d28fc..b03d51f5 100644 --- a/lib/cash-out/cash-out-atomic.js +++ b/lib/cash-out/cash-out-atomic.js @@ -1,6 +1,7 @@ const _ = require('lodash/fp') const pgp = require('pg-promise')() +const db = require('../db') const E = require('../error') const socket = require('../socket-client') const logger = require('../logger') @@ -14,9 +15,11 @@ const toObj = helper.toObj module.exports = { atomic } function atomic (tx, pi, fromClient) { + const TransactionMode = pgp.txMode.TransactionMode + const isolationLevel = pgp.txMode.isolationLevel + const mode = new TransactionMode({ tiLevel: isolationLevel.serializable }) function transaction (t) { const sql = 'select * from cash_out_txs where id=$1' - return t.oneOrNone(sql, [tx.id]) .then(toObj) .then(oldTx => { @@ -27,8 +30,7 @@ function atomic (tx, pi, fromClient) { .then(preProcessedTx => cashOutLow.upsert(t, oldTx, preProcessedTx)) }) } - - return transaction + return db.tx({ mode }, transaction) } function preProcess (t, oldTx, newTx, pi) { diff --git a/lib/cash-out/cash-out-tx.js b/lib/cash-out/cash-out-tx.js index 17901e40..ed86428d 100644 --- a/lib/cash-out/cash-out-tx.js +++ b/lib/cash-out/cash-out-tx.js @@ -37,11 +37,7 @@ function selfPost (tx, pi) { } function post (tx, pi, fromClient = true) { - const TransactionMode = pgp.txMode.TransactionMode - const isolationLevel = pgp.txMode.isolationLevel - const mode = new TransactionMode({ tiLevel: isolationLevel.serializable }) - - return db.tx({ mode }, cashOutAtomic.atomic(tx, pi, fromClient)) + return cashOutAtomic.atomic(tx, pi, fromClient) .then(txVector => { const [, newTx, justAuthorized] = txVector return postProcess(txVector, justAuthorized, pi)