سامانه صدور فاکتور و حسابداری
chore: remove duplicate Wave 1 migration
davoodmehraban89 committed
Oct 2, 2026 at 01:13 UTC
2f1d592fff18a000cfe79c4de1bafec01c7fc4f8
1 file changed
-112
supabase/migrations/20261002003000_request_legacy_transition_hardening.sql
deleted
-112
@@ -1,115 +0,0 @@
1
-
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_event public.request_instance_events%rowtype;
14
- v_action text;
15
- v_next text;
16
- v_from text;
17
-begin
18
- if auth.uid() is null then
19
- raise exception 'authentication required' using errcode='28000';
20
- end if;
21
- if char_length(trim(coalesce(p_idempotency_key,''))) < 8 then
22
- raise exception 'idempotency_key is required';
23
- end if;
24
-
25
- -- Lock and authorize the request before consulting idempotency evidence. The old
26
- -- implementation returned an event first, which could disclose another actor's result.
27
- select * into v_row
28
- from public.request_instances r
29
- where r.id=p_request_instance_id
30
- for update;
31
- if not found then raise exception 'request instance not found'; end if;
32
- if v_row.organization_id not in (select private.current_organization_ids()) then
33
- raise exception 'organization access denied' using errcode='42501';
34
- end if;
35
- if not private.organization_has_module_entitlement(v_row.organization_id,'requests_workflow',true) then
36
- raise exception 'active requests_workflow entitlement required' using errcode='42501';
37
- end if;
38
-
39
- -- D3 workflows are governed exclusively by request_step_decide. Keeping this legacy
40
- -- endpoint usable for them would let an approver skip ordered/quorum/condition controls.
41
- if exists(select 1 from private.request_step_instances s where s.request_instance_id=v_row.id) then
42
- raise exception 'D3 workflow requires request_step_decide' using errcode='55000';
43
- end if;
44
-
45
- v_action:=lower(trim(coalesce(p_action,'')));
46
- if v_action not in ('approve','reject','return','submit','cancel') then
47
- raise exception 'unsupported transition action';
48
- end if;
49
-
50
- -- Decision actions require approve. Edit alone is not an approval authority.
51
- if v_action in ('approve','reject','return') then
52
- if not private.has_org_capability(v_row.organization_id,'requests_workflow','approve') then
53
- raise exception 'requests_workflow approve capability required' using errcode='42501';
54
- end if;
55
- elsif v_action='submit' then
56
- if not (v_row.requester_user_id=auth.uid() or private.has_org_capability(v_row.organization_id,'requests_workflow','edit')) then
57
- raise exception 'requester or requests_workflow edit capability required' using errcode='42501';
58
- end if;
59
- else
60
- if not (v_row.requester_user_id=auth.uid() or private.has_org_capability(v_row.organization_id,'requests_workflow','edit')) then
61
- raise exception 'requester or requests_workflow edit capability required' using errcode='42501';
62
- end if;
63
- end if;
64
-
65
- select e.* into v_event
66
- from public.request_instance_events e
67
- where e.request_instance_id=v_row.id
68
- and e.organization_id=v_row.organization_id
69
- and e.idempotency_key=p_idempotency_key;
70
- if found then
71
- if v_event.actor_user_id<>auth.uid() or v_event.action<>v_action then
72
- raise exception 'idempotency key already used by another actor or action' using errcode='42501';
73
- end if;
74
- return query select v_row.id,v_event.to_status,v_event.revision;
75
- return;
76
- end if;
77
-
78
- if p_expected_revision is null or p_expected_revision<>v_row.revision then
79
- raise exception 'stale revision';
80
- end if;
81
- if v_row.status in ('approved','rejected','cancelled') then
82
- raise exception 'terminal request cannot transition';
83
- end if;
84
-
85
- -- Explicit legacy state machine. In particular, a returned request cannot be approved
86
- -- without being resubmitted, and draft/returned requests cannot be rejected directly.
87
- v_next:=case
88
- when v_action='approve' and v_row.status='submitted' then 'approved'
89
- when v_action='reject' and v_row.status='submitted' then 'rejected'
90
- when v_action='return' and v_row.status='submitted' then 'returned'
91
- when v_action='submit' and v_row.status in ('draft','returned') then 'submitted'
92
- when v_action='cancel' and v_row.status in ('draft','submitted','returned') then 'cancelled'
93
- else null
94
- end;
95
- if v_next is null then raise exception 'transition not allowed from current status'; end if;
96
-
97
- v_from:=v_row.status;
98
- update public.request_instances as ri
99
- set status=v_next,revision=ri.revision+1,updated_at=now()
100
- where ri.id=v_row.id
101
- returning ri.* into v_row;
102
-
103
- insert into public.request_instance_events(
104
- organization_id,request_instance_id,actor_user_id,action,from_status,to_status,revision,note,idempotency_key
105
- ) values(
106
- v_row.organization_id,v_row.id,auth.uid(),v_action,v_from,v_next,v_row.revision,p_note,p_idempotency_key
107
- );
108
- return query select v_row.id,v_next,v_row.revision;
109
-end $$;
110
-
111
-revoke all on function private.request_transition_instance(uuid,text,bigint,text,text) from public,anon;
112
-grant execute on function private.request_transition_instance(uuid,text,bigint,text,text) to authenticated;