From a15adceb3104500ee8fa789b8ff20b360a0ee488 Mon Sep 17 00:00:00 2001 From: Pontus Abrahamsson Date: Sun, 3 Mar 2024 21:06:09 +0100 Subject: [PATCH] wip --- apps/dashboard/src/app/api/test/route.ts | 17 +++--- .../src/gocardless/gocardless-api.ts | 19 ++++--- .../src/gocardless/gocardless-provider.ts | 39 ++++++++++--- .../providers/src/gocardless/transform.ts | 38 ++++++++++--- packages/providers/src/gocardless/types.ts | 57 ++++++++++++++----- packages/providers/src/index.ts | 9 ++- .../providers/src/teller/teller-provider.ts | 13 +++-- packages/providers/src/types.ts | 21 +++++-- 8 files changed, 152 insertions(+), 61 deletions(-) diff --git a/apps/dashboard/src/app/api/test/route.ts b/apps/dashboard/src/app/api/test/route.ts index b5e71e6b41..64f732002a 100644 --- a/apps/dashboard/src/app/api/test/route.ts +++ b/apps/dashboard/src/app/api/test/route.ts @@ -4,14 +4,17 @@ import { NextResponse } from "next/server"; export async function GET(req) { const provider = new TransactionProvider({ provider: "gocardless" }); - const data = await provider.getTransactions({ - teamId: "dd6a039e-d071-423a-9a4d-9ba71325d890", - accountId: "c16a6d43-f121-41bc-ab82-1636029470bc", - dateFrom: "2024-01-01", - dateTo: "2024-01-31", - }); + // const data = await provider.getTransactions({ + // teamId: "dd6a039e-d071-423a-9a4d-9ba71325d890", + // accountId: "c16a6d43-f121-41bc-ab82-1636029470bc", + // dateFrom: "2024-01-01", + // dateTo: "2024-01-31", + // }); - console.log(data); + const data = await provider.getAccounts({ + id: "b3b06174-e7a0-4cd4-ae64-8d966d58a305", + countryCode: "SE", + }); return NextResponse.json(data); } diff --git a/packages/providers/src/gocardless/gocardless-api.ts b/packages/providers/src/gocardless/gocardless-api.ts index 1715a63337..00577624ab 100644 --- a/packages/providers/src/gocardless/gocardless-api.ts +++ b/packages/providers/src/gocardless/gocardless-api.ts @@ -96,7 +96,14 @@ export class GoCardLessApi { } const response = await this.#get( - `/api/v2/institutions/?country=${countryCode?.toLowerCase()}` + "/api/v2/institutions/", + null, + { + params: { + country: countryCode, + private_accounts_supported: false, + }, + } ); client.set(this.#banksCacheKey, response, { @@ -146,12 +153,12 @@ export class GoCardLessApi { } async getAccounts({ - accountId, + id, countryCode, }: GetAccountsRequest): Promise { const [banks, response] = await Promise.all([ this.getBanks(countryCode), - this.getRequestion(accountId), + this.getRequestion(id), ]); return Promise.all( @@ -191,10 +198,8 @@ export class GoCardLessApi { return this.#get("/api/v2/requisitions/"); } - async getRequestion(accountId: string): Promise { - return this.#get( - `/api/v2/requisitions/${accountId}/` - ); + async getRequestion(id: string): Promise { + return this.#get(`/api/v2/requisitions/${id}/`); } async deleteRequisition(id: string): Promise { diff --git a/packages/providers/src/gocardless/gocardless-provider.ts b/packages/providers/src/gocardless/gocardless-provider.ts index eda1f793cd..95c35cc054 100644 --- a/packages/providers/src/gocardless/gocardless-provider.ts +++ b/packages/providers/src/gocardless/gocardless-provider.ts @@ -1,7 +1,7 @@ import { Provider } from "../interface"; -import { GetAccountsParams, GetTransactionsParams } from "../types"; +import { GetAccountsRequest, GetTransactionsRequest } from "../types"; import { GoCardLessApi } from "./gocardless-api"; -import { transformTransaction } from "./transform"; +import { transformAccount, transformTransaction } from "./transform"; export class GoCardLessProvider implements Provider { #api: GoCardLessApi; @@ -10,9 +10,12 @@ export class GoCardLessProvider implements Provider { this.#api = new GoCardLessApi(); } - async getTransactions(params: GetTransactionsParams) { - const { dateFrom, dateTo, teamId, accountId } = params; - + async getTransactions({ + dateFrom, + dateTo, + teamId, + accountId, + }: GetTransactionsRequest) { const response = await this.#api.getTransactions({ dateFrom, dateTo, @@ -28,14 +31,32 @@ export class GoCardLessProvider implements Provider { ); } - async getAccounts(params: GetAccountsParams) { - const { accountId, countryCode } = params; + async getAccounts({ + id, + countryCode, + teamId, + accountId, + userId, + bankConnectionId, + }: GetAccountsRequest) { + if (!countryCode) { + throw Error("No countryCode provided"); + } const response = await this.#api.getAccounts({ - accountId, + id, countryCode, }); - return response; + return response.map(({ account }) => + transformAccount({ + name: account.name, + currency: account.currency, + teamId, + accountId, + bankConnectionId, + userId, + }) + ); } } diff --git a/packages/providers/src/gocardless/transform.ts b/packages/providers/src/gocardless/transform.ts index be5bb752df..edccd550a5 100644 --- a/packages/providers/src/gocardless/transform.ts +++ b/packages/providers/src/gocardless/transform.ts @@ -1,8 +1,13 @@ import { capitalCase } from "change-case"; import { - GoCardLessTranformTransactionDescriptionParams, - GoCardLessTransaction, - GoCardLessTransformTransactionParams, + Account as BaseAccount, + Transaction as BaseTransaction, +} from "../types"; +import { + Transaction, + TransactionDescriptionParams, + TransformAccountParams, + TransformTransactionParams, } from "./types"; export const mapTransactionMethod = (method?: string) => { @@ -23,9 +28,7 @@ export const mapTransactionMethod = (method?: string) => { } }; -export const transformTransactionName = ( - transaction: GoCardLessTransaction -) => { +export const transformTransactionName = (transaction: Transaction) => { if (transaction?.additionalInformation) { return capitalCase(transaction.additionalInformation); } @@ -61,7 +64,7 @@ export const transformTransactionName = ( const transformDescription = ({ transaction, name, -}: GoCardLessTranformTransactionDescriptionParams) => { +}: TransactionDescriptionParams) => { if (transaction?.remittanceInformationUnstructuredArray?.length) { const text = transaction?.remittanceInformationUnstructuredArray.join(" "); const description = capitalCase(text); @@ -78,7 +81,7 @@ export const transformTransaction = ({ transaction, teamId, accountId, -}: GoCardLessTransformTransactionParams) => { +}: TransformTransactionParams): BaseTransaction => { const method = mapTransactionMethod( transaction?.proprietaryBankTransactionCode ); @@ -121,3 +124,22 @@ export const transformTransaction = ({ status: "posted", }; }; + +export const transformAccount = ({ + name, + currency, + userId, + teamId, + accountId, + bankConnectionId, +}: TransformAccountParams): BaseAccount => { + return { + name, + created_by: userId, + team_id: teamId, + account_id: accountId, + currency, + bank_connection_id: bankConnectionId, + provider: "gocardless", + }; +}; diff --git a/packages/providers/src/gocardless/types.ts b/packages/providers/src/gocardless/types.ts index e3dfa82526..be63174676 100644 --- a/packages/providers/src/gocardless/types.ts +++ b/packages/providers/src/gocardless/types.ts @@ -97,22 +97,24 @@ export type GetAccountResponse = { owner_name?: string; }; +export type Account = { + resourceId: string; + iban: string; + currency: string; + ownerName: string; + name: string; + product: string; + cashAccountType: string; +}; + export type AccountDetails = { - account: { - resourceId: string; - iban: string; - currency: string; - ownerName: string; - name: string; - product: string; - cashAccountType: string; - }; + account: Account; }; export type GetAccountDetailsResponse = GetAccountResponse & AccountDetails; export type GetAccountsRequest = { - accountId: string; + id: string; countryCode: string; }; @@ -147,10 +149,17 @@ export type DeleteRequistionResponse = { status_code: number; }; -export type GetAccountsResponse = GetAccountDetailsResponse & - { - bank?: Bank; - }[]; +export type GetAccountsResponse = { + id: string; + created: string; + last_accessed: string; + iban?: string; + institution_id: string; + status: string; + owner_name?: string; + account: Account; + bank?: Bank; +}[]; export type GetTransactionsRequest = { accountId: string; @@ -164,3 +173,23 @@ export type GetTransactionsResponse = { posted: Transaction[]; }; }; + +export type TransactionDescriptionParams = { + transaction: Transaction; + name?: string; +}; + +export type TransformTransactionParams = { + transaction: Transaction; + teamId: string; + accountId: string; +}; + +export type TransformAccountParams = { + name: string; + currency: string; + teamId: string; + userId: string; + accountId: string; + bankConnectionId: string; +}; diff --git a/packages/providers/src/index.ts b/packages/providers/src/index.ts index e0783b254f..239a8ea30b 100644 --- a/packages/providers/src/index.ts +++ b/packages/providers/src/index.ts @@ -1,8 +1,8 @@ import { GoCardLessProvider } from "./gocardless/gocardless-provider"; import { TellerProvider } from "./teller/teller-provider"; import { - GetAccountsParams, - GetTransactionsParams, + GetAccountsRequest, + GetTransactionsRequest, TransactionProviderParams, } from "./types"; @@ -19,17 +19,16 @@ export class TransactionProvider { break; case "plaid": throw Error("Not implemented"); - default: throw Error("No provider selected"); } } - async getTransactions(params: GetTransactionsParams) { + async getTransactions(params: GetTransactionsRequest) { return this.#provider?.getTransactions(params); } - async getAccounts(params: GetAccountsParams) { + async getAccounts(params: GetAccountsRequest) { return this.#provider?.getAccounts(params); } } diff --git a/packages/providers/src/teller/teller-provider.ts b/packages/providers/src/teller/teller-provider.ts index db204307f3..b19ec3bfd8 100644 --- a/packages/providers/src/teller/teller-provider.ts +++ b/packages/providers/src/teller/teller-provider.ts @@ -1,5 +1,5 @@ import { Provider } from "../interface"; -import { GetAccountsParams, GetTransactionsParams } from "../types"; +import { GetAccountsRequest, GetTransactionsRequest } from "../types"; import { TellerApi } from "./teller-api"; import { transformTransaction } from "./transform"; @@ -10,9 +10,11 @@ export class TellerProvider implements Provider { this.#api = new TellerApi(); } - async getTransactions(params: GetTransactionsParams) { - const { teamId, accountId, accessToken } = params; - + async getTransactions({ + teamId, + accountId, + accessToken, + }: GetTransactionsRequest) { if (!accessToken) { throw Error("accessToken missing"); } @@ -31,8 +33,7 @@ export class TellerProvider implements Provider { ); } - async getAccounts(params: GetAccountsParams) { - // const { accountId, countryCode } = params; + async getAccounts({ accountId, countryCode }: GetAccountsRequest) { // const response = await this.#api.getAccounts({ // accountId, // countryCode, diff --git a/packages/providers/src/types.ts b/packages/providers/src/types.ts index fdb1e7feae..8416a72b9e 100644 --- a/packages/providers/src/types.ts +++ b/packages/providers/src/types.ts @@ -18,14 +18,21 @@ export type Transaction = { method: string; name: string; description?: string; + currency_rate?: number; + currency_source?: string; }; -export type Accounts = { - id: string; +export type Account = { + created_by: string; + team_id: string; + account_id: string; + name: string; + currency: string; + bank_connection_id: string; provider: Providers; }; -export type GetTransactionsParams = { +export type GetTransactionsRequest = { teamId: string; accountId: string; dateFrom?: string; @@ -33,7 +40,11 @@ export type GetTransactionsParams = { accessToken?: string; // Teller }; -export type GetAccountsParams = { +export type GetAccountsRequest = { + id: string; + userId: string; + teamId: string; accountId: string; - countryCode?: string; + bankConnectionId: string; + countryCode?: string; // GoCardLess };