/ Cases / Restaurants / System
Purchasing, receiving matched against the invoice, location orders with approval, blind counts, waste, production, QR-tagged assets and costing from electronic invoices.
≈108
server functions
Oct 2026
warehouse off Loyverse
0
ledger edits: only new entries
/ The problem
The central warehouse supplied every location using a point of sale built for coffee shops. There was no receiving against invoices, no reliable counts and no real cost of what went out.
/ What we built
/ How it works
/ By the numbers
■ Real production data · counts per week, no amounts or personal data · Since launch (Oct 12); current week excluded.
/ Real code
-- ── A. Guardas del mayor ───────────────────────────────────────────────────
-- M2. Las guardas son security INVOKER a propósito: así current_user es quien escribe de verdad
-- (dentro de la RPC, su dueño; por PostgREST, service_role). Una marca falsificada desde la API
-- no alcanza. El chequeo va escrito en cada trigger (no en una función aparte) para que el rol de
-- la API no necesite EXECUTE de nada para recibir el mensaje claro.
create or replace function private.bodega_mayor_solo_por_rpc()
returns trigger language plpgsql security invoker set search_path = '' as $$
declare
v_rpc boolean := coalesce(current_setting('bodega.mayor_rpc', true), '') = '1'
and current_user not in ('service_role', 'anon', 'authenticated', 'authenticator');
begin
if tg_op = 'DELETE' then
raise exception 'mayor_inmutable: un movimiento no se borra; se revierte con otro movimiento' using errcode = 'P0001';
end if;
if not v_rpc then
raise exception 'mayor_inmutable: el mayor sólo cambia con bodega_asentar_movimiento' using errcode = 'P0001';
end if;
if tg_op = 'UPDATE' and (to_jsonb(new) - 'saldo_despues') is distinct from (to_jsonb(old) - 'saldo_despues') then
raise exception 'mayor_inmutable: de un movimiento sólo se completa el saldo después' using errcode = 'P0001';
end if;
return new;
end $$;
-- …
create trigger bodega_movimientos_solo_por_rpc before insert or update or delete on public.bodega_movimientos
for each row execute function private.bodega_mayor_solo_por_rpc();
■ The inventory ledger is immutable in the database itself: a movement is never deleted or edited, only reversed by another, and not even the API service key can bypass it.
/ Stack