@davoodmehraban89 / Factor-easy / commits / d5df58c
Fork 0

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

fix: preserve request workflow transition evidence

davoodmehraban89 committed Oct 1, 2026 at 22:14 UTC d5df58cd425939b19655e12df3b2547e3e3f40fe
1 file changed +41
supabase/migrations/20261001205500_request_workflow_d1_transition_evidence.sql new
+41
@@ -0,0 +1,41 @@
1 +-- Slice D1 hardening: preserve the true pre-transition state in immutable evidence.
2 +create or replace function public.request_transition_instance(
3 + p_request_instance_id uuid,
4 + p_action text,
5 + p_expected_revision bigint,
6 + p_idempotency_key text,
7 + p_note text default null
8 +) returns table(request_instance_id uuid, status text, revision bigint)
9 +language plpgsql security definer set search_path=''
10 +as $$
11 +declare
12 + v_row public.request_instances%rowtype;
13 + v_next text;
14 + v_from text;
15 + v_event public.request_instance_events%rowtype;
16 +begin
17 + if auth.uid() is null then raise exception 'authentication required' using errcode='28000'; end if;
18 + select e.* into v_event from public.request_instance_events e
19 + where e.request_instance_id=p_request_instance_id and e.idempotency_key=p_idempotency_key;
20 + if found then return query select p_request_instance_id,v_event.to_status,v_event.revision; return; end if;
21 +
22 + select * into v_row from public.request_instances r where r.id=p_request_instance_id for update;
23 + if not found then raise exception 'request instance not found'; end if;
24 + if v_row.organization_id not in (select private.current_organization_ids()) then raise exception 'organization access denied' using errcode='42501'; end if;
25 + if not private.organization_has_module_entitlement(v_row.organization_id,'requests_workflow',true) then raise exception 'active requests_workflow entitlement required' using errcode='42501'; end if;
26 + if not (private.has_org_capability(v_row.organization_id,'requests_workflow','approve') or private.has_org_capability(v_row.organization_id,'requests_workflow','edit')) then raise exception 'requests_workflow approve or edit capability required' using errcode='42501'; end if;
27 + if p_expected_revision is null or p_expected_revision <> v_row.revision then raise exception 'stale revision'; end if;
28 + if char_length(trim(coalesce(p_idempotency_key,''))) < 8 then raise exception 'idempotency_key is required'; end if;
29 +
30 + v_from := v_row.status;
31 + v_next := case lower(trim(p_action)) when 'approve' then 'approved' when 'reject' then 'rejected' when 'return' then 'returned' when 'submit' then 'submitted' when 'cancel' then 'cancelled' else null end;
32 + if v_next is null then raise exception 'unsupported transition action'; end if;
33 + if v_from in ('approved','rejected','cancelled') then raise exception 'terminal request cannot transition'; end if;
34 +
35 + update public.request_instances set status=v_next,revision=revision+1,updated_at=now() where id=v_row.id returning * into v_row;
36 + insert into public.request_instance_events(organization_id,request_instance_id,actor_user_id,action,from_status,to_status,revision,note,idempotency_key)
37 + values(v_row.organization_id,v_row.id,auth.uid(),lower(trim(p_action)),v_from,v_next,v_row.revision,p_note,p_idempotency_key);
38 + return query select v_row.id,v_next,v_row.revision;
39 +end $$;
40 +revoke all on function public.request_transition_instance(uuid,text,bigint,text,text) from public,anon;
41 +grant execute on function public.request_transition_instance(uuid,text,bigint,text,text) to authenticated;