From 730d99d38b7da622bc5ce07eeb25989473078ebc Mon Sep 17 00:00:00 2001 From: MangaValk Date: Thu, 19 Nov 2026 20:44:36 +0100 Subject: [PATCH] Check for own transfers on every app open, not only with a bank sync The bank sync runs at most every 6 hours (and never without a linked bank), so transfers in imported statements stayed counted as spending and income. Co-Authored-By: Claude Opus 5.5 --- src/app/api/transfers/find/route.ts | 16 ++++++++++++++++ src/components/AutoSync.tsx | 8 +++++--- 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/app/api/transfers/find/route.ts diff --git a/src/app/api/transfers/find/route.ts b/src/app/api/transfers/find/route.ts new file mode 100644 index 0000000..000f9e4 --- /dev/null +++ b/src/app/api/transfers/find/route.ts @@ -0,0 +1,16 @@ +import { NextResponse } from "next/server"; +import { fileOwnTransfers } from "@/lib/sync"; +import { createClient } from "@/utils/supabase/server"; + +// Marks transfers between the signed-in user's own accounts (lib/transfers.ts findOwnTransfers). AutoSync calls it on +// opening broke, apart from the bank sync, which only runs every few hours: imported statements need it too. +export async function POST() { + const { data: { user } } = await (await createClient()).auth.getUser(); + if (!user) return NextResponse.json({ error: "Not signed in" }, { status: 401 }); + try { + return NextResponse.json({ marked: await fileOwnTransfers(user.id) }); + } catch (e) { + console.error("[broke] own transfers:", e instanceof Error ? e.message : e); + return NextResponse.json({ error: "Couldn't check for transfers." }, { status: 500 }); + } +} diff --git a/src/components/AutoSync.tsx b/src/components/AutoSync.tsx index c6db45a..60d5bbb 100644 --- a/src/components/AutoSync.tsx +++ b/src/components/AutoSync.tsx @@ -4,17 +4,19 @@ import { useRouter } from "next/navigation"; import { useEffect } from "react"; // Opening broke syncs any bank that's due (the layout decides), in the background, once per 10 minutes per tab, -// then refreshes the page with what came in. Without a deployed webhook this is how new and older history arrives. +// then refreshes the page with what came in. With nothing due it still checks for transfers between own accounts. Without a deployed webhook this is how new and older history arrives. export function AutoSync({ due }: { due: boolean }) { const router = useRouter(); useEffect(() => { - if (!due) return; try { const last = Number(sessionStorage.getItem("broke:autosync")); if (last && Date.now() - last < 10 * 60_000) return; sessionStorage.setItem("broke:autosync", String(Date.now())); } catch {} - fetch("/api/plaid/sync", { method: "POST" }).then((r) => r.ok && router.refresh()).catch(() => {}); + // A due bank syncs, and the sync also marks transfers. Otherwise only the transfer check runs: imported statements + // between own accounts would count as spending and income until the next bank sync, hours away or never. + if (due) fetch("/api/plaid/sync", { method: "POST" }).then((r) => r.ok && router.refresh()).catch(() => {}); + else fetch("/api/transfers/find", { method: "POST" }).then((r) => (r.ok ? r.json() : null)).then((b) => b?.marked && router.refresh()).catch(() => {}); }, [due, router]); return null; }