سامانه صدور فاکتور و حسابداری

Archive tenant-aware office registration repair

davoodmehraban89 committed Sep 30, 2026 at 23:56 UTC ce0414d1a57681f1539f5e4d919f0c460fed9f5d
1 file changed +72
supabase/migrations/20260930202526_office_registration_tenant_repair.sql new
+72
@@ -0,0 +1,72 @@
1 +drop function if exists public.office_register_correspondence(text,text,jsonb);
2 +
3 +create or replace function public.office_register_correspondence(
4 + p_id text,
5 + p_registry_id text,
6 + p_payload jsonb,
7 + p_organization_id uuid default null
8 +) returns text
9 +language plpgsql
10 +security invoker
11 +set search_path=''
12 +as $$
13 +declare
14 + v_org uuid:=coalesce(p_organization_id,private.default_organization_id());
15 + v_uid uuid:=(select auth.uid());
16 + registry_row public.records%rowtype;
17 + next_no integer;
18 + prefix text;
19 + assigned text;
20 + v_template_key text:=nullif(trim(coalesce(p_payload->>'templateKey','')),'');
21 + v_template_version integer;
22 +begin
23 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
24 + if v_org is null
25 + or not private.organization_has_module_entitlement(v_org,'office_automation',true)
26 + or not private.has_org_capability(v_org,'office_automation','register')
27 + then raise exception 'forbidden' using errcode='42501'; end if;
28 + if p_id is null or p_id !~ '^[A-Za-z0-9_-]{1,64}$' then raise exception 'invalid correspondence id' using errcode='22023'; end if;
29 + if p_registry_id is null or p_registry_id !~ '^[A-Za-z0-9_-]{1,64}$' then raise exception 'invalid registry id' using errcode='22023'; end if;
30 + if p_payload is null or jsonb_typeof(p_payload)<>'object' then raise exception 'invalid payload' using errcode='22023'; end if;
31 +
32 + if v_template_key is not null or p_payload ? 'templateVersion' then
33 + if v_template_key is null or coalesce(p_payload->>'templateVersion','') !~ '^[1-9][0-9]*$'
34 + then raise exception 'invalid template provenance' using errcode='22023'; end if;
35 + v_template_version:=(p_payload->>'templateVersion')::integer;
36 + if not exists(
37 + select 1 from public.records t
38 + where t.organization_id=v_org
39 + and t.collection='correspondenceTemplates'
40 + and t.data->>'templateKey'=v_template_key
41 + and (t.data->>'version')::integer=v_template_version
42 + ) then raise exception 'template version not found in organization' using errcode='42501'; end if;
43 + end if;
44 +
45 + select * into registry_row
46 + from public.records r
47 + where r.organization_id=v_org and r.collection='officeRegistries' and r.id=p_registry_id
48 + for update;
49 + if not found then raise exception 'registry not found' using errcode='42501'; end if;
50 +
51 + next_no:=greatest(1,coalesce(nullif(registry_row.data->>'nextNumber','')::integer,1));
52 + prefix:=trim(coalesce(registry_row.data->>'prefix',''));
53 + assigned:=case when prefix='' then next_no::text else prefix||'-'||next_no::text end;
54 +
55 + update public.records
56 + set data=jsonb_set(registry_row.data,'{nextNumber}',to_jsonb(next_no+1),true)
57 + where organization_id=v_org and collection='officeRegistries' and id=p_registry_id;
58 +
59 + insert into public.records(organization_id,owner_id,collection,id,data)
60 + values(v_org,v_uid,'correspondence',p_id,
61 + p_payload || jsonb_build_object(
62 + 'registryId',p_registry_id,'registerNumber',assigned,'status','registered','registeredAt',clock_timestamp()
63 + ))
64 + on conflict(organization_id,collection,id) do update
65 + set owner_id=excluded.owner_id,
66 + data=case when public.records.data->>'status'='registered' then public.records.data else excluded.data end;
67 +
68 + return assigned;
69 +end $$;
70 +
71 +revoke all on function public.office_register_correspondence(text,text,jsonb,uuid) from public,anon;
72 +grant execute on function public.office_register_correspondence(text,text,jsonb,uuid) to authenticated;