feat: create a new batching function which pairs with machine value updates

refactor: abstract amount of cassettes from the cassette wizard

fix: dashboard cassettes
This commit is contained in:
Sérgio Salgado 2021-11-29 23:24:04 +00:00
parent f14674c4f3
commit ec90776d2a
9 changed files with 180 additions and 105 deletions

View file

@ -1,3 +1,4 @@
const constants = require('./constants')
const db = require('./db')
const _ = require('lodash/fp')
const uuid = require('uuid')
@ -18,6 +19,34 @@ function createCashboxBatch (deviceId, cashboxCount) {
})
}
function updateMachineWithBatch (machineContext, oldCashboxCount) {
const isValidContext = _.has(['deviceId', 'cashbox', 'cassettes'], machineContext)
const isCassetteAmountWithinRange = _.inRange(constants.CASH_OUT_MINIMUM_AMOUNT_OF_CASSETTES, constants.CASH_OUT_MAXIMUM_AMOUNT_OF_CASSETTES + 1, _.size(machineContext.cassettes))
if (!isValidContext && !isCassetteAmountWithinRange)
throw new Error('Insufficient info to create a new cashbox batch')
if (_.isEqual(0, oldCashboxCount)) throw new Error('Cashbox is empty. Cashbox batch could not be created.')
return db.tx(t => {
const deviceId = machineContext.deviceId
const batchId = uuid.v4()
const q1 = t.none(`INSERT INTO cashbox_batches (id, device_id, created, operation_type) VALUES ($1, $2, now(), 'cash-in-empty')`, [batchId, deviceId])
const q2 = t.none(`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`, [batchId, deviceId])
const q3 = t.none(`UPDATE devices SET cashbox=$1, cassette1=$2, cassette2=$3, cassette3=$4, cassette4=$5 WHERE device_id=$6`, [
machineContext.cashbox,
machineContext.cassettes[0],
machineContext.cassettes[1],
machineContext.cassettes[2],
machineContext.cassettes[3],
machineContext.deviceId
])
return t.batch([q1, q2, q3])
})
}
function getBatches () {
const sql = `
SELECT cb.id, cb.device_id, cb.created, cb.operation_type, cb.bill_count_override, cb.performed_by, json_agg(b.*) AS bills
@ -39,4 +68,4 @@ function getBillsByBatchId (id) {
return db.any(sql, [id])
}
module.exports = { createCashboxBatch, getBatches, getBillsByBatchId, editBatchById }
module.exports = { createCashboxBatch, updateMachineWithBatch, getBatches, getBillsByBatchId, editBatchById }