@davoodmehraban89 / Factor-easy / commits / e5c16b0
Fork 0

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

Archive production Office C3 related archive migration

davoodmehraban89 committed Sep 30, 2026 at 19:18 UTC e5c16b07a4f280ec7d7735b375c0ac23062e2b71
1 file changed +107
supabase/migrations/20260930154625_office_slice_c3_related_archive.sql new
+107
@@ -0,0 +1,107 @@
1 +create or replace function public.office_link_correspondence(
2 + p_organization_id uuid,
3 + p_source_id text,
4 + p_target_id text,
5 + p_relation_type text
6 +) returns text
7 +language plpgsql security definer set search_path=''
8 +as $$
9 +declare
10 + v_uid uuid:=(select auth.uid());
11 + v_source public.records%rowtype;
12 + v_target public.records%rowtype;
13 + v_id text;
14 + v_cycle boolean;
15 +begin
16 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
17 + if p_relation_type not in ('reply_to','related') then raise exception 'invalid relation type' using errcode='22023'; end if;
18 + if p_source_id=p_target_id then raise exception 'self relation is not allowed' using errcode='22023'; end if;
19 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
20 + or not (private.has_org_capability(p_organization_id,'office_automation','edit') or private.has_org_capability(p_organization_id,'office_automation','refer'))
21 + then raise exception 'forbidden' using errcode='42501'; end if;
22 + select * into v_source from public.records r where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=p_source_id;
23 + if not found or not private.can_access_record(p_organization_id,'correspondence',v_source.owner_id,v_source.data,'read')
24 + then raise exception 'source correspondence not found or not readable' using errcode='42501'; end if;
25 + select * into v_target from public.records r where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=p_target_id;
26 + if not found or not private.can_access_record(p_organization_id,'correspondence',v_target.owner_id,v_target.data,'read')
27 + then raise exception 'target correspondence not found or not readable' using errcode='42501'; end if;
28 + if p_relation_type='reply_to' then
29 + if exists(select 1 from public.records a where a.organization_id=p_organization_id and a.collection='correspondenceAudit' and a.data->>'event'='relation_created' and a.data->>'relationType'='reply_to' and a.data->>'sourceId'=p_source_id)
30 + then raise exception 'reply parent already exists' using errcode='23505'; end if;
31 + with recursive edges as (
32 + select a.data->>'sourceId' as source_id,a.data->>'targetId' as target_id
33 + from public.records a where a.organization_id=p_organization_id and a.collection='correspondenceAudit' and a.data->>'event'='relation_created' and a.data->>'relationType'='reply_to'
34 + ), walk(node) as (
35 + select p_target_id union select e.target_id from edges e join walk w on e.source_id=w.node
36 + )
37 + select exists(select 1 from walk where node=p_source_id) into v_cycle;
38 + if v_cycle then raise exception 'reply cycle is not allowed' using errcode='22023'; end if;
39 + else
40 + if exists(select 1 from public.records a where a.organization_id=p_organization_id and a.collection='correspondenceAudit' and a.data->>'event'='relation_created' and a.data->>'relationType'='related' and ((a.data->>'sourceId'=p_source_id and a.data->>'targetId'=p_target_id) or (a.data->>'sourceId'=p_target_id and a.data->>'targetId'=p_source_id)))
41 + then raise exception 'related correspondence link already exists' using errcode='23505'; end if;
42 + end if;
43 + v_id:='REL_'||replace(gen_random_uuid()::text,'-','');
44 + insert into public.records(organization_id,owner_id,collection,id,data)
45 + values(p_organization_id,v_uid,'correspondenceAudit',v_id,jsonb_build_object('event','relation_created','relationType',p_relation_type,'sourceId',p_source_id,'targetId',p_target_id,'actorUserId',v_uid,'at',now()));
46 + return v_id;
47 +end $$;
48 +revoke all on function public.office_link_correspondence(uuid,text,text,text) from public,anon;
49 +grant execute on function public.office_link_correspondence(uuid,text,text,text) to authenticated;
50 +
51 +create or replace function public.office_classify_correspondence(
52 + p_organization_id uuid,p_correspondence_id text,p_folder text,p_classification_code text,p_tags text[]
53 +) returns text
54 +language plpgsql security definer set search_path=''
55 +as $$
56 +declare
57 + v_uid uuid:=(select auth.uid());
58 + v_source public.records%rowtype;
59 + v_id text;
60 + v_tags text[]:=coalesce(p_tags,array[]::text[]);
61 +begin
62 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
63 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
64 + or not (private.has_org_capability(p_organization_id,'office_automation','archive') or private.has_org_capability(p_organization_id,'office_automation','configure'))
65 + then raise exception 'archive permission required' using errcode='42501'; end if;
66 + select * into v_source from public.records r where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=p_correspondence_id;
67 + if not found or not private.can_access_record(p_organization_id,'correspondence',v_source.owner_id,v_source.data,'read')
68 + then raise exception 'correspondence not found or not readable' using errcode='42501'; end if;
69 + if length(coalesce(p_folder,''))>120 then raise exception 'archive folder too long' using errcode='22023'; end if;
70 + if length(coalesce(p_classification_code,''))>64 then raise exception 'classification code too long' using errcode='22023'; end if;
71 + if cardinality(v_tags)>20 or exists(select 1 from unnest(v_tags) t where length(t)>64)
72 + then raise exception 'invalid archive tags' using errcode='22023'; end if;
73 + v_id:='ARC_'||replace(gen_random_uuid()::text,'-','');
74 + insert into public.records(organization_id,owner_id,collection,id,data)
75 + values(p_organization_id,v_uid,'correspondenceAudit',v_id,jsonb_build_object('event','archive_classified','correspondenceId',p_correspondence_id,'folder',trim(coalesce(p_folder,'')),'classificationCode',trim(coalesce(p_classification_code,'')),'tags',to_jsonb(v_tags),'actorUserId',v_uid,'at',now()));
76 + return v_id;
77 +end $$;
78 +revoke all on function public.office_classify_correspondence(uuid,text,text,text,text[]) from public,anon;
79 +grant execute on function public.office_classify_correspondence(uuid,text,text,text,text[]) to authenticated;
80 +
81 +create or replace function public.office_archive_search(
82 + p_organization_id uuid,p_query text default null,p_folder text default null,p_classification_code text default null,p_related_to text default null
83 +) returns table(
84 + correspondence_id text,register_number text,subject text,counterparty text,status text,folder text,classification_code text,tags text[],related_count bigint
85 +)
86 +language sql stable security definer set search_path=''
87 +as $$
88 + select c.id,c.data->>'registerNumber',c.data->>'subject',c.data->>'counterparty',c.data->>'status',
89 + coalesce(cls.data->>'folder',''),coalesce(cls.data->>'classificationCode',''),
90 + coalesce(array(select jsonb_array_elements_text(coalesce(cls.data->'tags','[]'::jsonb))),array[]::text[]),
91 + (select count(*) from public.records rel where rel.organization_id=p_organization_id and rel.collection='correspondenceAudit' and rel.data->>'event'='relation_created' and (rel.data->>'sourceId'=c.id or rel.data->>'targetId'=c.id))
92 + from public.records c
93 + left join lateral (
94 + select a.data from public.records a
95 + where a.organization_id=p_organization_id and a.collection='correspondenceAudit' and a.data->>'event'='archive_classified' and a.data->>'correspondenceId'=c.id
96 + order by nullif(a.data->>'at','')::timestamptz desc limit 1
97 + ) cls on true
98 + where c.organization_id=p_organization_id and c.collection='correspondence'
99 + and private.can_access_record(p_organization_id,'correspondence',c.owner_id,c.data,'read')
100 + and (nullif(trim(coalesce(p_query,'')),'') is null or lower(concat_ws(' ',c.data->>'registerNumber',c.data->>'subject',c.data->>'counterparty',c.data->>'externalNumber',c.data->>'body',coalesce(cls.data->>'folder',''),coalesce(cls.data->>'classificationCode',''),coalesce((cls.data->'tags')::text,''))) like '%'||lower(trim(p_query))||'%')
101 + and (nullif(trim(coalesce(p_folder,'')),'') is null or cls.data->>'folder'=trim(p_folder))
102 + and (nullif(trim(coalesce(p_classification_code,'')),'') is null or cls.data->>'classificationCode'=trim(p_classification_code))
103 + and (nullif(trim(coalesce(p_related_to,'')),'') is null or c.id=p_related_to or exists(select 1 from public.records rel where rel.organization_id=p_organization_id and rel.collection='correspondenceAudit' and rel.data->>'event'='relation_created' and ((rel.data->>'sourceId'=p_related_to and rel.data->>'targetId'=c.id) or (rel.data->>'targetId'=p_related_to and rel.data->>'sourceId'=c.id))))
104 + order by coalesce(c.data->>'registeredAt',c.data->>'createdAt','') desc
105 +$$;
106 +revoke all on function public.office_archive_search(uuid,text,text,text,text) from public,anon;
107 +grant execute on function public.office_archive_search(uuid,text,text,text,text) to authenticated;