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

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

fix: allow canonical immutable source link projection

davoodmehraban89 committed Nov 17, 2026 at 17:14 UTC debd75bd31d834e09c5daf9bf37dee3b14082c96
1 file changed +79
supabase/migrations/20261117120000_stage3_canonical_source_link_projection.sql new
+79
@@ -0,0 +1,79 @@
1 +-- Permit only server-verifiable accounting-link projection updates after source posting.
2 +-- Business payload remains immutable; metadata must match the real posted journal and reversal view.
3 +CREATE OR REPLACE FUNCTION private.finora_stage3_guard_posted_source()
4 +RETURNS trigger
5 +LANGUAGE plpgsql
6 +SECURITY DEFINER
7 +SET search_path = ''
8 +AS $guard$
9 +DECLARE
10 + v_kind text;
11 + v_voucher public.records%rowtype;
12 + v_effective_status text;
13 + v_projection_keys text[] := ARRAY[
14 + 'journalVoucherId','accountingStatus','accountingPostedAt',
15 + 'documentStatus','documentPostedAt','documentPostedBy',
16 + 'documentVersion','updatedAt',
17 + 'accountingPostingPending','accountingPostingPendingAt'
18 + ]::text[];
19 +BEGIN
20 + IF TG_OP NOT IN ('UPDATE','DELETE') THEN
21 + RETURN NEW;
22 + END IF;
23 + IF OLD.collection NOT IN ('invoices','purchases') THEN
24 + RETURN CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
25 + END IF;
26 + IF auth.uid() IS NULL
27 + OR private.finora_phase1a_mode(OLD.organization_id) <> 'enforce' THEN
28 + RETURN CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
29 + END IF;
30 +
31 + v_kind := CASE OLD.collection WHEN 'invoices' THEN 'sale' ELSE 'purchase' END;
32 + SELECT v.* INTO v_voucher
33 + FROM public.records v
34 + WHERE v.organization_id = OLD.organization_id
35 + AND v.collection = 'journalVouchers'
36 + AND v.data->>'sourceType' = v_kind
37 + AND v.data->>'sourceId' = OLD.id
38 + AND v.data->>'status' IN ('posted','reversed')
39 + ORDER BY v.data->>'postedAt' DESC NULLS LAST, v.id
40 + LIMIT 1;
41 +
42 + IF FOUND THEN
43 + -- A posted source's commercial/accounting inputs are immutable. The only
44 + -- permitted update is the canonical linkage/status projection below.
45 + IF (OLD.data - v_projection_keys) IS DISTINCT FROM (NEW.data - v_projection_keys) THEN
46 + RAISE EXCEPTION 'FINORA_POSTED_SOURCE_IMMUTABLE'
47 + USING ERRCODE = '23514';
48 + END IF;
49 +
50 + SELECT s.effective_status INTO v_effective_status
51 + FROM public.finora_journal_reversal_status s
52 + WHERE s.organization_id = OLD.organization_id
53 + AND s.voucher_id = v_voucher.id;
54 + v_effective_status := coalesce(v_effective_status, v_voucher.data->>'status');
55 +
56 + IF NEW.data->>'journalVoucherId' IS DISTINCT FROM v_voucher.id
57 + OR NEW.data->>'accountingStatus' IS DISTINCT FROM
58 + CASE WHEN v_effective_status = 'reversed' THEN 'reversed' ELSE 'posted' END
59 + OR NEW.data->>'documentStatus' IS DISTINCT FROM
60 + CASE WHEN v_effective_status = 'reversed' THEN 'reversed' ELSE 'posted' END
61 + OR NEW.data->>'accountingPostedAt' IS DISTINCT FROM v_voucher.data->>'postedAt'
62 + OR NEW.data->>'documentPostedAt' IS DISTINCT FROM v_voucher.data->>'postedAt'
63 + OR NEW.data->>'documentPostedBy' IS DISTINCT FROM
64 + coalesce(v_voucher.data->>'postedBy', OLD.data->>'documentPostedBy', auth.uid()::text)
65 + OR coalesce(NEW.data->>'documentVersion','1') IS DISTINCT FROM
66 + coalesce(OLD.data->>'documentVersion','1')
67 + OR NEW.data ? 'accountingPostingPending'
68 + OR NEW.data ? 'accountingPostingPendingAt' THEN
69 + RAISE EXCEPTION 'FINORA_POSTED_SOURCE_LINK_PROJECTION_INVALID'
70 + USING ERRCODE = '23514';
71 + END IF;
72 + END IF;
73 +
74 + RETURN CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
75 +END
76 +$guard$;
77 +
78 +REVOKE ALL ON FUNCTION private.finora_stage3_guard_posted_source()
79 +FROM PUBLIC, anon, authenticated;