From 5962b990c49234cfbac51a67cfc71ea604b44c15 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Sat, 7 Sep 2024 13:26:02 +0200 Subject: [PATCH] fix: Uncategorize bank transactions --- .../server/src/interfaces/CashflowService.ts | 1 + .../UncategorizeCashflowTransaction.ts | 16 +++++---- ...DeleteCashflowTransactionOnUncategorize.ts | 33 ++++++++----------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/packages/server/src/interfaces/CashflowService.ts b/packages/server/src/interfaces/CashflowService.ts index 415682ae5..9c2a68909 100644 --- a/packages/server/src/interfaces/CashflowService.ts +++ b/packages/server/src/interfaces/CashflowService.ts @@ -146,6 +146,7 @@ export interface ICashflowTransactionUncategorizedPayload { tenantId: number; uncategorizedTransactionId: number; uncategorizedTransactions: Array; + oldMainUncategorizedTransaction: IUncategorizedCashflowTransaction; oldUncategorizedTransactions: Array; trx: Knex.Transaction; } diff --git a/packages/server/src/services/Cashflow/UncategorizeCashflowTransaction.ts b/packages/server/src/services/Cashflow/UncategorizeCashflowTransaction.ts index 963bc80a2..285398b6d 100644 --- a/packages/server/src/services/Cashflow/UncategorizeCashflowTransaction.ts +++ b/packages/server/src/services/Cashflow/UncategorizeCashflowTransaction.ts @@ -33,22 +33,25 @@ export class UncategorizeCashflowTransaction { ): Promise> { const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId); - const oldUncategorizedTransaction = + const oldMainUncategorizedTransaction = await UncategorizedCashflowTransaction.query() .findById(uncategorizedTransactionId) .throwIfNotFound(); - validateTransactionShouldBeCategorized(oldUncategorizedTransaction); + validateTransactionShouldBeCategorized(oldMainUncategorizedTransaction); const associatedUncategorizedTransactions = await UncategorizedCashflowTransaction.query() - .where('categorizeRefId', oldUncategorizedTransaction.categorizeRefId) + .where('categorizeRefId', oldMainUncategorizedTransaction.categorizeRefId) .where( 'categorizeRefType', - oldUncategorizedTransaction.categorizeRefType - ); + oldMainUncategorizedTransaction.categorizeRefType + ) + // Exclude the main transaction. + .whereNot('id', uncategorizedTransactionId); + const oldUncategorizedTransactions = [ - oldUncategorizedTransaction, + oldMainUncategorizedTransaction, ...associatedUncategorizedTransactions, ]; const oldUncategoirzedTransactionsIds = oldUncategorizedTransactions.map( @@ -85,6 +88,7 @@ export class UncategorizeCashflowTransaction { { tenantId, uncategorizedTransactionId, + oldMainUncategorizedTransaction, uncategorizedTransactions, oldUncategorizedTransactions, trx, diff --git a/packages/server/src/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize.ts b/packages/server/src/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize.ts index 4bbdc3fac..42f69050b 100644 --- a/packages/server/src/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize.ts +++ b/packages/server/src/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize.ts @@ -22,32 +22,25 @@ export class DeleteCashflowTransactionOnUncategorize { }; /** - * Deletes the cashflow transaction on uncategorize transaction. + * Deletes the cashflow transaction once uncategorize the bank transaction. * @param {ICashflowTransactionUncategorizedPayload} payload */ public async deleteCashflowTransactionOnUncategorize({ tenantId, - oldUncategorizedTransactions, + oldMainUncategorizedTransaction, trx, }: ICashflowTransactionUncategorizedPayload) { - const _oldUncategorizedTransactions = oldUncategorizedTransactions.filter( - (transaction) => transaction.categorizeRefType === 'CashflowTransaction' - ); - - // Deletes the cashflow transaction. - if (_oldUncategorizedTransactions.length > 0) { - const result = await PromisePool.withConcurrency(1) - .for(_oldUncategorizedTransactions) - .process(async (oldUncategorizedTransaction) => { - await this.deleteCashflowTransactionService.deleteCashflowTransaction( - tenantId, - oldUncategorizedTransaction.categorizeRefId, - trx - ); - }); - if (result.errors.length > 0) { - throw new ServiceError('SOMETHING_WRONG'); - } + // Cannot continue if the main transaction does not reference to cashflow type. + if ( + oldMainUncategorizedTransaction.categorizeRefType !== + 'CashflowTransaction' + ) { + return; } + await this.deleteCashflowTransactionService.deleteCashflowTransaction( + tenantId, + oldMainUncategorizedTransaction.categorizeRefId, + trx + ); } }