fix: cashbox history optimize and fix query

This commit is contained in:
Rafael Taranto 2024-08-13 09:40:42 +01:00
parent d8ce011b28
commit 0bdbd4cc76
3 changed files with 18 additions and 23 deletions

View file

@ -73,14 +73,22 @@ function updateMachineWithBatch (machineContext, oldCashboxCount) {
function getBatches (from = new Date(0).toISOString(), until = new Date().toISOString()) {
const sql = `
SELECT cuo.id, cuo.device_id, cuo.created, cuo.operation_type, cuo.bill_count_override, cuo.performed_by, json_agg(bi.*) AS bills
SELECT
cuo.id,
cuo.device_id,
cuo.created,
cuo.operation_type,
cuo.bill_count_override,
cuo.performed_by,
COUNT(bi.id) AS bill_count,
COALESCE(SUM(bi.fiat), 0) AS fiat_total
FROM cash_unit_operation AS cuo
LEFT JOIN (
SELECT b.id, b.fiat, b.fiat_code, b.created, b.cashbox_batch_id, cit.device_id AS device_id FROM bills b LEFT OUTER JOIN (SELECT id, device_id FROM cash_in_txs) AS cit ON cit.id = b.cash_in_txs_id UNION
SELECT id, fiat, fiat_code, created, cashbox_batch_id, device_id FROM empty_unit_bills
) AS bi ON cuo.id = bi.cashbox_batch_id
WHERE cuo.created >= $1 AND cuo.created <= $2 AND cuo.operation_type = 'cash-box-empty'
GROUP BY cuo.id
GROUP BY cuo.id, cuo.device_id, cuo.created, cuo.operation_type, cuo.bill_count_override, cuo.performed_by
ORDER BY cuo.created DESC
`
@ -103,25 +111,13 @@ function getBillsByBatchId (id) {
function logFormatter (data) {
return _.map(
it => {
const bills = _.filter(
ite => !(_.isNil(ite) || _.isNil(ite.fiat_code) || _.isNil(ite.fiat) || _.isNaN(ite.fiat)),
it.bills
)
return {
id: it.id,
deviceId: it.deviceId,
created: it.created,
operationType: it.operationType,
billCount: _.size(bills),
fiatTotals: _.reduce(
(acc, value) => {
acc[value.fiat_code] = (acc[value.fiat_code] || 0) + value.fiat
return acc
},
{},
bills
),
billsByDenomination: _.countBy(ite => `${ite.fiat} ${ite.fiat_code}`, bills)
billCount: it.billCount,
fiatTotal: it.fiatTotal
}
},
data

View file

@ -8,7 +8,8 @@ const typeDef = gql`
operationType: String
customBillCount: Int
performedBy: String
bills: [Bill]
billCount: Int
fiatTotal: Int
}
type Query {