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

Archive production Office C5 approval migration

davoodmehraban89 committed Oct 1, 2026 at 00:06 UTC c7fa95420be6ade68af29789d165e8a02017f2e6
1 file changed +151
supabase/migrations/20260930203456_office_slice_c5_internal_approval.sql new
+151
@@ -0,0 +1,151 @@
1 +-- C5 provides internal approval/attestation evidence only; it is NOT a qualified digital signature.
2 +create or replace function private.office_request_approval_impl(
3 + p_organization_id uuid,p_correspondence_id text,p_approver_member_id uuid,p_note text,p_due_at timestamptz
4 +) returns text
5 +language plpgsql security definer set search_path=''
6 +as $$
7 +declare
8 + v_uid uuid:=(select auth.uid()); v_source public.records%rowtype; v_approver public.organization_members%rowtype;
9 + v_approval_id text; v_event_id text;
10 +begin
11 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
12 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
13 + or not (private.has_org_capability(p_organization_id,'office_automation','edit') or private.has_org_capability(p_organization_id,'office_automation','refer'))
14 + then raise exception 'approval request permission required' using errcode='42501'; end if;
15 + select * into v_source from public.records r where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=p_correspondence_id;
16 + if not found or v_source.data->>'status'='draft'
17 + or not private.can_access_record(p_organization_id,'correspondence',v_source.owner_id,v_source.data,'read')
18 + then raise exception 'registered correspondence not found or not readable' using errcode='42501'; end if;
19 + select * into v_approver from public.organization_members m where m.id=p_approver_member_id and m.organization_id=p_organization_id and m.status='active';
20 + if not found then raise exception 'approver member not found' using errcode='42501'; end if;
21 + if v_approver.user_id=v_uid then raise exception 'requester cannot approve own request' using errcode='22023'; end if;
22 + if not private.member_can_access_record(v_approver.id,'office_automation',v_source.owner_id,v_source.data,'read')
23 + or not private.member_can_access_record(v_approver.id,'office_automation',v_source.owner_id,v_source.data,'approve')
24 + then raise exception 'approver lacks approve/read access to correspondence' using errcode='42501'; end if;
25 + if exists(
26 + select 1 from public.records req
27 + where req.organization_id=p_organization_id and req.collection='correspondenceAudit'
28 + and req.data->>'event'='approval_requested' and req.data->>'correspondenceId'=p_correspondence_id
29 + and not exists(select 1 from public.records act where act.organization_id=p_organization_id and act.collection='correspondenceAudit'
30 + and act.data->>'approvalId'=req.data->>'approvalId' and act.data->>'event' in ('approval_approved','approval_rejected'))
31 + ) then raise exception 'active approval request already exists' using errcode='23505'; end if;
32 + v_approval_id:='AP_'||replace(gen_random_uuid()::text,'-',''); v_event_id:='APE_'||replace(gen_random_uuid()::text,'-','');
33 + insert into public.records(organization_id,owner_id,collection,id,data)
34 + values(p_organization_id,v_uid,'correspondenceAudit',v_event_id,
35 + jsonb_build_object('event','approval_requested','approvalId',v_approval_id,'correspondenceId',p_correspondence_id,
36 + 'requestedByUserId',v_uid,'approverMemberId',v_approver.id,'approverUserId',v_approver.user_id,
37 + 'note',coalesce(p_note,''),'dueAt',p_due_at,'aal',coalesce((select auth.jwt()->>'aal'),'aal1'),'at',clock_timestamp()));
38 + return v_approval_id;
39 +end $$;
40 +revoke all on function private.office_request_approval_impl(uuid,text,uuid,text,timestamptz) from public,anon;
41 +grant usage on schema private to authenticated;
42 +grant execute on function private.office_request_approval_impl(uuid,text,uuid,text,timestamptz) to authenticated;
43 +
44 +create or replace function public.office_request_approval(
45 + p_organization_id uuid,p_correspondence_id text,p_approver_member_id uuid,p_note text default null,p_due_at timestamptz default null
46 +) returns text language sql security invoker set search_path=''
47 +as $$ select private.office_request_approval_impl(p_organization_id,p_correspondence_id,p_approver_member_id,p_note,p_due_at) $$;
48 +revoke all on function public.office_request_approval(uuid,text,uuid,text,timestamptz) from public,anon;
49 +grant execute on function public.office_request_approval(uuid,text,uuid,text,timestamptz) to authenticated;
50 +
51 +create or replace function private.office_act_on_approval_impl(
52 + p_organization_id uuid,p_approval_id text,p_action text,p_note text
53 +) returns text
54 +language plpgsql security definer set search_path=''
55 +as $$
56 +declare
57 + v_uid uuid:=(select auth.uid()); v_req public.records%rowtype; v_source public.records%rowtype;
58 + v_member_id uuid; v_event_id text; v_event text;
59 +begin
60 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
61 + if p_action not in ('approved','rejected') then raise exception 'invalid approval action' using errcode='22023'; end if;
62 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
63 + then raise exception 'office automation entitlement required' using errcode='42501'; end if;
64 + select * into v_req from public.records r where r.organization_id=p_organization_id and r.collection='correspondenceAudit'
65 + and r.data->>'event'='approval_requested' and r.data->>'approvalId'=p_approval_id
66 + order by nullif(r.data->>'at','')::timestamptz desc limit 1;
67 + if not found then raise exception 'approval request not found' using errcode='42501'; end if;
68 + if v_req.data->>'approverUserId' is distinct from v_uid::text then raise exception 'only assigned approver may decide' using errcode='42501'; end if;
69 + if exists(select 1 from public.records a where a.organization_id=p_organization_id and a.collection='correspondenceAudit'
70 + and a.data->>'approvalId'=p_approval_id and a.data->>'event' in ('approval_approved','approval_rejected'))
71 + then raise exception 'approval request already decided' using errcode='23505'; end if;
72 + begin v_member_id:=(v_req.data->>'approverMemberId')::uuid; exception when others then raise exception 'invalid approver membership' using errcode='42501'; end;
73 + if not exists(select 1 from public.organization_members m where m.id=v_member_id and m.organization_id=p_organization_id and m.user_id=v_uid and m.status='active')
74 + then raise exception 'approver membership inactive' using errcode='42501'; end if;
75 + select * into v_source from public.records r where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=v_req.data->>'correspondenceId';
76 + if not found or not private.member_can_access_record(v_member_id,'office_automation',v_source.owner_id,v_source.data,'read')
77 + or not private.member_can_access_record(v_member_id,'office_automation',v_source.owner_id,v_source.data,'approve')
78 + then raise exception 'approver no longer has approve/read access' using errcode='42501'; end if;
79 + v_event:='approval_'||p_action; v_event_id:='APE_'||replace(gen_random_uuid()::text,'-','');
80 + insert into public.records(organization_id,owner_id,collection,id,data)
81 + values(p_organization_id,v_uid,'correspondenceAudit',v_event_id,
82 + jsonb_build_object('event',v_event,'approvalId',p_approval_id,'correspondenceId',v_req.data->>'correspondenceId',
83 + 'actorUserId',v_uid,'note',coalesce(p_note,''),'aal',coalesce((select auth.jwt()->>'aal'),'aal1'),'at',clock_timestamp()));
84 + return v_event_id;
85 +end $$;
86 +revoke all on function private.office_act_on_approval_impl(uuid,text,text,text) from public,anon;
87 +grant execute on function private.office_act_on_approval_impl(uuid,text,text,text) to authenticated;
88 +
89 +create or replace function public.office_act_on_approval(
90 + p_organization_id uuid,p_approval_id text,p_action text,p_note text default null
91 +) returns text language sql security invoker set search_path=''
92 +as $$ select private.office_act_on_approval_impl(p_organization_id,p_approval_id,p_action,p_note) $$;
93 +revoke all on function public.office_act_on_approval(uuid,text,text,text) from public,anon;
94 +grant execute on function public.office_act_on_approval(uuid,text,text,text) to authenticated;
95 +
96 +create or replace function private.office_approval_work_queue_impl(p_organization_id uuid)
97 +returns table(
98 + approval_id text,correspondence_id text,register_number text,subject text,
99 + requested_by_user_id uuid,approver_user_id uuid,note text,due_at timestamptz,
100 + requested_at timestamptz,state text,overdue boolean,decision_note text,decision_at timestamptz,decision_aal text
101 +)
102 +language sql stable security definer set search_path=''
103 +as $$
104 + with caller as (
105 + select m.id,m.user_id,m.is_owner from public.organization_members m
106 + where m.organization_id=p_organization_id and m.user_id=(select auth.uid()) and m.status='active'
107 + ), reqs as (
108 + select r.data as request_data,c.data as correspondence_data,c.owner_id as correspondence_owner
109 + from public.records r
110 + join public.records c on c.organization_id=r.organization_id and c.collection='correspondence' and c.id=r.data->>'correspondenceId'
111 + where r.organization_id=p_organization_id and r.collection='correspondenceAudit' and r.data->>'event'='approval_requested'
112 + )
113 + select
114 + r.request_data->>'approvalId',r.request_data->>'correspondenceId',
115 + r.correspondence_data->>'registerNumber',r.correspondence_data->>'subject',
116 + nullif(r.request_data->>'requestedByUserId','')::uuid,nullif(r.request_data->>'approverUserId','')::uuid,
117 + coalesce(r.request_data->>'note',''),nullif(r.request_data->>'dueAt','')::timestamptz,
118 + nullif(r.request_data->>'at','')::timestamptz,
119 + case when d.event='approval_approved' then 'approved' when d.event='approval_rejected' then 'rejected' else 'pending' end,
120 + (d.event is null and nullif(r.request_data->>'dueAt','')::timestamptz < clock_timestamp()),
121 + coalesce(d.note,''),d.at,d.aal
122 + from reqs r
123 + left join lateral (
124 + select a.data->>'event' as event,a.data->>'note' as note,nullif(a.data->>'at','')::timestamptz as at,a.data->>'aal' as aal
125 + from public.records a
126 + where a.organization_id=p_organization_id and a.collection='correspondenceAudit'
127 + and a.data->>'approvalId'=r.request_data->>'approvalId' and a.data->>'event' in ('approval_approved','approval_rejected')
128 + order by nullif(a.data->>'at','')::timestamptz desc limit 1
129 + ) d on true
130 + where exists(select 1 from caller)
131 + and private.organization_has_module_entitlement(p_organization_id,'office_automation',false)
132 + and ((select is_owner from caller limit 1)
133 + or r.request_data->>'requestedByUserId'=(select auth.uid())::text
134 + or r.request_data->>'approverUserId'=(select auth.uid())::text)
135 + and private.can_access_record(p_organization_id,'correspondence',r.correspondence_owner,r.correspondence_data,'read')
136 + order by (d.event is null and nullif(r.request_data->>'dueAt','')::timestamptz < clock_timestamp()) desc,
137 + nullif(r.request_data->>'at','')::timestamptz desc
138 +$$;
139 +revoke all on function private.office_approval_work_queue_impl(uuid) from public,anon;
140 +grant execute on function private.office_approval_work_queue_impl(uuid) to authenticated;
141 +
142 +create or replace function public.office_approval_work_queue(p_organization_id uuid)
143 +returns table(
144 + approval_id text,correspondence_id text,register_number text,subject text,
145 + requested_by_user_id uuid,approver_user_id uuid,note text,due_at timestamptz,
146 + requested_at timestamptz,state text,overdue boolean,decision_note text,decision_at timestamptz,decision_aal text
147 +)
148 +language sql stable security invoker set search_path=''
149 +as $$ select * from private.office_approval_work_queue_impl(p_organization_id) $$;
150 +revoke all on function public.office_approval_work_queue(uuid) from public,anon;
151 +grant execute on function public.office_approval_work_queue(uuid) to authenticated;