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

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

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

Synced Dec 3, 18:15 UTC

Archive production migration office_c7_bind_ocr_source_attachment

davoodmehraban89 committed Oct 1, 2026 at 00:29 UTC b3c5531965533474a32fd83f543e60312d1969ad
1 file changed +51
supabase/migrations/20260930205919_office_c7_bind_ocr_source_attachment.sql new
+51
@@ -0,0 +1,51 @@
1 +
2 +create or replace function private.office_record_ocr_evidence_impl(
3 + p_organization_id uuid,p_correspondence_id text,p_source_ref text,p_provider text,p_language text,p_confidence numeric,p_text text
4 +) returns text language plpgsql security definer set search_path='' as $$
5 +declare
6 + v_uid uuid:=(select auth.uid());
7 + v_source public.records%rowtype;
8 + v_attachment public.records%rowtype;
9 + v_id text;
10 +begin
11 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
12 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
13 + or not (private.has_org_capability(p_organization_id,'office_automation','archive')
14 + or private.has_org_capability(p_organization_id,'office_automation','configure'))
15 + then raise exception 'archive permission required' using errcode='42501'; end if;
16 +
17 + select * into v_source from public.records r
18 + where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=p_correspondence_id;
19 + if not found or not private.can_access_record(p_organization_id,'correspondence',v_source.owner_id,v_source.data,'read')
20 + then raise exception 'correspondence not found or not readable' using errcode='42501'; end if;
21 +
22 + if length(trim(coalesce(p_source_ref,'')))<1 or length(p_source_ref)>500
23 + then raise exception 'invalid OCR source reference' using errcode='22023'; end if;
24 +
25 + select * into v_attachment from public.records a
26 + where a.organization_id=p_organization_id
27 + and a.collection='correspondenceAttachments'
28 + and a.id=trim(p_source_ref)
29 + and a.data->>'correspondenceId'=p_correspondence_id
30 + and nullif(trim(coalesce(a.data->>'storagePath','')),'') is not null
31 + limit 1;
32 + if not found then raise exception 'OCR source attachment not found for correspondence' using errcode='42501'; end if;
33 +
34 + if length(trim(coalesce(p_provider,'')))<1 or length(p_provider)>120 then raise exception 'invalid OCR provider' using errcode='22023'; end if;
35 + if length(trim(coalesce(p_language,'')))<1 or length(p_language)>32 then raise exception 'invalid OCR language' using errcode='22023'; end if;
36 + if p_confidence is not null and (p_confidence<0 or p_confidence>1) then raise exception 'invalid OCR confidence' using errcode='22023'; end if;
37 + if length(trim(coalesce(p_text,'')))<1 or length(p_text)>200000 then raise exception 'invalid OCR text' using errcode='22023'; end if;
38 +
39 + v_id:='OCR_'||replace(gen_random_uuid()::text,'-','');
40 + insert into public.records(organization_id,owner_id,collection,id,data)
41 + values(p_organization_id,v_uid,'correspondenceAudit',v_id,
42 + jsonb_build_object(
43 + 'event','ocr_text_captured','correspondenceId',p_correspondence_id,
44 + 'sourceRef',v_attachment.id,'sourceStoragePath',v_attachment.data->>'storagePath',
45 + 'provider',trim(p_provider),'language',trim(p_language),'confidence',p_confidence,'rawText',p_text,
46 + 'actorUserId',v_uid,'at',clock_timestamp(),'performsOcr',false,'persianAccuracyVerified',false
47 + ));
48 + return v_id;
49 +end $$;
50 +revoke all on function private.office_record_ocr_evidence_impl(uuid,text,text,text,text,numeric,text) from public,anon;
51 +grant execute on function private.office_record_ocr_evidence_impl(uuid,text,text,text,text,numeric,text) to authenticated;