From 0bdbd4cc762cfa926c267f67b2c324ac8d7a4361 Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Tue, 13 Aug 2024 09:40:42 +0100 Subject: [PATCH] fix: cashbox history optimize and fix query --- lib/cashbox-batches.js | 28 ++++++++----------- lib/new-admin/graphql/types/cashbox.type.js | 3 +- .../src/pages/Maintenance/CashboxHistory.js | 10 +++---- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/lib/cashbox-batches.js b/lib/cashbox-batches.js index e0f89ae2..52f41dc6 100644 --- a/lib/cashbox-batches.js +++ b/lib/cashbox-batches.js @@ -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 diff --git a/lib/new-admin/graphql/types/cashbox.type.js b/lib/new-admin/graphql/types/cashbox.type.js index 17f5131a..1c9cf71c 100644 --- a/lib/new-admin/graphql/types/cashbox.type.js +++ b/lib/new-admin/graphql/types/cashbox.type.js @@ -8,7 +8,8 @@ const typeDef = gql` operationType: String customBillCount: Int performedBy: String - bills: [Bill] + billCount: Int + fiatTotal: Int } type Query { diff --git a/new-lamassu-admin/src/pages/Maintenance/CashboxHistory.js b/new-lamassu-admin/src/pages/Maintenance/CashboxHistory.js index 5a12b0d7..139899d1 100644 --- a/new-lamassu-admin/src/pages/Maintenance/CashboxHistory.js +++ b/new-lamassu-admin/src/pages/Maintenance/CashboxHistory.js @@ -24,10 +24,8 @@ const GET_BATCHES = gql` operationType customBillCount performedBy - bills { - fiat - fiatCode - } + billCount + fiatTotal } } ` @@ -168,7 +166,7 @@ const CashboxHistory = ({ machines, currency, timezone }) => { decimalPlaces: 0 }, view: it => - R.isNil(it.customBillCount) ? it.bills.length : it.customBillCount + R.isNil(it.customBillCount) ? it.billCount : it.customBillCount }, { name: 'total', @@ -177,7 +175,7 @@ const CashboxHistory = ({ machines, currency, timezone }) => { textAlign: 'right', view: it => ( - {R.sum(R.map(b => R.prop('fiat', b), it.bills))} {currency} + {it.fiatTotal} {currency} ) },