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

Archive C5 decision ordering fix

davoodmehraban89 committed Oct 1, 2026 at 00:09 UTC c4a21dd85c909a7b35c52b2ce0ee1b07cb09a142
1 file changed +49
supabase/migrations/20260930203709_office_c5_decision_event_order_fix.sql new
+49
@@ -0,0 +1,49 @@
1 +create or replace function private.office_act_on_approval_impl(
2 + p_organization_id uuid,p_approval_id text,p_action text,p_note text
3 +) returns text language plpgsql security definer set search_path=''
4 +as $$
5 +declare
6 + v_uid uuid:=(select auth.uid()); v_req public.records%rowtype; v_source public.records%rowtype; v_member_id uuid;
7 + v_event_id text; v_event text; v_current_digest text; v_aal text:=coalesce((select auth.jwt()->>'aal'),'aal1');
8 +begin
9 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
10 + if p_action not in ('approved','rejected') then raise exception 'invalid approval action' using errcode='22023'; end if;
11 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
12 + then raise exception 'office automation entitlement required' using errcode='42501'; end if;
13 + select * into v_req from public.records r
14 + where r.organization_id=p_organization_id and r.collection='correspondenceAudit'
15 + and r.data->>'event'='approval_requested' and r.data->>'approvalId'=p_approval_id
16 + order by nullif(r.data->>'at','')::timestamptz desc limit 1;
17 + if not found then raise exception 'approval request not found' using errcode='42501'; end if;
18 + if v_req.data->>'approverUserId' is distinct from v_uid::text
19 + then raise exception 'only assigned approver may decide' using errcode='42501'; end if;
20 + begin v_member_id:=(v_req.data->>'approverMemberId')::uuid;
21 + exception when others then raise exception 'invalid approver member' using errcode='42501'; end;
22 + if not exists(select 1 from public.organization_members m
23 + where m.id=v_member_id and m.organization_id=p_organization_id and m.user_id=v_uid and m.status='active')
24 + then raise exception 'approver membership inactive' using errcode='42501'; end if;
25 + select * into v_source from public.records r
26 + where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=v_req.data->>'correspondenceId';
27 + if not found
28 + or not private.member_can_access_record(v_member_id,'office_automation',v_source.owner_id,v_source.data,'approve')
29 + or not private.member_can_access_record(v_member_id,'office_automation',v_source.owner_id,v_source.data,'read')
30 + then raise exception 'approver no longer authorized' using errcode='42501'; end if;
31 + perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(p_organization_id::text||':office-approval:'||p_approval_id,0));
32 + if exists(select 1 from public.records a
33 + where a.organization_id=p_organization_id and a.collection='correspondenceAudit'
34 + and a.data->>'approvalId'=p_approval_id and a.data->>'event' in ('approval_approved','approval_rejected'))
35 + then raise exception 'approval already decided' using errcode='22023'; end if;
36 + v_current_digest:=private.office_correspondence_digest(v_source.data);
37 + if v_current_digest is distinct from v_req.data->>'contentDigest'
38 + then raise exception 'approval content changed; create a new approval request' using errcode='40001'; end if;
39 + v_event:=case when p_action='approved' then 'approval_approved' else 'approval_rejected' end;
40 + v_event_id:='APE_'||replace(gen_random_uuid()::text,'-','');
41 + insert into public.records(organization_id,owner_id,collection,id,data)
42 + values(p_organization_id,v_uid,'correspondenceAudit',v_event_id,
43 + jsonb_build_object('event',v_event,'approvalId',p_approval_id,'correspondenceId',v_req.data->>'correspondenceId',
44 + 'actorUserId',v_uid,'contentDigest',v_current_digest,'note',coalesce(p_note,''),'aal',v_aal,'at',clock_timestamp(),
45 + 'attestationType','internal_approval','qualifiedDigitalSignature',false));
46 + return v_event_id;
47 +end $$;
48 +revoke all on function private.office_act_on_approval_impl(uuid,text,text,text) from public,anon;
49 +grant execute on function private.office_act_on_approval_impl(uuid,text,text,text) to authenticated;