const db = require('./db')

function createCashboxBatch (rec) {
  const sql = 'INSERT INTO cashbox_batches (device_id, created) VALUES ($1, now()) RETURNING *'
  const sql2 = ` 
    UPDATE bills SET cashbox_batch_id=$1
    FROM cash_in_txs
    WHERE bills.cash_in_txs_id = cash_in_txs.id AND
          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])
    })
}

module.exports = { createCashboxBatch }

@@ -376,32 +376,77 @@
   %60%0A  return db.
+tx(async t =%3E %7B%0A    const newBatch = await t.
 oneOrNone(sql, %5B
@@ -464,29 +464,8 @@
 d%5D)%0A
-    .then(res =%3E %7B%0A  
     
@@ -471,18 +471,17 @@
  return 
-db
+t
 .oneOrNo
@@ -494,37 +494,96 @@
 2, %5B
-res.id, res.device_id%5D)%0A    %7D
+newBatch.id, newBatch.device_id%5D)%0A  %7D)%0A    .then(data =%3E data)%0A    .catch(error =%3E error
 )%0A%7D%0A

use db tx to chain queries