From f6350d3d61342ed9beb0a77556ae6d7d56047806 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Mon, 5 Aug 2024 21:11:15 +0200 Subject: [PATCH] fix: Should not show the excluded transactions in recognized transactions --- .../BankAccounts/GetBankAccountSummary.ts | 42 +++++++++++-------- .../Cashflow/GetRecongizedTransactions.ts | 1 + 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/server/src/services/Banking/BankAccounts/GetBankAccountSummary.ts b/packages/server/src/services/Banking/BankAccounts/GetBankAccountSummary.ts index cfa3c8d4c..b8c173dab 100644 --- a/packages/server/src/services/Banking/BankAccounts/GetBankAccountSummary.ts +++ b/packages/server/src/services/Banking/BankAccounts/GetBankAccountSummary.ts @@ -31,17 +31,21 @@ export class GetBankAccountSummary { .findById(bankAccountId) .throwIfNotFound(); + const commonQuery = (q) => { + // Include just the given account. + q.where('accountId', bankAccountId); + + // Only the not excluded. + q.modify('notExcluded'); + + // Only the not categorized. + q.modify('notCategorized'); + }; + // Retrieves the uncategorized transactions count of the given bank account. const uncategorizedTranasctionsCount = await UncategorizedCashflowTransaction.query().onBuild((q) => { - // Include just the given account. - q.where('accountId', bankAccountId); - - // Only the not excluded. - q.modify('notExcluded'); - - // Only the not categorized. - q.modify('notCategorized'); + commonQuery(q); // Only the not matched bank transactions. q.withGraphJoined('matchedBankTransactions'); @@ -52,16 +56,18 @@ export class GetBankAccountSummary { q.first(); }); - // Retrieves the recognized transactions count of the given bank account. - const recognizedTransactionsCount = await RecognizedBankTransaction.query() - .whereExists( - UncategorizedCashflowTransaction.query().where( - 'accountId', - bankAccountId - ) - ) - .count('id as total') - .first(); + // Retrives the recognized transactions count. + const recognizedTransactionsCount = + await UncategorizedCashflowTransaction.query().onBuild((q) => { + commonQuery(q); + + q.withGraphJoined('recognizedTransaction'); + q.whereNotNull('recognizedTransaction.id'); + + // Count the results. + q.count('uncategorized_cashflow_transactions.id as total'); + q.first(); + }); const totalUncategorizedTransactions = uncategorizedTranasctionsCount?.total || 0; diff --git a/packages/server/src/services/Cashflow/GetRecongizedTransactions.ts b/packages/server/src/services/Cashflow/GetRecongizedTransactions.ts index 3bc6ac4fc..9ba2bd102 100644 --- a/packages/server/src/services/Cashflow/GetRecongizedTransactions.ts +++ b/packages/server/src/services/Cashflow/GetRecongizedTransactions.ts @@ -34,6 +34,7 @@ export class GetRecognizedTransactionsService { q.withGraphFetched('recognizedTransaction.assignAccount'); q.withGraphFetched('recognizedTransaction.bankRule'); q.whereNotNull('recognizedTransactionId'); + q.modify('notExcluded'); if (_filter.accountId) { q.where('accountId', _filter.accountId);