feat(server): wip syncing Plaid transactions

This commit is contained in:
Ahmed Bouhuolia
2024-02-02 02:23:49 +02:00
parent b43cd26ecc
commit b940c6dd17
15 changed files with 491 additions and 7 deletions

View File

@@ -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);
}
}

View 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);
}
}

View File

@@ -0,0 +1,4 @@
export interface PlaidItemDTO {
publicToken: string;
institutionId: string;
}

View File

@@ -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);