mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
feat: delete Plaid item once bank account deleted
This commit is contained in:
@@ -37,6 +37,7 @@ export interface IAccount {
|
|||||||
accountNormal: string;
|
accountNormal: string;
|
||||||
accountParentType: string;
|
accountParentType: string;
|
||||||
bankBalance: string;
|
bankBalance: string;
|
||||||
|
plaidItemId: number | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum AccountNormal {
|
export enum AccountNormal {
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ import { UnlinkBankRuleOnDeleteBankRule } from '@/services/Banking/Rules/events/
|
|||||||
import { DecrementUncategorizedTransactionOnMatching } from '@/services/Banking/Matching/events/DecrementUncategorizedTransactionsOnMatch';
|
import { DecrementUncategorizedTransactionOnMatching } from '@/services/Banking/Matching/events/DecrementUncategorizedTransactionsOnMatch';
|
||||||
import { DecrementUncategorizedTransactionOnExclude } from '@/services/Banking/Exclude/events/DecrementUncategorizedTransactionOnExclude';
|
import { DecrementUncategorizedTransactionOnExclude } from '@/services/Banking/Exclude/events/DecrementUncategorizedTransactionOnExclude';
|
||||||
import { DecrementUncategorizedTransactionOnCategorize } from '@/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize';
|
import { DecrementUncategorizedTransactionOnCategorize } from '@/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize';
|
||||||
|
import { DisconnectPlaidItemOnAccountDeleted } from '@/services/Banking/BankAccounts/events/DisconnectPlaidItemOnAccountDeleted';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
return new EventPublisher();
|
return new EventPublisher();
|
||||||
@@ -274,5 +275,6 @@ export const susbcribers = () => {
|
|||||||
|
|
||||||
// Plaid
|
// Plaid
|
||||||
RecognizeSyncedBankTranasctions,
|
RecognizeSyncedBankTranasctions,
|
||||||
|
DisconnectPlaidItemOnAccountDeleted,
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export class DisconnectBankAccount {
|
|||||||
* @param {number} bankAccountId
|
* @param {number} bankAccountId
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async disconnectBankAccount(tenantId: number, bankAccountId: number) {
|
public async disconnectBankAccount(tenantId: number, bankAccountId: number) {
|
||||||
const { Account, PlaidItem } = this.tenancy.models(tenantId);
|
const { Account, PlaidItem } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
// Retrieve the bank account or throw not found error.
|
// Retrieve the bank account or throw not found error.
|
||||||
@@ -57,7 +57,7 @@ export class DisconnectBankAccount {
|
|||||||
isFeedsActive: false,
|
isFeedsActive: false,
|
||||||
});
|
});
|
||||||
// Remove the Plaid item.
|
// Remove the Plaid item.
|
||||||
const data = await plaidInstance.itemRemove({
|
await plaidInstance.itemRemove({
|
||||||
access_token: oldPlaidItem.plaidAccessToken,
|
access_token: oldPlaidItem.plaidAccessToken,
|
||||||
});
|
});
|
||||||
// Triggers `onBankAccountDisconnected` event.
|
// Triggers `onBankAccountDisconnected` event.
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import { IAccountEventDeletedPayload } from '@/interfaces';
|
||||||
|
import { PlaidClientWrapper } from '@/lib/Plaid';
|
||||||
|
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||||
|
import events from '@/subscribers/events';
|
||||||
|
import { Inject, Service } from 'typedi';
|
||||||
|
|
||||||
|
@Service()
|
||||||
|
export class DisconnectPlaidItemOnAccountDeleted {
|
||||||
|
@Inject()
|
||||||
|
private tenancy: HasTenancyService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor method.
|
||||||
|
*/
|
||||||
|
public attach(bus) {
|
||||||
|
bus.subscribe(
|
||||||
|
events.accounts.onDeleted,
|
||||||
|
this.handleDisconnectPlaidItemOnAccountDelete.bind(this)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes Plaid item from the system and Plaid once the account deleted.
|
||||||
|
* @param {IAccountEventDeletedPayload} payload
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
private async handleDisconnectPlaidItemOnAccountDelete({
|
||||||
|
tenantId,
|
||||||
|
oldAccount,
|
||||||
|
trx,
|
||||||
|
}: IAccountEventDeletedPayload) {
|
||||||
|
const { PlaidItem, Account } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
|
// 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 PlaidItem.query(trx).findById(
|
||||||
|
oldAccount.plaidItemId
|
||||||
|
);
|
||||||
|
// Unlink the Plaid item from all account before deleting it.
|
||||||
|
await Account.query(trx)
|
||||||
|
.where('plaidItemId', oldAccount.plaidItemId)
|
||||||
|
.patch({
|
||||||
|
plaidItemId: null,
|
||||||
|
});
|
||||||
|
// Remove the Plaid item from the system.
|
||||||
|
await PlaidItem.query(trx).findById(oldAccount.plaidItemId).delete();
|
||||||
|
|
||||||
|
if (oldPlaidItem) {
|
||||||
|
const plaidInstance = new PlaidClientWrapper();
|
||||||
|
|
||||||
|
// Remove the Plaid item.
|
||||||
|
await plaidInstance.itemRemove({
|
||||||
|
access_token: oldPlaidItem.plaidAccessToken,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user