سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 19:30 UTC
Archive Office C3 event ordering hardening
davoodmehraban89 committed
Sep 30, 2026 at 19:18 UTC
500c519af90f1633eb2a8cb926a41db3ecf517dc
1 file changed
+45
supabase/migrations/20260930154720_office_c3_event_time_ordering.sql
new
+45
@@ -0,0 +1,45 @@
1
+create or replace function public.office_classify_correspondence(
2
+ p_organization_id uuid,
3
+ p_correspondence_id text,
4
+ p_folder text,
5
+ p_classification_code text,
6
+ p_tags text[]
7
+) returns text
8
+language plpgsql security definer set search_path=''
9
+as $$
10
+declare
11
+ v_uid uuid:=(select auth.uid());
12
+ v_source public.records%rowtype;
13
+ v_id text;
14
+ v_tags text[]:=coalesce(p_tags,array[]::text[]);
15
+begin
16
+ if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
17
+ if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
18
+ or not (
19
+ private.has_org_capability(p_organization_id,'office_automation','archive')
20
+ or private.has_org_capability(p_organization_id,'office_automation','configure')
21
+ )
22
+ then raise exception 'archive permission required' using errcode='42501'; end if;
23
+ select * into v_source from public.records r
24
+ where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=p_correspondence_id;
25
+ if not found or not private.can_access_record(p_organization_id,'correspondence',v_source.owner_id,v_source.data,'read')
26
+ then raise exception 'correspondence not found or not readable' using errcode='42501'; end if;
27
+ if length(coalesce(p_folder,''))>120 then raise exception 'archive folder too long' using errcode='22023'; end if;
28
+ if length(coalesce(p_classification_code,''))>64 then raise exception 'classification code too long' using errcode='22023'; end if;
29
+ if cardinality(v_tags)>20 or exists(select 1 from unnest(v_tags) t where length(t)>64)
30
+ then raise exception 'invalid archive tags' using errcode='22023'; end if;
31
+ v_id:='ARC_'||replace(gen_random_uuid()::text,'-','');
32
+ insert into public.records(organization_id,owner_id,collection,id,data)
33
+ values(
34
+ p_organization_id,v_uid,'correspondenceAudit',v_id,
35
+ jsonb_build_object(
36
+ 'event','archive_classified','correspondenceId',p_correspondence_id,
37
+ 'folder',trim(coalesce(p_folder,'')),
38
+ 'classificationCode',trim(coalesce(p_classification_code,'')),
39
+ 'tags',to_jsonb(v_tags),'actorUserId',v_uid,'at',clock_timestamp()
40
+ )
41
+ );
42
+ return v_id;
43
+end $$;
44
+revoke all on function public.office_classify_correspondence(uuid,text,text,text,text[]) from public,anon;
45
+grant execute on function public.office_classify_correspondence(uuid,text,text,text,text[]) to authenticated;