@davoodmehraban89 / Factor-easy / commits / 33ef9a7
Fork 0

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

rollback: restore legacy source immutability guard

davoodmehraban89 committed Nov 17, 2026 at 17:27 UTC 33ef9a729f54b3e61f2c10a09299be9efb524991
1 file changed +44
supabase/rollback/20261117120000_wp1_stage3_canonical_source_link_projection.sql
+44
@@ -36,3 +36,47 @@ $guard$;
36
37 REVOKE ALL ON FUNCTION private.finora_stage3_guard_posted_source()
38 FROM PUBLIC, anon, authenticated;
39 +
40 +
41 +-- Restore the original production source immutability guard exactly.
42 +create or replace function public.finora_guard_posted_source_records()
43 +returns trigger
44 +language plpgsql
45 +security invoker
46 +set search_path = public
47 +as $$
48 +declare
49 + vid text := coalesce(old.data->>'journalVoucherId','');
50 + voucher_status text := '';
51 + source_locked boolean := false;
52 +begin
53 + if old.collection not in ('invoices','purchases','payments','expenses','cheques','contractStatements') then
54 + return case when tg_op='DELETE' then old else new end;
55 + end if;
56 +
57 + if vid <> '' then
58 + select coalesce(r.data->>'status','') into voucher_status
59 + from public.records r
60 + where r.owner_id=old.owner_id
61 + and r.collection='journalVouchers'
62 + and r.id=vid;
63 + end if;
64 +
65 + source_locked :=
66 + coalesce(old.data->>'accountingStatus','')='posted'
67 + or (old.collection='cheques' and coalesce(old.data->>'status','') in ('cleared','bounced','cancelled'))
68 + or (old.collection='contractStatements' and coalesce(old.data->>'status','') in ('posted','reversed'))
69 + or voucher_status in ('posted','reversed');
70 +
71 + if not source_locked then
72 + return case when tg_op='DELETE' then old else new end;
73 + end if;
74 +
75 + if tg_op='UPDATE' and new.data=old.data then return new; end if;
76 + raise exception 'source record linked to posted/reversed accounting history is immutable';
77 +end $$;
78 +
79 +drop trigger if exists trg_finora_guard_posted_source_records on public.records;
80 +create trigger trg_finora_guard_posted_source_records
81 +before update or delete on public.records
82 +for each row execute function public.finora_guard_posted_source_records();