diff --git a/apps/dashboard/src/actions/banks/create-end-user-agreement-action.ts b/apps/dashboard/src/actions/banks/create-end-user-agreement-action.ts index 9597f02502..7b0a1cb2e8 100644 --- a/apps/dashboard/src/actions/banks/create-end-user-agreement-action.ts +++ b/apps/dashboard/src/actions/banks/create-end-user-agreement-action.ts @@ -1,17 +1,27 @@ "use server"; import { GoCardLessApi } from "@midday/providers/src/gocardless/gocardless-api"; +import { headers } from "next/headers"; import { redirect } from "next/navigation"; import { action } from "../safe-action"; import { createEndUserAgreementSchema } from "../schema"; export const createEndUserAgreementAction = action( createEndUserAgreementSchema, - async ({ institutionId, redirect: redirectTo }) => { + async ({ institutionId, isDesktop }) => { const api = new GoCardLessApi(); + const headersList = headers(); + + const domain = headersList.get("x-forwarded-host") || ""; + const protocol = headersList.get("x-forwarded-proto") || ""; + const pathname = headersList.get("x-invoke-path") || ""; const data = await api.createEndUserAgreement(institutionId); + const url = `${protocol}://${domain}`; + const redirectBase = isDesktop ? "midday://" : url; + const redirectTo = `${redirectBase}/${pathname}?step=account&provider=gocardless`; + const { link } = await api.buildLink({ redirect: redirectTo, institutionId, diff --git a/apps/dashboard/src/actions/schema.ts b/apps/dashboard/src/actions/schema.ts index 86b470bb1e..57f39bb632 100644 --- a/apps/dashboard/src/actions/schema.ts +++ b/apps/dashboard/src/actions/schema.ts @@ -284,5 +284,5 @@ export const manualSyncTransactionsSchema = z.object({ export const createEndUserAgreementSchema = z.object({ institutionId: z.string(), - redirect: z.string(), + isDesktop: z.boolean().optional(), }); diff --git a/apps/dashboard/src/components/modals/connect-gocardless-modal.tsx b/apps/dashboard/src/components/modals/connect-gocardless-modal.tsx index ef5f487e10..e8b2d5c260 100644 --- a/apps/dashboard/src/components/modals/connect-gocardless-modal.tsx +++ b/apps/dashboard/src/components/modals/connect-gocardless-modal.tsx @@ -17,7 +17,6 @@ import { Skeleton } from "@midday/ui/skeleton"; import { isDesktopApp } from "@todesktop/client-core/platform/todesktop"; import { Loader2 } from "lucide-react"; import { useAction } from "next-safe-action/hooks"; -import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { useQueryState } from "nuqs"; import { useEffect, useState } from "react"; @@ -92,14 +91,10 @@ function Row({ id, name, logo, onSelect }) { } export function ConnectGoCardLessModal({ countryCode }) { - const pathname = usePathname(); const [loading, setLoading] = useState(true); const [results, setResults] = useState([]); const [filteredResults, setFilteredResults] = useState([]); - const redirectBase = isDesktopApp() ? "midday://" : location.origin; - const redirect = `${redirectBase}/${pathname}?step=account&provider=gocardless`; - const createEndUserAgreement = useAction(createEndUserAgreementAction); const [step, setStep] = useQueryState("step", { @@ -179,7 +174,7 @@ export function ConnectGoCardLessModal({ countryCode }) { onSelect={() => createEndUserAgreement.execute({ institutionId: bank.id, - redirect, + isDesktop: isDesktopApp(), }) } /> diff --git a/apps/dashboard/src/components/reconnect-button.tsx b/apps/dashboard/src/components/reconnect-button.tsx index 19bf4a20dd..5d6a4e313e 100644 --- a/apps/dashboard/src/components/reconnect-button.tsx +++ b/apps/dashboard/src/components/reconnect-button.tsx @@ -20,7 +20,6 @@ export function ReconnectButton({ id, institutionId }) { onClick={() => createEndUserAgreement.execute({ institutionId, - redirect, }) } > diff --git a/packages/jobs/src/transactions/sync.ts b/packages/jobs/src/transactions/sync.ts index 752edf3b16..ac4dd26a95 100644 --- a/packages/jobs/src/transactions/sync.ts +++ b/packages/jobs/src/transactions/sync.ts @@ -27,29 +27,26 @@ client.defineJob({ provider: account.bank_connection.provider, }); - // const transactions = await provider.getTransactions({ - // teamId: account.team_id, - // accountId: account.account_id, - // accessToken: account.bank_connection?.access_token, - // }); + if (!account) { + return; + } + + const transactions = await provider.getTransactions({ + teamId: account.team_id, + accountId: account.account_id, + accessToken: account.bank_connection?.access_token, + bankAccountId: account.id, + }); // NOTE: We will get all the transactions at once for each account so // we need to guard against massive payloads - // await processPromisesBatch(transactions, BATCH_LIMIT, async (batch) => { - // // await supabase.from("transactions").upsert(batch, { - // // onConflict: "internal_id", - // // ignoreDuplicates: true, - // // }); - // }); + const { error, data: transactionsData } = await supabase + .from("decrypted_transactions") + .upsert(transactions, { + onConflict: "internal_id", + ignoreDuplicates: true, + }) + .select("*, name:decrypted_name"); }); - - try { - if (promises) { - await Promise.all(promises); - } - } catch (error) { - await io.logger.error(error); - throw Error("Something went wrong"); - } }, });