GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — verifactu-postgres/ejemplos/02-supabase-envoltorio.sql at main · mrwolf99/verifactu-postgres

Expand file tree

/

02-supabase-envoltorio.sql

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
37 lines (35 loc) · 2.02 KB
/

02-supabase-envoltorio.sql

Copy path

File metadata and controls

37 lines (35 loc) · 2.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- verifactu-postgres · ejemplo 2: un envoltorio para Supabase
--
-- El esquema verifactu NO se expone en la API (no lo añadas a «Exposed schemas»). La app llama a una función de
-- public por PostgREST, supabase.rpc('verifactu_emitir_alta', {...}), y esa función decide quién emite y con qué
-- NIF. Aquí lo decide una marca en app_metadata, que solo escribe el servidor (service_role); nunca
-- user_metadata, que el propio usuario puede cambiar.
--
-- PostgREST abre una transacción READ COMMITTED por llamada: dos emisiones a la vez se encadenan en serie.
--
-- search_path = pg_catalog, pg_temp, como las funciones de la librería: en una SECURITY DEFINER, pg_temp tiene
-- que ir explícito y al final para que los nombres de tipo salgan siempre de pg_catalog.
--
-- Probado en un PostgreSQL corriente, simulando auth.jwt() y los roles anon y authenticated (test/pruebas/16).
-- Contra un proyecto de Supabase real: sin probar.
create or replace function public.verifactu_emitir_alta(
p_num_serie_factura text, p_fecha_expedicion_factura date, p_tipo_factura text,
p_cuota_total numeric, p_importe_total numeric, p_subsanacion boolean default false)
returns json
language plpgsql security definer set search_path = pg_catalog, pg_temp
as $$
declare
v_nif text := (select auth.jwt()) -> 'app_metadata' ->> 'verifactu_nif';
r verifactu.registro;
begin
if v_nif is null then
raise exception using errcode = '42501', message = 'verifactu: esta cuenta no puede emitir registros';
end if;
select * into r
from verifactu.emitir_alta(v_nif, p_num_serie_factura, p_fecha_expedicion_factura, p_tipo_factura,
p_cuota_total, p_importe_total, p_subsanacion);
return json_build_object('seq', r.seq, 'huella', r.huella, 'fecha_hora', r.fecha_hora_huso_gen_registro);
end
$$;
revoke all on function public.verifactu_emitir_alta(text, date, text, numeric, numeric, boolean) from public, anon;
grant execute on function public.verifactu_emitir_alta(text, date, text, numeric, numeric, boolean) to authenticated;
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.