feat: delete uncategorized transactions before deleting bank account

This commit is contained in:
Ahmed Bouhuolia
2024-08-18 19:30:09 +02:00
parent fb8118bea8
commit 2f21107a43
9 changed files with 104 additions and 72 deletions

View File

@@ -23,46 +23,56 @@ export class DeleteUncategorizedTransactionsOnAccountDeleting {
public attach(bus) {
bus.subscribe(
events.accounts.onDelete,
this.handleDeleteBankRulesOnAccountDeleting.bind(this),
)
bus.subscribe(
events.accounts.onDelete,
this.handleDeleteUncategorizedTransactions.bind(this)
this.handleDeleteBankRulesOnAccountDeleting.bind(this)
);
}
/**
* Handles delete the uncategorized transactions.
* @param {IAccountEventDeletePayload} payload -
*/
private async handleDeleteUncategorizedTransactions({ tenantId, oldAccount, trx }: IAccountEventDeletePayload) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
await UncategorizedCashflowTransaction.query(trx)
.where('accountId', oldAccount.id)
.delete();
}
/**
* Handles revert the recognized transactions and delete all the bank rules
* associated to the deleted bank account.
* @param {IAccountEventDeletePayload}
*/
private async handleDeleteBankRulesOnAccountDeleting({ tenantId, oldAccount, trx }: IAccountEventDeletePayload) {
private async handleDeleteBankRulesOnAccountDeleting({
tenantId,
oldAccount,
trx,
}: IAccountEventDeletePayload) {
const knex = this.tenancy.knex(tenantId);
const { BankRule, UncategorizedCashflowTransaction, MatchedBankTransaction, RecognizedBankTransaction } = this.tenancy.models(tenantId);
const {
BankRule,
UncategorizedCashflowTransaction,
MatchedBankTransaction,
RecognizedBankTransaction,
} = this.tenancy.models(tenantId);
const foundAssociatedRules = await BankRule.query(trx).where('applyIfAccountId', oldAccount.id);
const foundAssociatedRulesIds = foundAssociatedRules.map(rule => rule.id);
const foundAssociatedRules = await BankRule.query(trx).where(
'applyIfAccountId',
oldAccount.id
);
const foundAssociatedRulesIds = foundAssociatedRules.map((rule) => rule.id);
await initialize(knex, [
UncategorizedCashflowTransaction,
RecognizedBankTransaction,
MatchedBankTransaction,
]);
// Revert the recognized transactions of the given bank rules.
await this.revertRecognizedTransactins.revertRecognizedTransactions(
tenantId,
foundAssociatedRulesIds,
null,
trx
);
// Delete the associated uncategorized transactions.
await UncategorizedCashflowTransaction.query(trx)
.where('accountId', oldAccount.id)
.delete();
await this.revertRecognizedTransactins.revertRecognizedTransactions(tenantId, foundAssociatedRulesIds, null, trx)
await this.deleteBankRules.deleteBankRules(tenantId, foundAssociatedRulesIds);
// Delete the given bank rules.
await this.deleteBankRules.deleteBankRules(
tenantId,
foundAssociatedRulesIds,
trx
);
}
}
}

View File

@@ -2,8 +2,8 @@ import { Inject, Service } from 'typedi';
import { IAccountEventDeletedPayload } from '@/interfaces';
import { PlaidClientWrapper } from '@/lib/Plaid';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import events from '@/subscribers/events';
import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
import events from '@/subscribers/events';
@Service()
export class DisconnectPlaidItemOnAccountDeleted {