mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
fix: Delete bank rule if it has no associations
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { Knex } from 'knex';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
|
||||
@Service()
|
||||
export class UnlinkBankRuleRecognizedTransactions {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
/**
|
||||
* Unlinks the given bank rule out of recognized transactions.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} bankRuleId - Bank rule id.
|
||||
* @param {Knex.Transaction} trx - Knex transaction.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async unlinkBankRuleOutRecognizedTransactions(
|
||||
tenantId: number,
|
||||
bankRuleId: number,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<void> {
|
||||
const { UncategorizedCashflowTransaction, RecognizedBankTransaction } =
|
||||
this.tenancy.models(tenantId);
|
||||
|
||||
return this.uow.withTransaction(
|
||||
tenantId,
|
||||
async (trx: Knex.Transaction) => {
|
||||
// Retrieves all the recognized transactions of the banbk rule.
|
||||
const recognizedTransactions = await RecognizedBankTransaction.query(
|
||||
trx
|
||||
).where('bankRuleId', bankRuleId);
|
||||
|
||||
const uncategorizedTransactionIds = recognizedTransactions.map(
|
||||
(r) => r.uncategorizedTransactionId
|
||||
);
|
||||
// Unlink the recongized transactions out of uncategorized transactions.
|
||||
await UncategorizedCashflowTransaction.query(trx)
|
||||
.whereIn('id', uncategorizedTransactionIds)
|
||||
.patch({
|
||||
recognizedTransactionId: null,
|
||||
});
|
||||
// Delete the recognized bank transactions that assocaited to bank rule.
|
||||
await RecognizedBankTransaction.query(trx)
|
||||
.where({ bankRuleId })
|
||||
.delete();
|
||||
},
|
||||
trx
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import events from '@/subscribers/events';
|
||||
import { UnlinkBankRuleRecognizedTransactions } from '../UnlinkBankRuleRecognizedTransactions';
|
||||
import { IBankRuleEventDeletingPayload } from '../types';
|
||||
|
||||
@Service()
|
||||
export class UnlinkBankRuleOnDeleteBankRule {
|
||||
@Inject()
|
||||
private unlinkBankRule: UnlinkBankRuleRecognizedTransactions;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
public attach(bus) {
|
||||
bus.subscribe(
|
||||
events.bankRules.onDeleting,
|
||||
this.unlinkBankRuleOutRecognizedTransactionsOnRuleDeleting.bind(this)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlinks the bank rule out of recognized transactions.
|
||||
* @param {IBankRuleEventDeletingPayload} payload -
|
||||
*/
|
||||
private async unlinkBankRuleOutRecognizedTransactionsOnRuleDeleting({
|
||||
tenantId,
|
||||
ruleId,
|
||||
}: IBankRuleEventDeletingPayload) {
|
||||
await this.unlinkBankRule.unlinkBankRuleOutRecognizedTransactions(
|
||||
tenantId,
|
||||
ruleId
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user