feat: add emptyUnit bills table
feat: rework cash unit operations fix: multiple fixes for the empty unit workflow
This commit is contained in:
parent
797f074898
commit
45f722e724
8 changed files with 171 additions and 59 deletions
|
|
@ -1,6 +1,5 @@
|
|||
const _ = require('lodash/fp')
|
||||
const pgp = require('pg-promise')()
|
||||
const axios = require('axios')
|
||||
const uuid = require('uuid')
|
||||
|
||||
const batching = require('./cashbox-batches')
|
||||
|
|
@ -13,6 +12,7 @@ const settingsLoader = require('./new-settings-loader')
|
|||
const notifierUtils = require('./notifier/utils')
|
||||
const notifierQueries = require('./notifier/queries')
|
||||
const { ApolloError } = require('apollo-server-errors');
|
||||
const { loadLatestConfig } = require('./new-settings-loader')
|
||||
|
||||
const fullyFunctionalStatus = { label: 'Fully functional', type: 'success' }
|
||||
const unresponsiveStatus = { label: 'Unresponsive', type: 'error' }
|
||||
|
|
@ -173,39 +173,72 @@ function setCassetteBills (rec) {
|
|||
})
|
||||
}
|
||||
|
||||
function emptyMachineUnits (deviceId, units) {
|
||||
return Promise.all([getMachine(deviceId), configManager.getCashOut(deviceId, getConfig())])
|
||||
function emptyMachineUnits ({ deviceId, newUnits, fiatCode }) {
|
||||
return loadLatestConfig()
|
||||
.then(config => Promise.all([getMachine(deviceId), configManager.getCashOut(deviceId, config)]))
|
||||
.then(([machine, cashoutSettings]) => {
|
||||
const movedBills = _.reduce(
|
||||
(acc, value) => ({
|
||||
...acc,
|
||||
[value]: {
|
||||
delta: machine[value] - units[value],
|
||||
denomination: cashoutSettings[value]
|
||||
operationName: `cash-${_.replace(/(cassette|stacker)/g, '$1-')(value)}-empty`,
|
||||
delta: newUnits[value] - machine.cashUnits[value],
|
||||
denomination: value !== 'cashbox' ? cashoutSettings[value] : null
|
||||
}
|
||||
}),
|
||||
{},
|
||||
_.keys(units)
|
||||
_.keys(newUnits)
|
||||
)
|
||||
|
||||
const sql = `UPDATE devices SET cassette1=$2, cassette2=$3, cassette3=$4, cassette4=$5, stacker1f=$6, stacker1r=$7, stacker2f=$8, stacker2r=$9, stacker3f=$10, stacker3r=$11 WHERE device_id=$1`
|
||||
return db.none(sql, [
|
||||
deviceId,
|
||||
_.defaultTo(0, units.cassette1),
|
||||
_.defaultTo(0, units.cassette2),
|
||||
_.defaultTo(0, units.cassette3),
|
||||
_.defaultTo(0, units.cassette4),
|
||||
_.defaultTo(0, units.stacker1f),
|
||||
_.defaultTo(0, units.stacker1r),
|
||||
_.defaultTo(0, units.stacker2f),
|
||||
_.defaultTo(0, units.stacker2r),
|
||||
_.defaultTo(0, units.stacker3f),
|
||||
_.defaultTo(0, units.stacker3r)
|
||||
])
|
||||
const operationNames = _.mapValues(it => it.operationName)(_.filter(it => Math.abs(it.delta) > 0)(_.omit(['cashbox'], movedBills)))
|
||||
const operationsToCreate = _.map(it => ({
|
||||
id: uuid.v4(),
|
||||
device_id: deviceId,
|
||||
operation_type: it
|
||||
}))(operationNames)
|
||||
|
||||
const billArr = _.reduce(
|
||||
(acc, value) => {
|
||||
const unit = movedBills[value]
|
||||
return _.concat(acc, _.times(() => ({
|
||||
id: uuid.v4(),
|
||||
fiat: unit.denomination,
|
||||
fiat_code: fiatCode,
|
||||
device_id: deviceId
|
||||
// TODO: Uncomment this if we decide to keep track of bills across multiple operations. For now, we'll just create the emptying operations for each unit affected, but not relate these events with individual bills and just use the field for the cashbox batch event
|
||||
// cash_unit_operation_id: _.find(it => it.operation_type === `cash-${_.replace(/(cassette|stacker)/g, '$1-')(value)}-empty`, operationsToCreate).id
|
||||
}), Math.abs(unit.delta)))
|
||||
},
|
||||
[],
|
||||
_.keys(_.omit(['cashbox'], movedBills))
|
||||
)
|
||||
|
||||
return db.tx(t => {
|
||||
const q1Cols = ['id', 'device_id', 'operation_type']
|
||||
const q1= t.none(pgp.helpers.insert(operationsToCreate, q1Cols, 'cash_unit_operation'))
|
||||
const q2Cols = ['id', 'fiat', 'fiat_code', 'device_id']
|
||||
const q2 = t.none(pgp.helpers.insert(billArr, q2Cols, 'empty_unit_bills'))
|
||||
const q3 = t.none(`UPDATE devices SET cashbox=$1, cassette1=$2, cassette2=$3, cassette3=$4, cassette4=$5, stacker1f=$6, stacker1r=$7, stacker2f=$8, stacker2r=$9, stacker3f=$10, stacker3r=$11 WHERE device_id=$12`, [
|
||||
_.defaultTo(machine.cashUnits.cashbox, newUnits.cashbox),
|
||||
_.defaultTo(machine.cashUnits.cassette1, newUnits.cassette1),
|
||||
_.defaultTo(machine.cashUnits.cassette2, newUnits.cassette2),
|
||||
_.defaultTo(machine.cashUnits.cassette3, newUnits.cassette3),
|
||||
_.defaultTo(machine.cashUnits.cassette4, newUnits.cassette4),
|
||||
_.defaultTo(machine.cashUnits.stacker1f, newUnits.stacker1f),
|
||||
_.defaultTo(machine.cashUnits.stacker1r, newUnits.stacker1r),
|
||||
_.defaultTo(machine.cashUnits.stacker2f, newUnits.stacker2f),
|
||||
_.defaultTo(machine.cashUnits.stacker2r, newUnits.stacker2r),
|
||||
_.defaultTo(machine.cashUnits.stacker3f, newUnits.stacker3f),
|
||||
_.defaultTo(machine.cashUnits.stacker3r, newUnits.stacker3r),
|
||||
deviceId
|
||||
])
|
||||
|
||||
return t.batch([q1, q2, q3])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function refillMachineUnits (deviceId, units) {
|
||||
function refillMachineUnits ({deviceId, newUnits}) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue