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 <noreply@anthropic.com>
This commit is contained in:
MangaValkandClaude Opus 5.5 committed 2026-11-19 20:44:36 +01:00
1 parent 759b0c0d62
commit 730d99d38b
2 files changed
+21 -3

No files matched your search

+16
View File
@@ -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 });
}
}
+5 -3
View File
@@ -4,17 +4,19 @@ import { useRouter } from "next/navigation";
import { useEffect } from "react"; import { useEffect } from "react";
// Opening broke syncs any bank that's due (the layout decides), in the background, once per 10 minutes per tab, // 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 }) { export function AutoSync({ due }: { due: boolean }) {
const router = useRouter(); const router = useRouter();
useEffect(() => { useEffect(() => {
if (!due) return;
try { try {
const last = Number(sessionStorage.getItem("broke:autosync")); const last = Number(sessionStorage.getItem("broke:autosync"));
if (last && Date.now() - last < 10 * 60_000) return; if (last && Date.now() - last < 10 * 60_000) return;
sessionStorage.setItem("broke:autosync", String(Date.now())); sessionStorage.setItem("broke:autosync", String(Date.now()));
} catch {} } 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]); }, [due, router]);
return null; return null;
} }