mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
feat(server): wip syncing Plaid transactions
This commit is contained in:
@@ -1,18 +1,33 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { PlaidLinkTokenService } from './PlaidLinkToken';
|
||||
import { PlaidItemService } from './PlaidItem';
|
||||
import { PlaidItemDTO } from './_types';
|
||||
|
||||
@Service()
|
||||
export class PlaidApplication {
|
||||
@Inject()
|
||||
private getLinkTokenService: PlaidLinkTokenService;
|
||||
|
||||
@Inject()
|
||||
private plaidItemService: PlaidItemService;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param tenantId
|
||||
* @param itemId
|
||||
* @returns
|
||||
* Retrieves the Plaid link token.
|
||||
* @param {number} tenantId
|
||||
* @param {number} itemId
|
||||
* @returns
|
||||
*/
|
||||
public getLinkToken(tenantId: number) {
|
||||
return this.getLinkTokenService.getLinkToken(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exchanges the Plaid access token.
|
||||
* @param {number} tenantId
|
||||
* @param {PlaidItemDTO} itemDTO
|
||||
* @returns
|
||||
*/
|
||||
public exchangeToken(tenantId: number, itemDTO: PlaidItemDTO) {
|
||||
return this.plaidItemService.item(tenantId, itemDTO);
|
||||
}
|
||||
}
|
||||
|
||||
42
packages/server/src/services/Banking/Plaid/PlaidItem.ts
Normal file
42
packages/server/src/services/Banking/Plaid/PlaidItem.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { PlaidClientWrapper } from '@/lib/Plaid';
|
||||
import { PlaidItemDTO } from './_types';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { PlaidUpdateTransactions } from '@/lib/Plaid/PlaidUpdateTransactions';
|
||||
|
||||
@Service()
|
||||
export class PlaidItemService {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private plaidUpdateTranasctions: PlaidUpdateTransactions;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} tenantId
|
||||
* @param {PlaidItemDTO} itemDTO
|
||||
*/
|
||||
public async item(tenantId: number, itemDTO: PlaidItemDTO) {
|
||||
const { PlaidItem } = this.tenancy.models(tenantId);
|
||||
const { publicToken, institutionId } = itemDTO;
|
||||
|
||||
const plaidInstance = new PlaidClientWrapper();
|
||||
|
||||
// exchange the public token for a private access token and store with the item.
|
||||
const response = await plaidInstance.itemPublicTokenExchange({
|
||||
public_token: publicToken,
|
||||
});
|
||||
const plaidAccessToken = response.data.access_token;
|
||||
const plaidItemId = response.data.item_id;
|
||||
|
||||
const plaidItem = await PlaidItem.query().insertAndFetch({
|
||||
tenantId,
|
||||
plaidAccessToken,
|
||||
plaidItemId,
|
||||
plaidInstitutionId: institutionId,
|
||||
});
|
||||
|
||||
this.plaidUpdateTranasctions.updateTransactions(tenantId, plaidItemId);
|
||||
}
|
||||
}
|
||||
4
packages/server/src/services/Banking/Plaid/_types.ts
Normal file
4
packages/server/src/services/Banking/Plaid/_types.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface PlaidItemDTO {
|
||||
publicToken: string;
|
||||
institutionId: string;
|
||||
}
|
||||
@@ -86,6 +86,7 @@ export default class NewCashflowTransactionService {
|
||||
'cashflowAccountId',
|
||||
'creditAccountId',
|
||||
'branchId',
|
||||
'plaidAccountId'
|
||||
]);
|
||||
// Retreive the next invoice number.
|
||||
const autoNextNumber =
|
||||
@@ -124,7 +125,7 @@ export default class NewCashflowTransactionService {
|
||||
public newCashflowTransaction = async (
|
||||
tenantId: number,
|
||||
newTransactionDTO: ICashflowNewCommandDTO,
|
||||
userId: number
|
||||
userId?: number
|
||||
): Promise<{ cashflowTransaction: ICashflowTransaction }> => {
|
||||
const { CashflowTransaction, Account } = this.tenancy.models(tenantId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user