mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 22:30:31 +00:00
feat: delete uncategorized transactions before deleting bank account
This commit is contained in:
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user