سامانه صدور فاکتور و حسابداری
Harden Office C1 registered history and read evidence
davoodmehraban89 committed
Sep 30, 2026 at 09:12 UTC
20790d5998ef06105d5715864fb4ce2453dc470e
3 files changed
+90
.github/workflows/quality.yml
+2
@@ -50,6 +50,8 @@ jobs:
50
run: node scripts/enterprise-invitation-check.mjs
51
- name: Enterprise organization UX invariants
52
run: node scripts/enterprise-org-ux-check.mjs
53
+ - name: Office Slice C1 invariants
54
+ run: node scripts/office-slice-c1-check.mjs
55
- name: Install browser smoke-test runtime
56
run: |
57
npm install --no-save @playwright/test@1.55.0 pdf-lib@1.17.1
scripts/office-slice-c1-check.mjs
new
+15
@@ -0,0 +1,15 @@
1
+import fs from 'node:fs';
2
+const need=(t,r,l)=>{if(!r.test(t))throw new Error('Office C1 invariant missing: '+l)};
3
+const m=fs.readFileSync('supabase/migrations/20260930062000_office_registered_history_read_evidence.sql','utf8');
4
+need(m,/protect_registered_correspondence_history/i,'registered history guard');
5
+need(m,/registered correspondence cannot be deleted/i,'registered delete denial');
6
+need(m,/registryId[\s\S]*registerNumber[\s\S]*registeredAt/i,'registration identity protection');
7
+need(m,/status cannot move backward/i,'status monotonicity');
8
+need(m,/office_correspondence_audit_capture/i,'correspondence mutation audit');
9
+need(m,/office_mark_correspondence_read/i,'read evidence RPC');
10
+need(m,/readerUserId',v_uid/i,'authenticated reader identity');
11
+need(m,/private\.can_access_record[\s\S]*'read'/i,'record-level read authorization');
12
+need(m,/correspondence_audit_immutable/i,'read evidence immutability');
13
+const q=fs.readFileSync('.github/workflows/quality.yml','utf8');
14
+need(q,/Office Slice C1 invariants[\s\S]*office-slice-c1-check\.mjs/i,'quality workflow');
15
+console.log('Office Slice C1 server contract: PASS');
supabase/migrations/20260930062000_office_registered_history_read_evidence.sql
new
+73
@@ -0,0 +1,73 @@
1
+-- Office Slice C1: protect registered history and add server-authoritative read evidence.
2
+
3
+create or replace function private.protect_registered_correspondence_history()
4
+returns trigger
5
+language plpgsql set search_path=''
6
+as $$
7
+declare old_rank int; new_rank int;
8
+begin
9
+ if old.collection<>'correspondence' or coalesce(old.data->>'status','draft')='draft' then
10
+ if tg_op='DELETE' then return old; else return new; end if;
11
+ end if;
12
+ if tg_op='DELETE' then raise exception 'registered correspondence cannot be deleted' using errcode='42501'; end if;
13
+ if new.organization_id is distinct from old.organization_id
14
+ or new.owner_id is distinct from old.owner_id
15
+ or new.collection is distinct from old.collection
16
+ or new.id is distinct from old.id
17
+ or new.data->>'registryId' is distinct from old.data->>'registryId'
18
+ or new.data->>'registerNumber' is distinct from old.data->>'registerNumber'
19
+ or new.data->>'registeredAt' is distinct from old.data->>'registeredAt' then
20
+ raise exception 'registered correspondence identity is immutable' using errcode='42501';
21
+ end if;
22
+ old_rank:=case old.data->>'status' when 'registered' then 1 when 'submitted' then 2 when 'closed' then 3 else 1 end;
23
+ new_rank:=case new.data->>'status' when 'registered' then 1 when 'submitted' then 2 when 'closed' then 3 else 0 end;
24
+ if new_rank<old_rank then raise exception 'registered correspondence status cannot move backward' using errcode='42501'; end if;
25
+ return new;
26
+end $$;
27
+revoke all on function private.protect_registered_correspondence_history() from public,anon,authenticated;
28
+drop trigger if exists trg_protect_registered_correspondence_history on public.records;
29
+create trigger trg_protect_registered_correspondence_history
30
+before update or delete on public.records
31
+for each row when (old.collection='correspondence') execute function private.protect_registered_correspondence_history();
32
+
33
+create or replace function private.office_correspondence_audit_capture()
34
+returns trigger
35
+language plpgsql security definer set search_path=''
36
+as $$
37
+begin
38
+ insert into public.organization_audit(organization_id,actor_user_id,action,entity_type,entity_id,before_data,after_data)
39
+ values(new.organization_id,(select auth.uid()),case when tg_op='INSERT' then 'correspondence_registered' else 'correspondence_update' end,'correspondence',new.id,
40
+ case when tg_op='UPDATE' then to_jsonb(old) else null end,to_jsonb(new));
41
+ return new;
42
+end $$;
43
+revoke all on function private.office_correspondence_audit_capture() from public,anon,authenticated;
44
+drop trigger if exists correspondence_registered_audit on public.records;
45
+create trigger correspondence_registered_audit after insert or update on public.records
46
+for each row when (new.collection='correspondence' and coalesce(new.data->>'status','draft')<>'draft')
47
+execute function private.office_correspondence_audit_capture();
48
+
49
+create or replace function private.office_audit_immutable()
50
+returns trigger language plpgsql set search_path=''
51
+as $$ begin raise exception 'office audit evidence is immutable' using errcode='42501'; end $$;
52
+revoke all on function private.office_audit_immutable() from public,anon,authenticated;
53
+drop trigger if exists correspondence_audit_immutable on public.records;
54
+create trigger correspondence_audit_immutable before update or delete on public.records
55
+for each row when (old.collection='correspondenceAudit') execute function private.office_audit_immutable();
56
+
57
+create or replace function public.office_mark_correspondence_read(p_organization_id uuid,p_correspondence_id text)
58
+returns text
59
+language plpgsql security definer set search_path=''
60
+as $$
61
+declare v_source public.records%rowtype; v_id text; v_uid uuid:=(select auth.uid());
62
+begin
63
+ if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
64
+ if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true) then raise exception 'office automation entitlement required' using errcode='42501'; end if;
65
+ 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;
66
+ if not found or not private.can_access_record(p_organization_id,'correspondence',v_source.owner_id,v_source.data,'read') then raise exception 'correspondence not found or not readable' using errcode='42501'; end if;
67
+ v_id:='READ_'||replace(gen_random_uuid()::text,'-','');
68
+ insert into public.records(organization_id,owner_id,collection,id,data)
69
+ values(p_organization_id,v_uid,'correspondenceAudit',v_id,jsonb_build_object('event','read','correspondenceId',p_correspondence_id,'readerUserId',v_uid,'readAt',now()));
70
+ return v_id;
71
+end $$;
72
+revoke all on function public.office_mark_correspondence_read(uuid,text) from public,anon;
73
+grant execute on function public.office_mark_correspondence_read(uuid,text) to authenticated;