mirror of
https://github.com/Since-3/since3-portal.git
synced 2026-10-12 19:51:17 +00:00
24 lines
1.2 KiB
PL/PgSQL
24 lines
1.2 KiB
PL/PgSQL
-- KI-Demos der Vorlagen: Zähler pro Variante und Tag als Kostenbremse für öffentliche Links
|
|
create table public.ai_usage (
|
|
variant_id uuid not null references public.template_variants(id) on delete cascade,
|
|
day date not null default current_date,
|
|
requests integer not null default 0,
|
|
primary key (variant_id, day)
|
|
);
|
|
|
|
alter table public.ai_usage enable row level security;
|
|
|
|
create policy admin_select on public.ai_usage for select to authenticated
|
|
using ((select public.is_admin()));
|
|
|
|
-- Zählt eine Anfrage und meldet, ob sie noch im Tageslimit liegt. Nur der Server (service_role) ruft das auf.
|
|
create or replace function public.consume_ai_quota(p_variant_id uuid, p_limit integer)
|
|
returns boolean language sql volatile security definer set search_path = '' as $$
|
|
insert into public.ai_usage as u (variant_id, day, requests)
|
|
values (p_variant_id, current_date, 1)
|
|
on conflict (variant_id, day) do update set requests = u.requests + 1
|
|
returning requests <= p_limit;
|
|
$$;
|
|
|
|
revoke all on function public.consume_ai_quota(uuid, integer) from public, anon, authenticated;
|
|
grant execute on function public.consume_ai_quota(uuid, integer) to service_role;
|