Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Mar 8, 2024
1 parent 0d4ae3b commit 4a4ec46
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/actions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,5 @@ export const manualSyncTransactionsSchema = z.object({

export const createEndUserAgreementSchema = z.object({
institutionId: z.string(),
redirect: z.string(),
isDesktop: z.boolean().optional(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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", {
Expand Down Expand Up @@ -179,7 +174,7 @@ export function ConnectGoCardLessModal({ countryCode }) {
onSelect={() =>
createEndUserAgreement.execute({
institutionId: bank.id,
redirect,
isDesktop: isDesktopApp(),
})
}
/>
Expand Down
1 change: 0 additions & 1 deletion apps/dashboard/src/components/reconnect-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export function ReconnectButton({ id, institutionId }) {
onClick={() =>
createEndUserAgreement.execute({
institutionId,
redirect,
})
}
>
Expand Down
37 changes: 17 additions & 20 deletions packages/jobs/src/transactions/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
},
});

0 comments on commit 4a4ec46

Please sign in to comment.