see comments

This commit is contained in:
Rajpreet0 2026-09-28 16:15:01 +02:00
parent a9fdcf9fe5
commit 6dd4587818
9 changed files with 253 additions and 57 deletions

View file

@ -0,0 +1,69 @@
-- Legt einen neuen clients-Datensatz an und verknüpft ihn atomar mit profiles.client_id.
-- Der "where client_id is null"-Guard verhindert, dass zwei parallele Submits
-- jeweils einen eigenen clients-Datensatz erzeugen und verwaisen lassen.
create or replace function public.create_client_and_link_profile(
p_profile_id uuid,
p_company_name text,
p_legal_form text,
p_industry text,
p_vat_id text,
p_tax_number text,
p_commercial_register_number text,
p_street text,
p_postal_code text,
p_city text,
p_country char(2),
p_billing_street text,
p_billing_postal_code text,
p_billing_city text,
p_billing_country char(2),
p_contact_name text,
p_contact_position text,
p_contact_email text,
p_contact_phone text
) returns uuid
language plpgsql
security definer
set search_path = ''
as $$
declare
v_client_id uuid;
v_updated_rows int;
begin
insert into public.clients (
company_name, legal_form, industry, vat_id, tax_number, commercial_register_number,
street, postal_code, city, country,
billing_street, billing_postal_code, billing_city, billing_country,
contact_name, contact_position, contact_email, contact_phone
) values (
p_company_name, p_legal_form, p_industry, p_vat_id, p_tax_number, p_commercial_register_number,
p_street, p_postal_code, p_city, p_country,
p_billing_street, p_billing_postal_code, p_billing_city, p_billing_country,
p_contact_name, p_contact_position, p_contact_email, p_contact_phone
)
returning id into v_client_id;
update public.profiles
set client_id = v_client_id
where id = p_profile_id
and client_id is null;
get diagnostics v_updated_rows = row_count;
if v_updated_rows = 0 then
raise exception 'profile already linked to a client' using errcode = '23505';
end if;
return v_client_id;
end;
$$;
revoke execute on function public.create_client_and_link_profile(
uuid, text, text, text, text, text, text, text, text, text, char(2),
text, text, text, char(2), text, text, text, text
) from public, anon;
grant execute on function public.create_client_and_link_profile(
uuid, text, text, text, text, text, text, text, text, text, char(2),
text, text, text, char(2), text, text, text, text
) to authenticated;