Accounts: Merge into a linked account

The account editor of an imported account gets a Merge into section: pick the
linked account, see what moves and what the bank already has, then merge.
Tested in the stack: older rows move, overlap counts once, edits carry over,
and the next sync leaves it alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
MangaValkandClaude Opus 5.5 committed 2026-11-19 19:28:56 +01:00
1 parent 8e830dc422
commit 49ecfb975f
3 files changed
+49 -4

No files matched your search

+1 -1
View File
@@ -78,7 +78,7 @@ Know before you start:
- **Restricted mode is for your own accounts.** Enable Banking calls it "individual non-commercial use". Their docs don't list a price; check their site.
- **Access runs out.** ING lets it last up to 180 days. Two weeks before, Accounts warns you; after, it says "Needs attention". Press **Link again** and approve at the bank: your accounts, history and edits stay, nothing is added twice.
- **Usually only payment accounts.** Banks must share payment accounts. Dutch banks often leave savings out, and investment accounts are never included. Keep importing statements for those.
- **A linked account and a CSV account are two accounts.** Where both cover the same days, those transactions count twice. Delete the CSV account once the link covers its history, or keep only the older part of the CSV.
- **A linked account and a CSV account are two accounts.** Where both cover the same days, those transactions count twice. Fix it in one step: Accounts, edit the CSV account, **Merge into a linked account**. Its older transactions move over, from the day the bank's history starts the bank's own are kept, and your edits on those days carry over.
- **Not yet tried against the real service.** It's checked against Enable Banking's API docs and tested with a stand-in. If the first link fails, the error is in the broke container's log (`docker logs brokeless-broke-1`).
### 1. Make an Enable Banking app
+2 -1
View File
@@ -128,7 +128,8 @@ export default async function Accounts() {
<div className="text-xs text-muted">{a.item.institution_name}{a.subtype ? ` · ${subtypeLabel(a.subtype)}` : ""}</div>
</div>
<span className="tabular font-medium">{money(a.current_balance, a.currency)}</span>
<AccountEdit id={a.id} name={a.custom_name || a.name} bankName={a.name} type={a.type} manual={a.item.id === "manual"} />
<AccountEdit id={a.id} name={a.custom_name || a.name} bankName={a.name} type={a.type} manual={a.item.id === "manual"}
targets={a.item.id === "manual" ? all.filter((b) => b.item.id !== "manual").map((b) => ({ id: b.id, label: `${b.custom_name || b.name}${b.mask ? ` ··${b.mask}` : ""} · ${b.item.institution_name ?? "Bank"}` })) : []} />
<AccountToggle id={a.id} included={a.included} name={a.custom_name || a.name} />
</div>
{!!a.holdings?.length && (
+46 -2
View File
@@ -6,6 +6,7 @@ import { useState } from "react";
import { Dropdown } from "@/components/Dropdown";
import { Modal } from "@/components/Modal";
import { toast } from "@/components/Toast";
import { plural } from "@/lib/format";
import { createClient } from "@/utils/supabase/client";
const KINDS = [
@@ -19,7 +20,10 @@ const KINDS = [
// Rename an account, say what kind it is, or delete it. The kind decides where it counts: a credit card's balance is a
// debt and its purchases are spending on the card. Linked accounts come back on the next sync, so only accounts you
// added or imported can be deleted; a linked one is hidden instead, or its bank removed under Connections.
export function AccountEdit({ id, name, bankName, type, manual }: { id: string; name: string; bankName: string; type: string; manual: boolean }) {
type Preview = { start: string | null; moved: number; replaced: number; edits: number };
const day = (d: string) => new Date(`${d}T12:00:00Z`).toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric", timeZone: "UTC" });
export function AccountEdit({ id, name, bankName, type, manual, targets = [] }: { id: string; name: string; bankName: string; type: string; manual: boolean; targets?: { id: string; label: string }[] }) {
const router = useRouter();
const [open, setOpen] = useState(false);
const [label, setLabel] = useState(name);
@@ -27,7 +31,30 @@ export function AccountEdit({ id, name, bankName, type, manual }: { id: string;
const [busy, setBusy] = useState(false);
const [confirm, setConfirm] = useState(false);
const [err, setErr] = useState("");
const close = () => { setOpen(false); setConfirm(false); setErr(""); };
// Merge this imported account into a linked one (api/accounts/merge): pick it, see what happens, then confirm.
const [into, setInto] = useState("");
const [preview, setPreview] = useState<Preview | null>(null);
const close = () => { setOpen(false); setConfirm(false); setErr(""); setInto(""); setPreview(null); };
const merge = (target: string, dry: boolean) =>
fetch("/api/accounts/merge", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ from: id, into: target, preview: dry }) })
.then(async (r) => ({ ok: r.ok, body: (await r.json().catch(() => ({}))) as Preview & { error?: string } }));
async function pick(target: string) {
setInto(target);
setPreview(null);
setErr("");
const { ok, body } = await merge(target, true);
if (!ok) return setErr(body.error ?? "Couldn't check that. Try again.");
setPreview(body);
}
async function doMerge() {
setBusy(true);
const { ok, body } = await merge(into, false);
setBusy(false);
if (!ok) return setErr(body.error ?? "Couldn't merge them. Try again.");
close();
toast(`Merged ${name}`);
router.refresh();
}
async function save(e: React.FormEvent) {
e.preventDefault();
@@ -89,6 +116,23 @@ export function AccountEdit({ id, name, bankName, type, manual }: { id: string;
<button type="button" className="text-bad underline underline-offset-2" onClick={() => setConfirm(true)}>Delete this account</button>
)}
</div>
{manual && !!targets.length && (
<div className="border-t border-line pt-4 space-y-2 text-sm">
<span className="block font-medium">Merge into a linked account</span>
<p className="text-xs text-muted">For a statement you imported before linking the same account. Where both have the same days, they count twice until you merge.</p>
<Dropdown label="Merge into" placeholder="Choose the linked account" value={into} onChange={pick} options={targets.map((t) => ({ value: t.id, label: t.label }))} />
{preview && (
<>
<p className="text-muted">
{preview.start
? <>Moves {plural(preview.moved, "transaction")} from before {day(preview.start)}. From then on it keeps the bank&apos;s own and drops {plural(preview.replaced, "imported one")}{preview.edits ? <> (your changes on {preview.edits} carry over)</> : null}. Then {name} is removed.</>
: <>Moves all {plural(preview.moved, "transaction")}, then {name} is removed.</>}
</p>
<button type="button" className="btn-primary" disabled={busy} onClick={doMerge}>{busy ? "Merging..." : "Merge"}</button>
</>
)}
</div>
)}
</form>
</Modal>
)}