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

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

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

Synced Dec 3, 19:30 UTC

feat: implement C8 workflow policy database slice

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