fix: FK blocking unpair and bills tally on admin
This commit is contained in:
parent
0b9a39114b
commit
33200da994
2 changed files with 39 additions and 6 deletions
|
|
@ -18,6 +18,21 @@ const fullyFunctionalStatus = { label: 'Fully functional', type: 'success' }
|
|||
const unresponsiveStatus = { label: 'Unresponsive', type: 'error' }
|
||||
const stuckStatus = { label: 'Stuck', type: 'error' }
|
||||
|
||||
const MACHINE_WITH_CALCULATED_FIELD_SQL = `
|
||||
select d.*, emptybills + regularbills as cashbox from devices d
|
||||
left join (
|
||||
select count(*) as emptyBills, eub.device_id
|
||||
from empty_unit_bills eub
|
||||
where eub.cashbox_batch_id is null
|
||||
group by eub.device_id
|
||||
) as nebills on nebills.device_id = d.device_id
|
||||
left join (
|
||||
select count(*) as regularBills, cit.device_id from bills b
|
||||
left join cash_in_txs cit on b.cash_in_txs_id = cit.id
|
||||
where b.cashbox_batch_id is null and b.destination_unit = 'cashbox'
|
||||
group by cit.device_id
|
||||
) as nbills on nbills.device_id = d.device_id`
|
||||
|
||||
function toMachineObject (r) {
|
||||
return {
|
||||
deviceId: r.device_id,
|
||||
|
|
@ -48,7 +63,8 @@ function toMachineObject (r) {
|
|||
}
|
||||
|
||||
function getMachines () {
|
||||
return db.any('SELECT * FROM devices WHERE display=TRUE ORDER BY created')
|
||||
const sql = `${MACHINE_WITH_CALCULATED_FIELD_SQL} where display=TRUE ORDER BY created`
|
||||
return db.any(sql)
|
||||
.then(rr => rr.map(toMachineObject))
|
||||
}
|
||||
|
||||
|
|
@ -123,7 +139,8 @@ function getMachineName (machineId) {
|
|||
}
|
||||
|
||||
function getMachine (machineId, config) {
|
||||
const sql = 'SELECT * FROM devices WHERE device_id=$1'
|
||||
const sql = `${MACHINE_WITH_CALCULATED_FIELD_SQL} WHERE device_id = $1`
|
||||
|
||||
const queryMachine = db.oneOrNone(sql, [machineId]).then(r => {
|
||||
if (r === null) throw new ApolloError('Resource doesn\'t exist', 'NOT_FOUND')
|
||||
else return toMachineObject(r)
|
||||
|
|
@ -223,8 +240,7 @@ function emptyMachineUnits ({ deviceId, newUnits, fiatCode }) {
|
|||
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),
|
||||
const q3 = t.none(`UPDATE devices SET cassette1=$1, cassette2=$2, cassette3=$3, cassette4=$4, stacker1f=$5, stacker1r=$6, stacker2f=$7, stacker2r=$8, stacker3f=$9, stacker3r=$10 WHERE device_id=$11`, [
|
||||
_.defaultTo(machine.cashUnits.cassette1, newUnits.cassette1),
|
||||
_.defaultTo(machine.cashUnits.cassette2, newUnits.cassette2),
|
||||
_.defaultTo(machine.cashUnits.cassette3, newUnits.cassette3),
|
||||
|
|
@ -273,8 +289,7 @@ function refillMachineUnits ({ deviceId, newUnits }) {
|
|||
return db.tx(t => {
|
||||
const q1Cols = ['id', 'device_id', 'operation_type']
|
||||
const q1= t.none(pgp.helpers.insert(operationsToCreate, q1Cols, 'cash_unit_operation'))
|
||||
const q2 = 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),
|
||||
const q2 = t.none(`UPDATE devices SET cassette1=$1, cassette2=$2, cassette3=$3, cassette4=$4, stacker1f=$5, stacker1r=$6, stacker2f=$7, stacker2r=$8, stacker3f=$9, stacker3r=$10 WHERE device_id=$11`, [
|
||||
_.defaultTo(machine.cashUnits.cassette1, newUnits.cassette1),
|
||||
_.defaultTo(machine.cashUnits.cassette2, newUnits.cassette2),
|
||||
_.defaultTo(machine.cashUnits.cassette3, newUnits.cassette3),
|
||||
|
|
|
|||
18
migrations/1691523881128-bills-using-text.js
Normal file
18
migrations/1691523881128-bills-using-text.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
const db = require('./db')
|
||||
|
||||
exports.up = function (next) {
|
||||
var sql = [
|
||||
'ALTER TABLE empty_unit_bills ALTER COLUMN device_id TYPE TEXT, ALTER COLUMN device_id SET NOT NULL',
|
||||
'ALTER TABLE empty_unit_bills DROP CONSTRAINT empty_unit_bills_device_id_fkey',
|
||||
'ALTER TABLE devices DROP COLUMN cashbox',
|
||||
'CREATE INDEX ON empty_unit_bills (cashbox_batch_id)',
|
||||
'CREATE INDEX ON bills (cashbox_batch_id)',
|
||||
'CREATE INDEX ON bills (destination_unit)',
|
||||
]
|
||||
|
||||
db.multi(sql, next)
|
||||
}
|
||||
|
||||
exports.down = function (next) {
|
||||
next()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue