Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Mar 3, 2024
1 parent 727e55c commit a15adce
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 61 deletions.
17 changes: 10 additions & 7 deletions apps/dashboard/src/app/api/test/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
19 changes: 12 additions & 7 deletions packages/providers/src/gocardless/gocardless-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ export class GoCardLessApi {
}

const response = await this.#get<GetBanksResponse>(
`/api/v2/institutions/?country=${countryCode?.toLowerCase()}`
"/api/v2/institutions/",
null,
{
params: {
country: countryCode,
private_accounts_supported: false,
},
}
);

client.set(this.#banksCacheKey, response, {
Expand Down Expand Up @@ -146,12 +153,12 @@ export class GoCardLessApi {
}

async getAccounts({
accountId,
id,
countryCode,
}: GetAccountsRequest): Promise<GetAccountsResponse> {
const [banks, response] = await Promise.all([
this.getBanks(countryCode),
this.getRequestion(accountId),
this.getRequestion(id),
]);

return Promise.all(
Expand Down Expand Up @@ -191,10 +198,8 @@ export class GoCardLessApi {
return this.#get<GetRequisitionsResponse>("/api/v2/requisitions/");
}

async getRequestion(accountId: string): Promise<GetRequisitionResponse> {
return this.#get<GetRequisitionResponse>(
`/api/v2/requisitions/${accountId}/`
);
async getRequestion(id: string): Promise<GetRequisitionResponse> {
return this.#get<GetRequisitionResponse>(`/api/v2/requisitions/${id}/`);
}

async deleteRequisition(id: string): Promise<DeleteRequistionResponse> {
Expand Down
39 changes: 30 additions & 9 deletions packages/providers/src/gocardless/gocardless-provider.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand All @@ -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,
})
);
}
}
38 changes: 30 additions & 8 deletions packages/providers/src/gocardless/transform.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -78,7 +81,7 @@ export const transformTransaction = ({
transaction,
teamId,
accountId,
}: GoCardLessTransformTransactionParams) => {
}: TransformTransactionParams): BaseTransaction => {
const method = mapTransactionMethod(
transaction?.proprietaryBankTransactionCode
);
Expand Down Expand Up @@ -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",
};
};
57 changes: 43 additions & 14 deletions packages/providers/src/gocardless/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
Expand All @@ -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;
};
9 changes: 4 additions & 5 deletions packages/providers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GoCardLessProvider } from "./gocardless/gocardless-provider";
import { TellerProvider } from "./teller/teller-provider";
import {
GetAccountsParams,
GetTransactionsParams,
GetAccountsRequest,
GetTransactionsRequest,
TransactionProviderParams,
} from "./types";

Expand All @@ -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);
}
}
13 changes: 7 additions & 6 deletions packages/providers/src/teller/teller-provider.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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");
}
Expand All @@ -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,
Expand Down
21 changes: 16 additions & 5 deletions packages/providers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,33 @@ 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;
dateTo?: string;
accessToken?: string; // Teller
};

export type GetAccountsParams = {
export type GetAccountsRequest = {
id: string;
userId: string;
teamId: string;
accountId: string;
countryCode?: string;
bankConnectionId: string;
countryCode?: string; // GoCardLess
};

0 comments on commit a15adce

Please sign in to comment.