Skip to content

Commit

Permalink
Add resource_id (#346)
Browse files Browse the repository at this point in the history
* Add resource_id

* Add resource_id

* Add resource_id

* Save account reference

* Sync account ref

* Sync account ref
  • Loading branch information
pontusab authored Dec 14, 2024
1 parent 1402433 commit b1221bb
Show file tree
Hide file tree
Showing 15 changed files with 2,013 additions and 1,943 deletions.
35 changes: 35 additions & 0 deletions apps/dashboard/jobs/tasks/bank/accounts/update-reference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { client } from "@midday/engine/client";
import { createClient } from "@midday/supabase/job";
import { schemaTask } from "@trigger.dev/sdk/v3";
import { z } from "zod";

export const updateReference = schemaTask({
id: "update-reference",
maxDuration: 300,
schema: z.object({
connectionId: z.string(),
}),
run: async ({ connectionId }) => {
const supabase = createClient();

const accountsResponse = await client.accounts.$get({
query: {
id: connectionId,
provider: "gocardless",
},
});

const { data: accountsData } = await accountsResponse.json();

await Promise.all(
accountsData.map(async (account) => {
return supabase
.from("bank_accounts")
.update({
account_reference: account.resource_id,
})
.eq("account_id", account.id);
}),
);
},
});
7 changes: 7 additions & 0 deletions apps/dashboard/jobs/tasks/bank/sync/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { logger, schemaTask } from "@trigger.dev/sdk/v3";
import { revalidateCache } from "jobs/utils/revalidate-cache";
import { triggerSequenceAndWait } from "jobs/utils/trigger-sequence";
import { z } from "zod";
import { updateReference } from "../accounts/update-reference";
import { transactionNotifications } from "../notifications/transactions";
import { syncAccount } from "./account";

Expand Down Expand Up @@ -140,6 +141,12 @@ export const syncConnection = schemaTask({
.update({ status: "disconnected" })
.eq("id", connectionId);
}

if (data.provider === "gocardless") {
await updateReference.trigger({
connectionId,
});
}
} catch (error) {
logger.error("Failed to check connection status by accounts", {
error,
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/src/actions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export const connectBankAccountSchema = z.object({
currency: z.string(),
name: z.string(),
institution_id: z.string(),
account_reference: z.string().nullable().optional(),
enabled: z.boolean(),
logo_url: z.string().nullable().optional(),
type: z.enum([
Expand Down
18 changes: 16 additions & 2 deletions apps/dashboard/src/components/modals/select-bank-accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useConnectParams } from "@/hooks/use-connect-params";
import { useI18n } from "@/locales/client";
import { getInitials } from "@/utils/format";
import { zodResolver } from "@hookform/resolvers/zod";
import type { Accounts } from "@midday-ai/engine/resources/accounts.mjs";
import { Avatar, AvatarFallback } from "@midday/ui/avatar";
import { Button } from "@midday/ui/button";
import {
Expand Down Expand Up @@ -39,6 +38,20 @@ import z from "zod";
import { FormatAmount } from "../format-amount";
import { LoadingTransactionsEvent } from "../loading-transactions-event";

type Account = {
id: string;
name: string;
balance: number;
currency: string;
type: string;
subtype: string;
mask: string;
institution: {
id: string;
name: string;
};
};

function RowsSkeleton() {
return (
<div className="space-y-6">
Expand Down Expand Up @@ -140,7 +153,7 @@ export function SelectBankAccountsModal() {
const { toast } = useToast();
const t = useI18n();

const [accounts, setAccounts] = useState<Accounts.Data[]>([]);
const [accounts, setAccounts] = useState<Account[]>([]);
const [loading, setLoading] = useState(true);
const [runId, setRunId] = useState<string>();
const [accessToken, setAccessToken] = useState<string>();
Expand Down Expand Up @@ -235,6 +248,7 @@ export function SelectBankAccountsModal() {
institution_id: account.institution.id,
logo_url: account.institution?.logo,
account_id: account.id,
account_reference: account.resource_id,
bank_name: account.institution.name,
// TODO: Remove once we have a fix and return currency from engine
currency: account.currency ?? account.balance.currency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ exports[`Transform accounts 1`] = `
"provider": "gocardless",
},
"name": "Pleo Account",
"resource_id": "3133",
"type": "depository",
}
`;
Expand Down
1 change: 1 addition & 0 deletions apps/engine/src/providers/gocardless/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export const transformAccount = ({
enrollment_id: null,
balance: transformAccountBalance(balance),
institution: transformInstitution(institution),
resource_id: account.resourceId,
};
};

Expand Down
1 change: 1 addition & 0 deletions apps/engine/src/providers/gocardless/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export type GetAccountsResponse = {
account: Account;
balance?: GetBalanceRequest;
institution: Institution;
resourceId?: string;
}[];

export type GetTransactionsRequest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ exports[`Transform accounts 1`] = `
"provider": "plaid",
},
"name": "Plaid Mortgage",
"resource_id": null,
"type": "other_asset",
}
`;
Expand Down
1 change: 1 addition & 0 deletions apps/engine/src/providers/plaid/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export const transformAccount = ({
logo: getLogoURL(institution.id),
provider: Providers.Enum.plaid,
},
resource_id: null,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ exports[`Transform accounts 1`] = `
"provider": "teller",
},
"name": "Platinum Card",
"resource_id": null,
"type": "credit",
}
`;
Expand Down
1 change: 1 addition & 0 deletions apps/engine/src/providers/teller/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export const transformAccount = ({
institution: transformInstitution(institution),
type: getType(type),
balance: transformAccountBalance(balance),
resource_id: null,
};
};

Expand Down
1 change: 1 addition & 0 deletions apps/engine/src/providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type Account = {
institution: Institution;
balance: Balance;
enrollment_id: string | null; // Teller
resource_id: string | null; // GoCardLess
};

export type ConnectionStatus = {
Expand Down
8 changes: 8 additions & 0 deletions apps/engine/src/routes/accounts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@ export const AccountSchema = z
enrollment_id: z
.string()
.openapi({
description: "Teller/Plaid enrollment id",
example: "add29d44-1b36-4bcc-b317-b2cbc73ab8e7",
})
.nullable(),
resource_id: z
.string()
.openapi({
description: "GoCardLess reference id",
example: "GBRGZX62Y8",
})
.nullable(),
})
.openapi("AccountSchema");

Expand Down
2 changes: 2 additions & 0 deletions packages/supabase/src/mutations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type CreateBankConnectionPayload = {
enabled: boolean;
balance: number;
type: "depository" | "credit" | "other_asset" | "loan" | "other_liability";
account_reference: string | null;
}[];
balance: number;
accessToken?: string;
Expand Down Expand Up @@ -84,6 +85,7 @@ export async function createBankConnection(
currency: account.currency,
enabled: account.enabled,
type: account.type,
account_reference: account.account_reference,
balance: account.balance ?? 0,
}),
{
Expand Down
Loading

0 comments on commit b1221bb

Please sign in to comment.