mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
refactor(nestjs): plaid banking syncing
This commit is contained in:
@@ -17,9 +17,7 @@ export class PlaidItemService {
|
||||
private readonly tenancyContext: TenancyContext,
|
||||
|
||||
@Inject(SystemPlaidItem.name)
|
||||
private readonly systemPlaidItemModel: TenantModelProxy<
|
||||
typeof SystemPlaidItem
|
||||
>,
|
||||
private readonly systemPlaidItemModel: typeof SystemPlaidItem,
|
||||
|
||||
@Inject(PlaidItem.name)
|
||||
private readonly plaidItemModel: TenantModelProxy<typeof PlaidItem>,
|
||||
@@ -55,7 +53,7 @@ export class PlaidItemService {
|
||||
plaidInstitutionId: institutionId,
|
||||
});
|
||||
// Stores the Plaid item id on system scope.
|
||||
await this.systemPlaidItemModel().query().insert({ tenantId, plaidItemId });
|
||||
await this.systemPlaidItemModel.query().insert({ tenantId, plaidItemId });
|
||||
|
||||
// Triggers `onPlaidItemCreated` event.
|
||||
await this.eventEmitter.emitAsync(events.plaid.onItemCreated, {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as R from 'ramda';
|
||||
import bluebird from 'bluebird';
|
||||
import * as bluebird from 'bluebird';
|
||||
import * as uniqid from 'uniqid';
|
||||
import { entries, groupBy } from 'lodash';
|
||||
import {
|
||||
AccountBase as PlaidAccountBase,
|
||||
@@ -12,7 +13,6 @@ import {
|
||||
transformPlaidTrxsToCashflowCreate,
|
||||
} from '../utils';
|
||||
import { Knex } from 'knex';
|
||||
import uniqid from 'uniqid';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { RemovePendingUncategorizedTransaction } from '../../BankingTransactions/commands/RemovePendingUncategorizedTransaction.service';
|
||||
import { CreateAccountService } from '../../Accounts/CreateAccount.service';
|
||||
|
||||
@@ -103,7 +103,6 @@ export class PlaidUpdateTransactions {
|
||||
|
||||
/**
|
||||
* Fetches transactions from the `Plaid API` for a given item.
|
||||
* @param {number} tenantId - Tenant ID.
|
||||
* @param {string} plaidItemId - The Plaid ID for the item.
|
||||
* @returns {Promise<PlaidFetchedTransactionsUpdates>}
|
||||
*/
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
import { PlaidItem } from '../models/PlaidItem';
|
||||
import { PlaidUpdateTransactions } from './PlaidUpdateTransactions';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
@@ -8,7 +9,7 @@ export class PlaidWebooks {
|
||||
private readonly updateTransactionsService: PlaidUpdateTransactions,
|
||||
|
||||
@Inject(PlaidItem.name)
|
||||
private readonly plaidItemModel: typeof PlaidItem,
|
||||
private readonly plaidItemModel: TenantModelProxy<typeof PlaidItem>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -76,11 +77,10 @@ export class PlaidWebooks {
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async handleTransactionsWebooks(
|
||||
tenantId: number,
|
||||
plaidItemId: string,
|
||||
webhookCode: string,
|
||||
): Promise<void> {
|
||||
const plaidItem = await this.plaidItemModel
|
||||
const plaidItem = await this.plaidItemModel()
|
||||
.query()
|
||||
.findOne({ plaidItemId })
|
||||
.throwIfNotFound();
|
||||
@@ -122,9 +122,8 @@ export class PlaidWebooks {
|
||||
|
||||
/**
|
||||
* Handles all Item webhook events.
|
||||
* @param {number} tenantId - Tenant ID
|
||||
* @param {string} webhookCode - The webhook code
|
||||
* @param {string} plaidItemId - The Plaid ID for the item
|
||||
* @param {string} webhookCode - The webhook code
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async itemsHandler(
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { ClsService } from 'nestjs-cls';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { SystemPlaidItem } from '../models/SystemPlaidItem';
|
||||
import { TenantModel } from '@/modules/System/models/TenantModel';
|
||||
import { SystemUser } from '@/modules/System/models/SystemUser';
|
||||
|
||||
@Injectable()
|
||||
export class SetupPlaidItemTenantService {
|
||||
constructor(
|
||||
private readonly clsService: ClsService,
|
||||
|
||||
@Inject(SystemPlaidItem.name)
|
||||
private readonly systemPlaidItemModel: typeof SystemPlaidItem,
|
||||
|
||||
@Inject(TenantModel.name)
|
||||
private readonly tenantModel: typeof TenantModel,
|
||||
|
||||
@Inject(SystemUser.name)
|
||||
private readonly systemUserModel: typeof SystemUser,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Sets up the Plaid tenant.
|
||||
* @param {string} plaidItemId - The Plaid item id.
|
||||
* @param {() => void} callback - The callback function to execute after setting up the Plaid tenant.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async setupPlaidTenant(plaidItemId: string, callback: () => void) {
|
||||
const plaidItem = await this.systemPlaidItemModel
|
||||
.query()
|
||||
.findOne({ plaidItemId });
|
||||
|
||||
if (!plaidItem) {
|
||||
throw new Error('Plaid item not found');
|
||||
}
|
||||
const tenant = await this.tenantModel
|
||||
.query()
|
||||
.findOne({ id: plaidItem.tenantId })
|
||||
.throwIfNotFound();
|
||||
|
||||
const user = await this.systemUserModel
|
||||
.query()
|
||||
.findOne({
|
||||
tenantId: tenant.id,
|
||||
})
|
||||
.modify('active')
|
||||
.throwIfNotFound();
|
||||
|
||||
this.clsService.set('organizationId', tenant.organizationId);
|
||||
this.clsService.set('userId', user.id);
|
||||
|
||||
return callback();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user