mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { IAccountEventDeletePayload } from '@/interfaces/Account';
|
||||
import { RevertRecognizedTransactionsService } from '@/modules/BankingTranasctionsRegonize/commands/RevertRecognizedTransactions.service';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { DeleteBankRulesService } from '@/modules/BankRules/commands/DeleteBankRules.service';
|
||||
import { BankRule } from '@/modules/BankRules/models/BankRule';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteUncategorizedTransactionsOnAccountDeleting {
|
||||
constructor(
|
||||
private readonly deleteBankRules: DeleteBankRulesService,
|
||||
private readonly revertRecognizedTransactins: RevertRecognizedTransactionsService,
|
||||
|
||||
@Inject(BankRule.name)
|
||||
private bankRuleModel: TenantModelProxy<typeof BankRule>,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private uncategorizedCashflowTransactionModel: TenantModelProxy<
|
||||
typeof UncategorizedBankTransaction
|
||||
>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Handles revert the recognized transactions and delete all the bank rules
|
||||
* associated to the deleted bank account.
|
||||
* @param {IAccountEventDeletePayload}
|
||||
*/
|
||||
@OnEvent(events.accounts.onDelete)
|
||||
public async handleDeleteBankRulesOnAccountDeleting({
|
||||
oldAccount,
|
||||
trx,
|
||||
}: IAccountEventDeletePayload) {
|
||||
const foundAssociatedRules = await this.bankRuleModel()
|
||||
.query(trx)
|
||||
.where('applyIfAccountId', oldAccount.id);
|
||||
const foundAssociatedRulesIds = foundAssociatedRules.map((rule) => rule.id);
|
||||
|
||||
// Revert the recognized transactions of the given bank rules.
|
||||
await this.revertRecognizedTransactins.revertRecognizedTransactions(
|
||||
foundAssociatedRulesIds,
|
||||
null,
|
||||
trx,
|
||||
);
|
||||
// Delete the associated uncategorized transactions.
|
||||
await this.uncategorizedCashflowTransactionModel()
|
||||
.query(trx)
|
||||
.where('accountId', oldAccount.id)
|
||||
.delete();
|
||||
|
||||
// Delete the given bank rules.
|
||||
await this.deleteBankRules.deleteBankRules(foundAssociatedRulesIds, trx);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { IAccountEventDeletedPayload } from '@/interfaces/Account';
|
||||
import { events } from '@/common/events/events';
|
||||
import { PlaidItem } from '@/modules/BankingPlaid/models/PlaidItem';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { PlaidApi } from 'plaid';
|
||||
import { PLAID_CLIENT } from '@/modules/Plaid/Plaid.module';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class DisconnectPlaidItemOnAccountDeleted {
|
||||
constructor(
|
||||
@Inject(PLAID_CLIENT) private plaidClient: PlaidApi,
|
||||
|
||||
@Inject(PlaidItem.name)
|
||||
private plaidItemModel: TenantModelProxy<typeof PlaidItem>,
|
||||
|
||||
@Inject(Account.name)
|
||||
private accountModel: TenantModelProxy<typeof Account>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Deletes Plaid item from the system and Plaid once the account deleted.
|
||||
* @param {IAccountEventDeletedPayload} payload
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
@OnEvent(events.accounts.onDeleted)
|
||||
public async handleDisconnectPlaidItemOnAccountDelete({
|
||||
oldAccount,
|
||||
trx,
|
||||
}: IAccountEventDeletedPayload) {
|
||||
// Can't continue if the deleted account is not linked to Plaid item.
|
||||
if (!oldAccount.plaidItemId) return;
|
||||
|
||||
// Retrieves the Plaid item that associated to the deleted account.
|
||||
const oldPlaidItem = await this.plaidItemModel()
|
||||
.query(trx)
|
||||
.findOne('plaidItemId', oldAccount.plaidItemId);
|
||||
// Unlink the Plaid item from all account before deleting it.
|
||||
await this.accountModel()
|
||||
.query(trx)
|
||||
.where('plaidItemId', oldAccount.plaidItemId)
|
||||
.patch({
|
||||
plaidAccountId: null,
|
||||
plaidItemId: null,
|
||||
});
|
||||
// Remove the Plaid item from the system.
|
||||
await this.plaidItemModel()
|
||||
.query(trx)
|
||||
.findOne('plaidItemId', oldAccount.plaidItemId)
|
||||
.delete();
|
||||
|
||||
// Remove Plaid item once the transaction resolve.
|
||||
if (oldPlaidItem) {
|
||||
// Remove the Plaid item.
|
||||
await this.plaidClient.itemRemove({
|
||||
access_token: oldPlaidItem.plaidAccessToken,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user