ProjectOS / services /wallet /supabase /migrations /20261031000000_wallet_ledger.sql
AnthropicallyHuggingFaces's picture
Deploy feat/wallet-service
5bd4e3a verified
Raw History Blame Contribute Delete
3.39 kB
-- ProjectOS wallet: double-entry ledger, keyed by user_id for future multi-user.
create extension if not exists pgcrypto;
create table if not exists wallet_accounts (
id uuid primary key default gen_random_uuid(),
user_id uuid not null,
kind text not null check (kind in ('user_wallet','stripe_clearing','merchant_payable')),
currency char(3) not null default 'usd',
created_at timestamptz not null default now(),
unique (user_id, kind, currency)
);
create table if not exists wallet_transactions (
id uuid primary key,
user_id uuid not null,
kind text not null check (kind in ('funding','invoice_payment','adjustment')),
idempotency_key text not null,
memo text,
created_at timestamptz not null default now(),
unique (user_id, idempotency_key)
);
create table if not exists wallet_postings (
id bigserial primary key,
transaction_id uuid not null references wallet_transactions(id) on delete restrict,
account_id uuid not null references wallet_accounts(id) on delete restrict,
amount bigint not null check (amount <> 0)
);
create index if not exists wallet_postings_account_idx on wallet_postings(account_id);
create or replace view wallet_account_balances as
select account_id, coalesce(sum(amount),0)::bigint as balance from wallet_postings group by account_id;
-- Atomic, balanced insert. Rejects unbalanced sets and overdrawn user wallets.
create or replace function wallet_post_transaction(
p_id uuid, p_user_id uuid, p_kind text, p_key text, p_memo text, p_postings jsonb
) returns uuid language plpgsql security definer set search_path = public as $$
declare total bigint; bad int;
begin
select coalesce(sum((p->>'amount')::bigint),0) into total from jsonb_array_elements(p_postings) p;
if total <> 0 or jsonb_array_length(p_postings) < 2 then raise exception 'unbalanced transaction'; end if;
select count(*) into bad from jsonb_array_elements(p_postings) p
left join wallet_accounts a on a.id = (p->>'account_id')::uuid and a.user_id = p_user_id
where a.id is null;
if bad > 0 then raise exception 'posting account not owned by user'; end if;
insert into wallet_transactions(id,user_id,kind,idempotency_key,memo) values (p_id,p_user_id,p_kind,p_key,p_memo)
on conflict (user_id, idempotency_key) do nothing;
if not found then return p_id; end if;
insert into wallet_postings(transaction_id,account_id,amount)
select p_id, (p->>'account_id')::uuid, (p->>'amount')::bigint from jsonb_array_elements(p_postings) p;
if exists (select 1 from wallet_accounts a join wallet_account_balances b on b.account_id = a.id
where a.user_id = p_user_id and a.kind = 'user_wallet' and b.balance > 0) then
raise exception 'insufficient funds';
end if;
return p_id;
end $$;
alter table wallet_accounts enable row level security;
alter table wallet_transactions enable row level security;
alter table wallet_postings enable row level security;
-- Service role bypasses RLS. Future multi-user: add policies on auth.uid() = user_id.
create policy wallet_accounts_owner on wallet_accounts for select using (auth.uid() = user_id);
create policy wallet_tx_owner on wallet_transactions for select using (auth.uid() = user_id);
create policy wallet_postings_owner on wallet_postings for select using (
exists (select 1 from wallet_transactions t where t.id = transaction_id and t.user_id = auth.uid()));