From b4a5044f8ad084caf00f8057f442ebb202f7e764 Mon Sep 17 00:00:00 2001 From: minimus7 Date: Thu, 12 Nov 2026 11:05:36 -0600 Subject: [PATCH] Accounts: rename, set the kind (bank account or credit card), and delete - An Edit button on every account: name, and what kind it is (bank account, credit card, loan, investment, something else). The kind decides where it counts: a credit card's balance is money you owe. - The kind you pick sticks: migration 0031 adds accounts.type_override, and a trigger puts it back whenever a sync writes the bank's type, so every page keeps reading accounts.type. - An imported or hand-added credit card or loan now adds its purchases to what you owe (it subtracted them, as for a bank account). - Delete for accounts you added or imported, with their transactions. A linked account says to hide it or remove the bank, since sync would bring it back. Tested on a local Supabase: rename + Credit card moved the imported account under Credit cards; a sync-style write of type depository kept it credit; deleting removed it and its 358 transactions. Co-Authored-By: Claude Opus 5.5 --- src/app/(app)/accounts/page.tsx | 2 + src/components/AccountEdit.tsx | 97 +++++++++++++++++++++++ supabase/migrations/0031_account_kind.sql | 29 +++++++ 3 files changed, 128 insertions(+) create mode 100644 src/components/AccountEdit.tsx create mode 100644 supabase/migrations/0031_account_kind.sql diff --git a/src/app/(app)/accounts/page.tsx b/src/app/(app)/accounts/page.tsx index dac943c..fb653f7 100644 --- a/src/app/(app)/accounts/page.tsx +++ b/src/app/(app)/accounts/page.tsx @@ -1,5 +1,6 @@ import { Banknote, BarChart3, Bitcoin, Car, CreditCard, Gem, HandCoins, House, Landmark, type LucideIcon } from "lucide-react"; import Link from "next/link"; +import { AccountEdit } from "@/components/AccountEdit"; import { AccountToggle } from "@/components/AccountToggle"; import { HoldingFee } from "@/components/HoldingFee"; import { CardArt } from "@/components/CardArt"; @@ -123,6 +124,7 @@ export default async function Accounts() {
{a.item.institution_name}{a.subtype ? ` ยท ${subtypeLabel(a.subtype)}` : ""}
{money(a.current_balance, a.currency)} + {!!a.holdings?.length && ( diff --git a/src/components/AccountEdit.tsx b/src/components/AccountEdit.tsx new file mode 100644 index 0000000..b301bc4 --- /dev/null +++ b/src/components/AccountEdit.tsx @@ -0,0 +1,97 @@ +"use client"; + +import { Pencil, X } from "lucide-react"; +import { useRouter } from "next/navigation"; +import { useState } from "react"; +import { Dropdown } from "@/components/Dropdown"; +import { Modal } from "@/components/Modal"; +import { toast } from "@/components/Toast"; +import { createClient } from "@/utils/supabase/client"; + +const KINDS = [ + { value: "depository", label: "Bank account (checking or savings)" }, + { value: "credit", label: "Credit card" }, + { value: "loan", label: "Loan" }, + { value: "investment", label: "Investment" }, + { value: "other", label: "Something else (cash, a car, a home)" }, +]; + +// 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 }) { + const router = useRouter(); + const [open, setOpen] = useState(false); + const [label, setLabel] = useState(name); + const [kind, setKind] = useState(type); + const [busy, setBusy] = useState(false); + const [confirm, setConfirm] = useState(false); + const [err, setErr] = useState(""); + const close = () => { setOpen(false); setConfirm(false); setErr(""); }; + + async function save(e: React.FormEvent) { + e.preventDefault(); + setBusy(true); + const patch: Record = { custom_name: label.trim() && label.trim() !== bankName ? label.trim() : null }; + if (kind !== type) patch.type_override = kind; + // RLS blocks with 0 rows, not an error. + const { data, error } = await createClient().from("accounts").update(patch).eq("id", id).select("id"); + setBusy(false); + if (error || !data?.length) return setErr(/type_override/.test(error?.message ?? "") ? "This needs a database update. Open Setup to run it." : "Couldn't save that. Try again."); + close(); + toast("Saved"); + router.refresh(); + } + + async function remove() { + setBusy(true); + const { data, error } = await createClient().from("accounts").delete().eq("id", id).select("id"); + setBusy(false); + if (error || !data?.length) return setErr("Couldn't delete it. Try again."); + close(); + toast(`Deleted ${name}`); + router.refresh(); + } + + return ( + <> + + {open && ( + +
+
+
Edit account
+ +
+ +
+ What kind of account is it? + +

A credit card's balance counts as money you owe. A bank account's counts as money you have.

+
+ {err &&

{err}

} + +
+ {!manual ? ( +

This account comes from your bank, so it comes back on the next sync. To stop counting it, use the switch next to it. To remove the whole bank, use Remove under Connections.

+ ) : confirm ? ( +
+ Delete it and all its transactions? This can't be undone. + + +
+ ) : ( + + )} +
+
+
+ )} + + ); +} diff --git a/supabase/migrations/0031_account_kind.sql b/supabase/migrations/0031_account_kind.sql new file mode 100644 index 0000000..2655cea --- /dev/null +++ b/supabase/migrations/0031_account_kind.sql @@ -0,0 +1,29 @@ +-- The kind you pick for an account (bank account, credit card, loan...) wins over what the bank says, sync after sync. +-- Every read stays on accounts.type: this trigger puts the override back whenever a sync writes the bank's type. +alter table public.accounts add column if not exists type_override text + check (type_override in ('depository', 'credit', 'loan', 'investment', 'other')); +grant update (type_override) on public.accounts to authenticated; + +create or replace function public.apply_type_override() returns trigger +language plpgsql set search_path = '' as $$ +begin + if new.type_override is not null and new.type is distinct from new.type_override then + new.type := new.type_override; + new.subtype := null; -- the bank's subtype ("checking") is wrong for the kind you picked + end if; + return new; +end $$; + +drop trigger if exists accounts_type_override on public.accounts; +create trigger accounts_type_override before insert or update on public.accounts + for each row execute function public.apply_type_override(); + +-- A hand-added or imported account's balance comes from its transactions (positive = money out). On a bank account that +-- lowers what you have; on a credit card or loan it raises what you owe. +create or replace function public.balance(public.accounts) returns numeric +language sql stable set search_path = '' as $$ + select case when $1.manual + then coalesce($1.current_balance, 0) + case when $1.type in ('credit', 'loan') then 1 else -1 end + * coalesce((select sum(t.amount) from public.transactions t where t.account_id = $1.id), 0) + else $1.current_balance end +$$;