Transfers between own accounts no longer count as spending and income
Imported statements come in without a category, so money moved to savings counted as spent on one side and earned on the other. After every import and sync, brokeless now marks them as Transfers out / in: pairs (same amount out of one own account and into another within 4 days) and savings accounts by name (Oranje Spaarrekening, Doelspaar, Direct Sparen...). Only rows without a category or with Other change. All history, so existing imports get fixed on the next sync. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
49ecfb975f
commit
2371a5674f
5 files changed
+88
-3
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { saveFoundRecurring } from "@/lib/recurring-save";
|
||||
import { NextResponse } from "next/server";
|
||||
import { plaidError } from "@/lib/plaid";
|
||||
import { ITEM_COLS, syncItem } from "@/lib/sync";
|
||||
import { fileOwnTransfers, ITEM_COLS, syncItem } from "@/lib/sync";
|
||||
import { createAdminClient } from "@/utils/supabase/admin";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
|
||||
@@ -35,6 +35,8 @@ export async function POST() {
|
||||
results.push({ id: item.id, added: 0, modified: 0, removed: 0, error: err.code });
|
||||
}
|
||||
}
|
||||
// Transfers between own accounts nobody marked (imported statements): neither spending nor income.
|
||||
await fileOwnTransfers(user.id).catch((e) => console.error("[broke] own transfers:", e instanceof Error ? e.message : e));
|
||||
// broke's own recurring finder, over every account (Plaid's misses, feeds, imported statements).
|
||||
await saveFoundRecurring(user.id).catch((e) => console.error("[broke] recurring:", e instanceof Error ? e.message : e));
|
||||
return NextResponse.json({ ok: true, results });
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { saveFoundRecurring } from "@/lib/recurring-save";
|
||||
import { fileOwnTransfers } from "@/lib/sync";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
|
||||
// Finds charges and pay that repeat in the signed-in user's own transactions. Called after a statement import.
|
||||
export async function POST() {
|
||||
const { data: { user } } = await (await createClient()).auth.getUser();
|
||||
if (!user) return NextResponse.json({ error: "Not signed in" }, { status: 401 });
|
||||
// A statement just came in: mark transfers between own accounts first, so they aren't found as repeating charges.
|
||||
await fileOwnTransfers(user.id).catch((e) => console.error("[broke] own transfers:", e instanceof Error ? e.message : e));
|
||||
return NextResponse.json(await saveFoundRecurring(user.id));
|
||||
}
|
||||
+27
-1
@@ -5,7 +5,7 @@ import { akahuTokens } from "@/lib/providers";
|
||||
import { akahuAccounts, akahuTransactions, toAccount, toTxn } from "@/lib/providers/akahu";
|
||||
import { feedData, isFeed, type FeedAccount, type FeedId, type FeedTxn } from "@/lib/providers/feeds";
|
||||
import { applyRules, RULE_COLS, type Rule } from "@/lib/rules";
|
||||
import { pairTransfers } from "@/lib/transfers";
|
||||
import { pairTransfers, findOwnTransfers, type OwnRow } from "@/lib/transfers";
|
||||
import { createAdminClient } from "@/utils/supabase/admin";
|
||||
|
||||
type Item = { id: string; user_id: string; access_token: string | null; cursor: string | null; provider?: string | null };
|
||||
@@ -160,6 +160,32 @@ export async function fileTransferPairs(userId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Transfers between the person's own accounts that came in uncategorized (statement imports above all): both sides
|
||||
* become Transfers out / in, so they count as neither spending nor income. All history, read in pages. */
|
||||
export async function fileOwnTransfers(userId: string) {
|
||||
const db = createAdminClient();
|
||||
const rows: OwnRow[] = [];
|
||||
for (let from = 0; ; from += 1000) {
|
||||
const { data, error } = await db.from("transactions").select("id, date, amount, account_id, name, category:categories(key)").eq("user_id", userId).order("id").range(from, from + 999);
|
||||
if (error) throw error;
|
||||
for (const r of (data ?? []) as unknown as { id: string; date: string; amount: number; account_id: string; name: string | null; category: { key: string } | null }[])
|
||||
rows.push({ id: r.id, date: r.date, amount: Number(r.amount), account_id: r.account_id, name: r.name ?? "", categoryKey: r.category?.key ?? null });
|
||||
if ((data ?? []).length < 1000) break;
|
||||
}
|
||||
const found = findOwnTransfers(rows);
|
||||
if (!found.length) return 0;
|
||||
const { data: cats } = await db.from("categories").select("id, key").is("user_id", null).in("key", ["TRANSFER_OUT", "TRANSFER_IN"]);
|
||||
const idOf = Object.fromEntries((cats ?? []).map((c) => [c.key, c.id]));
|
||||
for (const key of ["TRANSFER_OUT", "TRANSFER_IN"] as const) {
|
||||
const ids = found.filter((f) => f.key === key).map((f) => f.id);
|
||||
for (let i = 0; i < ids.length; i += 200) {
|
||||
const { error } = await db.from("transactions").update({ category_id: idOf[key] }).eq("user_id", userId).in("id", ids.slice(i, i + 200));
|
||||
if (error) throw error;
|
||||
}
|
||||
}
|
||||
return found.length;
|
||||
}
|
||||
|
||||
// Plaid sends a new bank's recent weeks first and fetches up to 2 years of history in the background, which takes
|
||||
// minutes to hours. Until it says the history is in, sync the item again every 10 minutes, for up to 4 hours.
|
||||
// ponytail: in-process timers, lost on restart (the next Sync now picks it up); swap for Plaid's webhook once deployed.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Run: node src/lib/transfers.test.ts
|
||||
import assert from "node:assert/strict";
|
||||
import { pairTransfers } from "./transfers.ts";
|
||||
import { findOwnTransfers, pairTransfers } from "./transfers.ts";
|
||||
|
||||
const chk = { id: "chk", name: "Everyday Checking", mask: "1234" }, card = { id: "card", name: "Capital One", mask: "9876" };
|
||||
const t = (id: string, date: string, amount: number, account: typeof chk, kind: string | null = "transfer") => ({ id, date, amount, account, category: { kind } });
|
||||
@@ -19,3 +19,22 @@ p = pairTransfers([t("o1", "2026-10-01", 500, chk), t("o2", "2026-10-15", 500, c
|
||||
assert.deepEqual([...p.hidden].sort(), ["i1", "i2"], "each money-in pairs once");
|
||||
assert.equal(pairTransfers([t("in", "2026-10-03", -500, card)]).hidden.size, 0, "partner not in the list: shown");
|
||||
console.log("transfers ok");
|
||||
|
||||
// Own transfers nobody marked: pairs across own accounts, then savings names on their own.
|
||||
{
|
||||
const r = (id: string, date: string, amount: number, account_id: string, name = "x", categoryKey: string | null = null) => ({ id, date, amount, account_id, name, categoryKey });
|
||||
const found = findOwnTransfers([
|
||||
r("to-sav", "2026-10-01", 1000, "chk", "Naar spaar"), r("at-sav", "2026-10-02", -1000, "sav", "Van betaal"), // pair
|
||||
r("same-acct-a", "2026-10-03", 50, "chk"), r("same-acct-b", "2026-10-03", -50, "chk"), // same account: no
|
||||
r("far-a", "2026-10-01", 70, "chk"), r("far-b", "2026-10-20", -70, "sav"), // 19 days apart: no
|
||||
r("mine", "2026-10-05", 200, "chk", "x", "GROCERIES"), r("mine-b", "2026-10-05", -200, "sav"), // a chosen category stays
|
||||
r("oranje", "2026-10-07", 300, "chk", "Oranje Spaarrekening V123456"), // savings name, other side not imported
|
||||
r("ah", "2026-10-08", 42.17, "chk", "Albert Heijn 1234"),
|
||||
]);
|
||||
assert.deepEqual(found, [{ id: "to-sav", key: "TRANSFER_OUT" }, { id: "at-sav", key: "TRANSFER_IN" }, { id: "oranje", key: "TRANSFER_OUT" }]);
|
||||
// Two transfers of the same amount: each pairs with its nearest partner, once.
|
||||
const two = findOwnTransfers([r("o1", "2026-10-01", 500, "chk"), r("o2", "2026-10-15", 500, "chk"), r("i2", "2026-10-16", -500, "sav"), r("i1", "2026-10-02", -500, "sav")]);
|
||||
assert.deepEqual(two.map((t) => t.id).sort(), ["i1", "i2", "o1", "o2"]);
|
||||
assert.deepEqual(two.slice(0, 2), [{ id: "o1", key: "TRANSFER_OUT" }, { id: "i1", key: "TRANSFER_IN" }]);
|
||||
}
|
||||
console.log("own transfers ok");
|
||||
@@ -24,3 +24,38 @@ export function pairTransfers(txns: Row[]) {
|
||||
}
|
||||
return { hidden, to, pairs };
|
||||
}
|
||||
|
||||
// ---- Spotting transfers nobody marked yet ----
|
||||
// Money moved between the person's own accounts that came in uncategorized (statement imports have no category):
|
||||
// without this, moving €1,000 to savings counts as €1,000 spent on one side and €1,000 earned on the other.
|
||||
export type OwnRow = { id: string; date: string; amount: number; account_id: string; name: string; categoryKey: string | null };
|
||||
export type OwnTransfer = { id: string; key: "TRANSFER_OUT" | "TRANSFER_IN" };
|
||||
|
||||
// Savings accounts at Dutch banks, by the name the other side carries: ING (Oranje) Spaarrekening, Doelspaarrekening,
|
||||
// ABN AMRO Direct Sparen, Rabobank/ASN/SNS bonusrente and deposito accounts.
|
||||
const SAVINGS = /spaarrekening|doelspaar|direct sparen|bonusrente|spaardeposito|vermogensspaar|spaarpot/i;
|
||||
const open = (r: OwnRow) => r.categoryKey === null || r.categoryKey === "OTHER"; // a category the person chose stays
|
||||
|
||||
/** Pairs first (same amount out of one own account and into another within DAYS days, nearest date wins, each row used
|
||||
* once), then single rows whose name is a savings account. Only rows without a category, or "Other". */
|
||||
export function findOwnTransfers(rows: OwnRow[]): OwnTransfer[] {
|
||||
const out: OwnTransfer[] = [];
|
||||
const done = new Set<string>();
|
||||
const ins = rows.filter((r) => r.amount < 0 && open(r));
|
||||
for (const o of [...rows].filter((r) => r.amount > 0 && open(r)).sort((a, b) => a.date.localeCompare(b.date))) {
|
||||
let best: OwnRow | null = null, gap = Infinity;
|
||||
for (const i of ins) {
|
||||
if (done.has(i.id) || i.account_id === o.account_id || Math.abs(i.amount + o.amount) >= 0.005) continue;
|
||||
const d = Math.abs(Date.parse(i.date) - Date.parse(o.date));
|
||||
if (d <= DAYS * 864e5 && d < gap) { best = i; gap = d; }
|
||||
}
|
||||
if (!best) continue;
|
||||
done.add(o.id).add(best.id);
|
||||
out.push({ id: o.id, key: "TRANSFER_OUT" }, { id: best.id, key: "TRANSFER_IN" });
|
||||
}
|
||||
for (const r of rows) {
|
||||
if (done.has(r.id) || !open(r) || !SAVINGS.test(r.name)) continue;
|
||||
out.push({ id: r.id, key: r.amount > 0 ? "TRANSFER_OUT" : "TRANSFER_IN" });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user