@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / eebf84b
Fork 0

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

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 18:15 UTC

chore: remove pre-deployment C8 migration filename

davoodmehraban89 committed Oct 1, 2026 at 21:10 UTC eebf84b34824406f9d8f67a9ea03e1ec936f1bc4
1 file changed -220
supabase/migrations/20261001173125_office_slice_c8_workflow_policy.sql deleted
-220
@@ -1,222 +0,0 @@
1 -
2 -create table if not exists private.office_workflow_policy_versions(
3 - organization_id uuid not null references public.organizations(id) on delete restrict,
4 - policy_key text not null,
5 - version integer not null check(version > 0),
6 - title text not null,
7 - definition jsonb not null,
8 - published_by uuid not null references public.profiles(id) on delete restrict,
9 - published_at timestamptz not null default clock_timestamp(),
10 - primary key(organization_id,policy_key,version),
11 - check(policy_key ~ '^[A-Za-z][A-Za-z0-9_-]{0,63}$'),
12 - check(char_length(btrim(title)) between 1 and 160)
13 -);
14 -alter table private.office_workflow_policy_versions enable row level security;
15 -revoke all on private.office_workflow_policy_versions from public,anon,authenticated;
16 -
17 -create or replace function private.office_workflow_policy_immutable()
18 -returns trigger
19 -language plpgsql security definer set search_path=''
20 -as $$
21 -begin
22 - raise exception 'published workflow policy is immutable' using errcode='55000';
23 -end $$;
24 -revoke all on function private.office_workflow_policy_immutable() from public,anon,authenticated;
25 -
26 -drop trigger if exists office_workflow_policy_immutable on private.office_workflow_policy_versions;
27 -create trigger office_workflow_policy_immutable
28 -before update or delete on private.office_workflow_policy_versions
29 -for each row execute function private.office_workflow_policy_immutable();
30 -
31 -create or replace function private.office_workflow_validate_definition(p_definition jsonb)
32 -returns void
33 -language plpgsql immutable security definer set search_path=''
34 -as $$
35 -declare
36 - v_stage_count integer;
37 - v_edge_count integer;
38 - v_start_id text;
39 - v_reachable integer;
40 - v_can_finish integer;
41 -begin
42 - if p_definition is null or jsonb_typeof(p_definition) <> 'object' then
43 - raise exception 'workflow definition must be an object' using errcode='22023';
44 - end if;
45 - if octet_length(p_definition::text) > 65536 then
46 - raise exception 'workflow definition exceeds 64 KiB' using errcode='22023';
47 - end if;
48 - if p_definition - array['stages','edges']::text[] <> '{}'::jsonb
49 - or not (p_definition ? 'stages') or not (p_definition ? 'edges')
50 - or jsonb_typeof(p_definition->'stages') <> 'array'
51 - or jsonb_typeof(p_definition->'edges') <> 'array' then
52 - raise exception 'workflow definition keys are invalid' using errcode='22023';
53 - end if;
54 -
55 - v_stage_count := jsonb_array_length(p_definition->'stages');
56 - v_edge_count := jsonb_array_length(p_definition->'edges');
57 - if v_stage_count < 2 or v_stage_count > 20 then
58 - raise exception 'workflow requires 2 to 20 stages' using errcode='22023';
59 - end if;
60 - if v_edge_count > 60 then
61 - raise exception 'workflow allows at most 60 edges' using errcode='22023';
62 - end if;
63 -
64 - if exists(
65 - select 1 from jsonb_array_elements(p_definition->'stages') s
66 - where jsonb_typeof(s) <> 'object'
67 - or s - array['id','label','start','terminal']::text[] <> '{}'::jsonb
68 - or not (s ?& array['id','label','start','terminal'])
69 - or jsonb_typeof(s->'id') <> 'string'
70 - or jsonb_typeof(s->'label') <> 'string'
71 - or jsonb_typeof(s->'start') <> 'boolean'
72 - or jsonb_typeof(s->'terminal') <> 'boolean'
73 - or coalesce(s->>'id','') !~ '^[A-Za-z][A-Za-z0-9_-]{0,63}$'
74 - or char_length(btrim(coalesce(s->>'label',''))) not between 1 and 160
75 - ) then raise exception 'invalid workflow stage' using errcode='22023'; end if;
76 -
77 - if (select count(*) from jsonb_array_elements(p_definition->'stages') s where (s->>'start')::boolean) <> 1 then
78 - raise exception 'workflow requires exactly one start stage' using errcode='22023';
79 - end if;
80 - if (select count(*) from jsonb_array_elements(p_definition->'stages') s where (s->>'terminal')::boolean) < 1 then
81 - raise exception 'workflow requires a terminal stage' using errcode='22023';
82 - end if;
83 - if (select count(distinct s->>'id') from jsonb_array_elements(p_definition->'stages') s) <> v_stage_count then
84 - raise exception 'workflow stage ids must be unique' using errcode='22023';
85 - end if;
86 -
87 - if exists(
88 - select 1 from jsonb_array_elements(p_definition->'edges') e
89 - where jsonb_typeof(e) <> 'object'
90 - or e - array['id','from','to','label','capability']::text[] <> '{}'::jsonb
91 - or not (e ?& array['id','from','to','label','capability'])
92 - or jsonb_typeof(e->'id') <> 'string'
93 - or jsonb_typeof(e->'from') <> 'string'
94 - or jsonb_typeof(e->'to') <> 'string'
95 - or jsonb_typeof(e->'label') <> 'string'
96 - or jsonb_typeof(e->'capability') <> 'string'
97 - or coalesce(e->>'id','') !~ '^[A-Za-z][A-Za-z0-9_-]{0,63}$'
98 - or coalesce(e->>'from','') !~ '^[A-Za-z][A-Za-z0-9_-]{0,63}$'
99 - or coalesce(e->>'to','') !~ '^[A-Za-z][A-Za-z0-9_-]{0,63}$'
100 - or char_length(btrim(coalesce(e->>'label',''))) not between 1 and 160
101 - or coalesce(e->>'capability','') not in ('edit','refer','approve')
102 - ) then raise exception 'invalid workflow edge' using errcode='22023'; end if;
103 -
104 - if (select count(distinct e->>'id') from jsonb_array_elements(p_definition->'edges') e) <> v_edge_count then
105 - raise exception 'workflow edge ids must be unique' using errcode='22023';
106 - end if;
107 - if exists(select 1 from jsonb_array_elements(p_definition->'edges') e where e->>'from'=e->>'to') then
108 - raise exception 'workflow self edge is not allowed' using errcode='22023';
109 - end if;
110 - if exists(
111 - select 1 from jsonb_array_elements(p_definition->'edges') e
112 - where not exists(select 1 from jsonb_array_elements(p_definition->'stages') s where s->>'id'=e->>'from')
113 - or not exists(select 1 from jsonb_array_elements(p_definition->'stages') s where s->>'id'=e->>'to')
114 - ) then raise exception 'workflow edge references unknown stage' using errcode='22023'; end if;
115 - if exists(
116 - select 1 from jsonb_array_elements(p_definition->'edges') a
117 - join jsonb_array_elements(p_definition->'edges') b
118 - on a->>'id' <> b->>'id' and a->>'from'=b->>'from' and a->>'to'=b->>'to'
119 - ) then raise exception 'duplicate workflow edge is not allowed' using errcode='22023'; end if;
120 - if exists(
121 - select 1 from jsonb_array_elements(p_definition->'stages') s
122 - where (s->>'terminal')::boolean
123 - and exists(select 1 from jsonb_array_elements(p_definition->'edges') e where e->>'from'=s->>'id')
124 - ) then raise exception 'terminal stage cannot have outgoing edge' using errcode='22023'; end if;
125 -
126 - select s->>'id' into v_start_id from jsonb_array_elements(p_definition->'stages') s where (s->>'start')::boolean;
127 - with recursive reachable(id) as (
128 - select v_start_id
129 - union
130 - select e->>'to' from reachable r join jsonb_array_elements(p_definition->'edges') e on e->>'from'=r.id
131 - ) select count(*) into v_reachable from reachable;
132 - if v_reachable <> v_stage_count then
133 - raise exception 'all workflow stages must be reachable from start' using errcode='22023';
134 - end if;
135 -
136 - with recursive can_finish(id) as (
137 - select s->>'id' from jsonb_array_elements(p_definition->'stages') s where (s->>'terminal')::boolean
138 - union
139 - select e->>'from' from can_finish f join jsonb_array_elements(p_definition->'edges') e on e->>'to'=f.id
140 - ) select count(distinct id) into v_can_finish from can_finish;
141 - if v_can_finish <> v_stage_count then
142 - raise exception 'every workflow stage must be able to reach a terminal stage' using errcode='22023';
143 - end if;
144 -end $$;
145 -revoke all on function private.office_workflow_validate_definition(jsonb) from public,anon,authenticated;
146 -
147 -create or replace function private.office_workflow_publish_impl(
148 - p_organization_id uuid,p_policy_key text,p_title text,p_definition jsonb
149 -) returns integer
150 -language plpgsql security definer set search_path=''
151 -as $$
152 -declare
153 - v_uid uuid := (select auth.uid());
154 - v_key text := btrim(coalesce(p_policy_key,''));
155 - v_title text := btrim(coalesce(p_title,''));
156 - v_version integer;
157 -begin
158 - if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
159 - if p_organization_id is null
160 - or p_organization_id not in (select private.current_organization_ids())
161 - or not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
162 - or not private.has_org_capability(p_organization_id,'office_automation','configure') then
163 - raise exception 'workflow configure permission required' using errcode='42501';
164 - end if;
165 - if v_key !~ '^[A-Za-z][A-Za-z0-9_-]{0,63}$' then raise exception 'invalid policy key' using errcode='22023'; end if;
166 - if char_length(v_title) not between 1 and 160 then raise exception 'invalid policy title' using errcode='22023'; end if;
167 - perform private.office_workflow_validate_definition(p_definition);
168 - perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(p_organization_id::text||':office-workflow:'||v_key,0));
169 - select coalesce(max(p.version),0)+1 into v_version
170 - from private.office_workflow_policy_versions p
171 - where p.organization_id=p_organization_id and p.policy_key=v_key;
172 - insert into private.office_workflow_policy_versions(organization_id,policy_key,version,title,definition,published_by,published_at)
173 - values(p_organization_id,v_key,v_version,v_title,p_definition,v_uid,clock_timestamp());
174 - return v_version;
175 -end $$;
176 -revoke all on function private.office_workflow_publish_impl(uuid,text,text,jsonb) from public,anon;
177 -grant usage on schema private to authenticated;
178 -grant execute on function private.office_workflow_publish_impl(uuid,text,text,jsonb) to authenticated;
179 -
180 -create or replace function public.office_workflow_publish(
181 - p_organization_id uuid,p_policy_key text,p_title text,p_definition jsonb
182 -) returns integer
183 -language sql security invoker set search_path=''
184 -as $$ select private.office_workflow_publish_impl(p_organization_id,p_policy_key,p_title,p_definition) $$;
185 -revoke all on function public.office_workflow_publish(uuid,text,text,jsonb) from public,anon;
186 -grant execute on function public.office_workflow_publish(uuid,text,text,jsonb) to authenticated;
187 -
188 -create or replace function private.office_workflow_catalog_impl(p_organization_id uuid)
189 -returns jsonb
190 -language plpgsql stable security definer set search_path=''
191 -as $$
192 -declare v_uid uuid := (select auth.uid()); v_result jsonb;
193 -begin
194 - if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
195 - if p_organization_id is null
196 - or p_organization_id not in (select private.current_organization_ids())
197 - or not private.organization_has_module_entitlement(p_organization_id,'office_automation',false)
198 - or not private.has_org_capability(p_organization_id,'office_automation','read') then
199 - raise exception 'workflow catalog permission required' using errcode='42501';
200 - end if;
201 - select coalesce(jsonb_agg(jsonb_build_object(
202 - 'policy_key',x.policy_key,'version',x.version,'title',x.title,'definition',x.definition
203 - ) order by x.policy_key),'[]'::jsonb) into v_result
204 - from (
205 - select distinct on(p.policy_key) p.policy_key,p.version,p.title,p.definition
206 - from private.office_workflow_policy_versions p
207 - where p.organization_id=p_organization_id
208 - order by p.policy_key,p.version desc
209 - ) x;
210 - return v_result;
211 -end $$;
212 -revoke all on function private.office_workflow_catalog_impl(uuid) from public,anon;
213 -grant execute on function private.office_workflow_catalog_impl(uuid) to authenticated;
214 -
215 -create or replace function public.office_workflow_catalog(p_organization_id uuid)
216 -returns jsonb
217 -language sql stable security invoker set search_path=''
218 -as $$ select private.office_workflow_catalog_impl(p_organization_id) $$;
219 -revoke all on function public.office_workflow_catalog(uuid) from public,anon;
220 -grant execute on function public.office_workflow_catalog(uuid) to authenticated;