سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 18:15 UTC
fix: qualify D1 transition revision update
davoodmehraban89 committed
Oct 1, 2026 at 22:22 UTC
a93ca82ebef4ef942143ce0190783221a05a552a
1 file changed
+37
supabase/migrations/20261001211500_request_workflow_d1_revision_qualification.sql
new
+37
@@ -0,0 +1,37 @@
1
+-- D1 behavioral test found PL/pgSQL output-column ambiguity on revision.
2
+create or replace function private.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 where e.request_instance_id=p_request_instance_id and e.idempotency_key=p_idempotency_key;
19
+ if found then return query select p_request_instance_id,v_event.to_status,v_event.revision; return; end if;
20
+ select * into v_row from public.request_instances r where r.id=p_request_instance_id for update;
21
+ if not found then raise exception 'request instance not found'; end if;
22
+ if v_row.organization_id not in (select private.current_organization_ids()) then raise exception 'organization access denied' using errcode='42501'; end if;
23
+ 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;
24
+ 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;
25
+ if p_expected_revision is null or p_expected_revision<>v_row.revision then raise exception 'stale revision'; end if;
26
+ if char_length(trim(coalesce(p_idempotency_key,'')))<8 then raise exception 'idempotency_key is required'; end if;
27
+ v_from:=v_row.status;
28
+ 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;
29
+ if v_next is null then raise exception 'unsupported transition action'; end if;
30
+ if v_from in ('approved','rejected','cancelled') then raise exception 'terminal request cannot transition'; end if;
31
+ update public.request_instances as ri set status=v_next,revision=ri.revision+1,updated_at=now() where ri.id=v_row.id returning ri.* into v_row;
32
+ insert into public.request_instance_events(organization_id,request_instance_id,actor_user_id,action,from_status,to_status,revision,note,idempotency_key)
33
+ 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);
34
+ return query select v_row.id,v_next,v_row.revision;
35
+end $$;
36
+revoke all on function private.request_transition_instance(uuid,text,bigint,text,text) from public,anon;
37
+grant execute on function private.request_transition_instance(uuid,text,bigint,text,text) to authenticated;