Compare commits

...

2 Commits

Author SHA1 Message Date
Ahmed Bouhuolia
6d24474162 Merge pull request #663 from bigcapitalhq/fix-uncategorize-bank-transaction
fix: Un-categorize bank transactions
2024-09-07 13:27:11 +02:00
Ahmed Bouhuolia
5962b990c4 fix: Uncategorize bank transactions 2024-09-07 13:26:02 +02:00
3 changed files with 24 additions and 26 deletions

View File

@@ -146,6 +146,7 @@ export interface ICashflowTransactionUncategorizedPayload {
tenantId: number; tenantId: number;
uncategorizedTransactionId: number; uncategorizedTransactionId: number;
uncategorizedTransactions: Array<IUncategorizedCashflowTransaction>; uncategorizedTransactions: Array<IUncategorizedCashflowTransaction>;
oldMainUncategorizedTransaction: IUncategorizedCashflowTransaction;
oldUncategorizedTransactions: Array<IUncategorizedCashflowTransaction>; oldUncategorizedTransactions: Array<IUncategorizedCashflowTransaction>;
trx: Knex.Transaction; trx: Knex.Transaction;
} }

View File

@@ -33,22 +33,25 @@ export class UncategorizeCashflowTransaction {
): Promise<Array<number>> { ): Promise<Array<number>> {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId); const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
const oldUncategorizedTransaction = const oldMainUncategorizedTransaction =
await UncategorizedCashflowTransaction.query() await UncategorizedCashflowTransaction.query()
.findById(uncategorizedTransactionId) .findById(uncategorizedTransactionId)
.throwIfNotFound(); .throwIfNotFound();
validateTransactionShouldBeCategorized(oldUncategorizedTransaction); validateTransactionShouldBeCategorized(oldMainUncategorizedTransaction);
const associatedUncategorizedTransactions = const associatedUncategorizedTransactions =
await UncategorizedCashflowTransaction.query() await UncategorizedCashflowTransaction.query()
.where('categorizeRefId', oldUncategorizedTransaction.categorizeRefId) .where('categorizeRefId', oldMainUncategorizedTransaction.categorizeRefId)
.where( .where(
'categorizeRefType', 'categorizeRefType',
oldUncategorizedTransaction.categorizeRefType oldMainUncategorizedTransaction.categorizeRefType
); )
// Exclude the main transaction.
.whereNot('id', uncategorizedTransactionId);
const oldUncategorizedTransactions = [ const oldUncategorizedTransactions = [
oldUncategorizedTransaction, oldMainUncategorizedTransaction,
...associatedUncategorizedTransactions, ...associatedUncategorizedTransactions,
]; ];
const oldUncategoirzedTransactionsIds = oldUncategorizedTransactions.map( const oldUncategoirzedTransactionsIds = oldUncategorizedTransactions.map(
@@ -85,6 +88,7 @@ export class UncategorizeCashflowTransaction {
{ {
tenantId, tenantId,
uncategorizedTransactionId, uncategorizedTransactionId,
oldMainUncategorizedTransaction,
uncategorizedTransactions, uncategorizedTransactions,
oldUncategorizedTransactions, oldUncategorizedTransactions,
trx, trx,

View File

@@ -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 * @param {ICashflowTransactionUncategorizedPayload} payload
*/ */
public async deleteCashflowTransactionOnUncategorize({ public async deleteCashflowTransactionOnUncategorize({
tenantId, tenantId,
oldUncategorizedTransactions, oldMainUncategorizedTransaction,
trx, trx,
}: ICashflowTransactionUncategorizedPayload) { }: ICashflowTransactionUncategorizedPayload) {
const _oldUncategorizedTransactions = oldUncategorizedTransactions.filter( // Cannot continue if the main transaction does not reference to cashflow type.
(transaction) => transaction.categorizeRefType === 'CashflowTransaction' if (
); oldMainUncategorizedTransaction.categorizeRefType !==
'CashflowTransaction'
// Deletes the cashflow transaction. ) {
if (_oldUncategorizedTransactions.length > 0) { return;
const result = await PromisePool.withConcurrency(1) }
.for(_oldUncategorizedTransactions)
.process(async (oldUncategorizedTransaction) => {
await this.deleteCashflowTransactionService.deleteCashflowTransaction( await this.deleteCashflowTransactionService.deleteCashflowTransaction(
tenantId, tenantId,
oldUncategorizedTransaction.categorizeRefId, oldMainUncategorizedTransaction.categorizeRefId,
trx trx
); );
});
if (result.errors.length > 0) {
throw new ServiceError('SOMETHING_WRONG');
}
}
} }
} }