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 <noreply@anthropic.com>
This commit is contained in:
1 parent
1cf12f4619
commit
b4a5044f8a
3 files changed
+128
No files matched your search
@@ -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
|
||||
$$;
|
||||
Reference in new issue
Block a user