Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Mar 6, 2024
1 parent e0cb34a commit e65f0f5
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 115 deletions.
6 changes: 3 additions & 3 deletions apps/dashboard/src/app/api/test/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Provider } from "@midday/providers";
import { NextResponse } from "next/server";

export async function GET(req) {
const provider = new Provider({ provider: "teller" });
const provider = new Provider({ provider: "plaid" });

// const data = await provider.getTransactions({
// teamId: "dd6a039e-d071-423a-9a4d-9ba71325d890",
Expand All @@ -22,9 +22,9 @@ export async function GET(req) {

const data = await provider.getTransactions({
teamId: "123",
accountId: "acc_os41qe3a66ks2djhss000",
accessToken: "test_token_53oy4vz2xfgl6",
accessToken: "access-sandbox-fa4c2b4c-1b25-4e66-a8be-504fa51bf5a3",
bankAccountId: "a87e60b9-8323-4bab-8dc9-4ac9786b48a6",
accountId: "AG7EkLW7DRSVaN8Z75jMT1DJN51QpWc9LKB7w",
});

// const provider = new Provider({ provider: "gocardless" });
Expand Down
25 changes: 15 additions & 10 deletions apps/dashboard/src/components/modals/select-bank-accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,15 @@ export function SelectBankAccountsModal({ countryCode }) {
className="flex justify-between"
>
<FormLabel className="flex items-between space-x-4">
{/* <Image
src={account.institution?.logo}
alt={account?.institution?.name}
width={34}
height={34}
className="rounded-full overflow-hidden border"
/> */}
{account?.institution?.logo && (
<Image
src={account.institution.logo}
alt={account?.institution?.name}
width={34}
height={34}
className="rounded-full overflow-hidden border"
/>
)}
<div className="space-y-1">
<p className="text-sm font-medium leading-none mb-1">
{account.name}
Expand Down Expand Up @@ -245,9 +247,12 @@ export function SelectBankAccountsModal({ countryCode }) {
<Button
className="w-full"
type="submit"
// disabled={connectBankAction.status === "executing"}
// Check for atleast one enabled account
disabled
disabled={
connectBankAction.status === "executing" ||
!form
.getValues("accounts")
.find((account) => account.enabled)
}
>
{connectBankAction.status === "executing" ? (
<Loader2 className="w-4 h-4 animate-spin pointer-events-none" />
Expand Down
8 changes: 5 additions & 3 deletions apps/dashboard/src/components/transaction-bank-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export function TransactionBankAccount({
}) {
return (
<div className="flex space-x-2 mt-1 items-center">
<div className="rounded-full overflow-hidden">
<Image src={logoUrl} alt={name} width={size} height={size} />
</div>
{logoUrl && (
<div className="rounded-full overflow-hidden">
<Image src={logoUrl} alt={name} width={size} height={size} />
</div>
)}
<span className={cn("text-sm", className)}>{name}</span>
</div>
);
Expand Down
12 changes: 7 additions & 5 deletions apps/dashboard/src/components/transaction-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ export function TransactionDetails({ transactionId, data: initialData }) {
</div>
) : (
<div className="flex items-center justify-between">
<TransactionBankAccount
name={data?.bank_account?.name}
logoUrl={data?.bank_account?.bank_connection?.logo_url}
className="text-[#606060] text-xs"
/>
{data?.bank_account?.bank_connection?.logo_url && (
<TransactionBankAccount
name={data?.bank_account?.name}
logoUrl={data.bank_account.bank_connection.logo_url}
className="text-[#606060] text-xs"
/>
)}
<span className="text-[#606060] text-xs">
{data?.date && format(new Date(data.date), "MMM d, y")}
</span>
Expand Down
10 changes: 1 addition & 9 deletions packages/providers/src/gocardless/gocardless-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ export class GoCardLessProvider implements Provider {
countryCode,
});

return response.map(({ id, account, bank }) =>
transformAccount({
id,
name: account.name,
currency: account.currency,
bank,
product: account.product,
})
);
return response.map(transformAccount);
}
}
26 changes: 14 additions & 12 deletions packages/providers/src/gocardless/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
} from "../types";
import {
Transaction,
TransactionDescriptionParams,
TransactionDescription,
TransformAccount,
TransformAccountName,
TransformAccountParams,
TransformTransactionParams,
TransformTransaction,
} from "./types";

export const mapTransactionCategory = (transaction: Transaction) => {
Expand Down Expand Up @@ -77,7 +77,7 @@ export const transformTransactionName = (transaction: Transaction) => {
const transformDescription = ({
transaction,
name,
}: TransactionDescriptionParams) => {
}: TransactionDescription) => {
if (transaction?.remittanceInformationUnstructuredArray?.length) {
const text = transaction?.remittanceInformationUnstructuredArray.join(" ");
const description = capitalCase(text);
Expand All @@ -94,7 +94,7 @@ export const transformTransaction = ({
transaction,
teamId,
bankAccountId,
}: TransformTransactionParams): BaseTransaction => {
}: TransformTransaction): BaseTransaction => {
const method = mapTransactionMethod(
transaction?.proprietaryBankTransactionCode
);
Expand Down Expand Up @@ -125,7 +125,7 @@ export const transformTransaction = ({
name,
method,
internal_id: `${teamId}_${transaction.internalTransactionId}`,
amount: transaction.transactionAmount.amount,
amount: +transaction.transactionAmount.amount,
currency: transaction.transactionAmount.currency,
bank_account_id: bankAccountId,
category: mapTransactionCategory(transaction),
Expand Down Expand Up @@ -156,15 +156,17 @@ const transformAccountName = (account: TransformAccountName) => {

export const transformAccount = ({
id,
name,
currency,
account,
bank,
product,
}: TransformAccountParams): BaseAccount => {
}: TransformAccount): BaseAccount => {
return {
id,
name: transformAccountName({ name, bank, product }),
currency,
name: transformAccountName({
name: account.name,
bank,
product: account.product,
}),
currency: account.currency,
institution: bank && {
id: bank?.id,
logo: bank?.logo,
Expand Down
12 changes: 3 additions & 9 deletions packages/providers/src/gocardless/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,18 @@ export type GetTransactionsResponse = {
};
};

export type TransactionDescriptionParams = {
export type TransactionDescription = {
transaction: Transaction;
name?: string;
};

export type TransformTransactionParams = {
export type TransformTransaction = {
transaction: Transaction;
teamId: string;
bankAccountId: string;
};

export type TransformAccountParams = {
id: string;
name: string;
currency: string;
product: string;
bank?: Bank;
};
export type TransformAccount = GetAccountsResponse[0];

export type TransformAccountName = {
name: string;
Expand Down
14 changes: 14 additions & 0 deletions packages/providers/src/plaid/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP

exports[`Transform pending transaction 1`] = `
{
"amount": 5.4,
"balance": null,
"bank_account_id": "123",
"currency": "CAD",
"date": "2024-02-24",
"internal_id": "123_NxkDjlyk45cQoDm5PEqJuKJaw6qrj9cy89zBA",
"status": "pending",
"team_id": "123",
}
`;
29 changes: 22 additions & 7 deletions packages/providers/src/plaid/plaid-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
PlaidApi as PlaidBaseApi,
PlaidEnvironments,
Products,
TransactionsSyncResponse,
Transaction,
} from "plaid";
import {
GetAccountsRequest,
GetAccountsResponse,
GetTransactionsRequest,
GetTransactionsResponse,
ItemPublicTokenExchangeRequest,
LinkTokenCreateRequest,
} from "./types";
Expand Down Expand Up @@ -56,12 +57,26 @@ export class PlaidApi {

async getTransactions({
accessToken,
}: GetTransactionsRequest): Promise<
import("axios").AxiosResponse<TransactionsSyncResponse>
> {
return this.#client.transactionsSync({
access_token: accessToken,
});
accountId,
}: GetTransactionsRequest): Promise<GetTransactionsResponse> {
let added: Array<Transaction> = [];
let cursor = undefined;
let hasMore = true;

while (hasMore) {
const { data } = await this.#client.transactionsSync({
access_token: accessToken,
cursor,
});

added = added.concat(data.added);
hasMore = data.has_more;
cursor = data.next_cursor;
}

// NOTE: Plaid transactions for all accounts, we need to filter based on the
// Provided accountId
return added.filter((transaction) => transaction.account_id === accountId);
}

async linkTokenCreate({
Expand Down
19 changes: 6 additions & 13 deletions packages/providers/src/plaid/plaid-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ export class PlaidProvider implements Provider {
accessToken,
teamId,
accountId,
bankAccountId,
}: GetTransactionsRequest) {
if (!accessToken) {
throw Error("accessToken missing");
if (!accessToken || !accountId) {
throw Error("accessToken or accountId is missing");
}

const response = await this.#api.getTransactions({
accessToken,
accountId,
});

return response.map((transaction) =>
transformTransaction({
transaction,
teamId,
accountId,
bankAccountId,
})
);
}
Expand All @@ -42,15 +44,6 @@ export class PlaidProvider implements Provider {
institutionId,
});

return response?.map((account) => {
return transformAccount({
id: account.account_id,
name: account.name,
currency:
account.balances.iso_currency_code ||
account.balances.unofficial_currency_code,
institution: account.institution,
});
});
return response?.map(transformAccount);
}
}
Loading

0 comments on commit e65f0f5

Please sign in to comment.