Skip to content

Commit

Permalink
fix: Uncategorize bank transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
abouolia committed Sep 7, 2024
1 parent 9f21f64 commit 5962b99
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/server/src/interfaces/CashflowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export interface ICashflowTransactionUncategorizedPayload {
tenantId: number;
uncategorizedTransactionId: number;
uncategorizedTransactions: Array<IUncategorizedCashflowTransaction>;
oldMainUncategorizedTransaction: IUncategorizedCashflowTransaction;
oldUncategorizedTransactions: Array<IUncategorizedCashflowTransaction>;
trx: Knex.Transaction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,25 @@ export class UncategorizeCashflowTransaction {
): Promise<Array<number>> {
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(
Expand Down Expand Up @@ -85,6 +88,7 @@ export class UncategorizeCashflowTransaction {
{
tenantId,
uncategorizedTransactionId,
oldMainUncategorizedTransaction,
uncategorizedTransactions,
oldUncategorizedTransactions,
trx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit 5962b99

Please sign in to comment.