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

test(phase1a): conservative API down/up preserves retail sample journal, immutable audit and counters

davoodmehraban89 committed Oct 16, 2026 at 16:45 UTC 32a3f34afe2c7653914a9f28541b4dd574961cab
1 file changed +79
supabase/tests/phase1a_stage1_up_down_up.sql new
+79
@@ -0,0 +1,79 @@
1 +-- Stage 1 UP / conservative DOWN / UP. Disposable Supabase CLI only.
2 +-- DOWN removes exposed APIs and view; PRESERVES accounting/audit/counter rows,
3 +-- unique indexes, and internal rollback-compatible structures to avoid data loss.
4 +\set ON_ERROR_STOP on
5 +BEGIN;
6 +INSERT INTO auth.users(id,instance_id,aud,role,email,email_confirmed_at,raw_user_meta_data)
7 +VALUES('ffff6666-6666-4666-8666-666666666666','00000000-0000-0000-0000-000000000000',
8 +'authenticated','authenticated','phase1a-rollback-real@example.invalid',now(),'{}'::jsonb);
9 +SELECT set_config('finora_rb.org',(SELECT organization_id::text FROM public.organization_members
10 +WHERE user_id='ffff6666-6666-4666-8666-666666666666' LIMIT 1),true);
11 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
12 +VALUES(current_setting('finora_rb.org')::uuid,'ffff6666-6666-4666-8666-666666666666',
13 +'companies','COMP_RB_REAL','{"name":"Retail Sample Company"}');
14 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
15 +VALUES(current_setting('finora_rb.org')::uuid,'ffff6666-6666-4666-8666-666666666666',
16 +'fiscalYears','FY_RB_REAL','{"companyId":"COMP_RB_REAL","startDate":"1405/01/01","endDate":"1405/12/29","status":"open"}');
17 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
18 +VALUES
19 +(current_setting('finora_rb.org')::uuid,'ffff6666-6666-4666-8666-666666666666','accounts','BANK_REAL',
20 +'{"companyId":"COMP_RB_REAL","code":"1101","title":"بانک جاری","postingAllowed":true,"active":true}'),
21 +(current_setting('finora_rb.org')::uuid,'ffff6666-6666-4666-8666-666666666666','accounts','SALES_REAL',
22 +'{"companyId":"COMP_RB_REAL","code":"4101","title":"درآمد فروش","postingAllowed":true,"active":true}');
23 +INSERT INTO private.finora_phase1a_guard_modes(organization_id,mode)
24 +VALUES(current_setting('finora_rb.org')::uuid,'enforce');
25 +SET LOCAL ROLE authenticated;
26 +SELECT set_config('request.jwt.claim.sub','ffff6666-6666-4666-8666-666666666666',true);
27 +SELECT public.finora_journal_create_draft(current_setting('finora_rb.org')::uuid,'RB_REAL_SALE_001',
28 + '{"companyId":"COMP_RB_REAL","fiscalYearId":"FY_RB_REAL","date":"1405/07/24","description":"فروش کالای نمونه"}',
29 + '[{"accountId":"BANK_REAL","debit":12340000,"credit":0},{"accountId":"SALES_REAL","debit":0,"credit":12340000}]');
30 +SELECT public.finora_journal_post(current_setting('finora_rb.org')::uuid,'RB_REAL_SALE_001',1);
31 +SELECT public.finora_journal_reverse(current_setting('finora_rb.org')::uuid,'RB_REAL_SALE_001',
32 +'RB_REAL_REVERSAL_001','1405/07/24','برگشت حسابداری نمونه',2,NULL);
33 +RESET ROLE;
34 +DO $snapshot$
35 +BEGIN
36 + IF (SELECT count(*) FROM public.records WHERE organization_id=current_setting('finora_rb.org')::uuid
37 + AND collection='journalVouchers' AND id LIKE 'RB_REAL_%')<>2
38 + OR (SELECT count(*) FROM private.finora_phase1a_journal_audit
39 + WHERE organization_id=current_setting('finora_rb.org')::uuid AND action IN ('posted','reversed'))<>3
40 + OR (SELECT last_no FROM private.finora_phase1a_journal_counters
41 + WHERE organization_id=current_setting('finora_rb.org')::uuid AND fiscal_year_id='FY_RB_REAL')<>2 THEN
42 + RAISE EXCEPTION 'FINORA_STAGE1_ROLLBACK_INITIAL_DATA_INVALID';
43 + END IF;
44 +END $snapshot$;
45 +-- DOWN: drop the public exposed surfaces, NOT financial data, audit, or counters.
46 +DROP VIEW public.finora_journal_reversal_status;
47 +DROP FUNCTION public.finora_journal_create_draft(uuid,text,jsonb,jsonb);
48 +DROP FUNCTION public.finora_journal_update_draft(uuid,text,bigint,jsonb,jsonb);
49 +DROP FUNCTION public.finora_journal_post(uuid,text,bigint);
50 +DROP FUNCTION public.finora_journal_delete_draft(uuid,text,bigint);
51 +DROP FUNCTION public.finora_journal_autopost(uuid,text,text,text,jsonb,jsonb);
52 +DROP FUNCTION public.finora_journal_reverse(uuid,text,text,text,text,bigint,jsonb);
53 +DO $down$
54 +BEGIN
55 + IF to_regprocedure('public.finora_journal_post(uuid,text,bigint)') IS NOT NULL
56 + OR (SELECT count(*) FROM public.records WHERE organization_id=current_setting('finora_rb.org')::uuid
57 + AND collection='journalVouchers' AND id LIKE 'RB_REAL_%')<>2
58 + OR (SELECT count(*) FROM private.finora_phase1a_journal_audit
59 + WHERE organization_id=current_setting('finora_rb.org')::uuid AND action IN ('posted','reversed'))<>3
60 + THEN RAISE EXCEPTION 'FINORA_STAGE1_ROLLBACK_DOWN_LOST_DATA_OR_API';END IF;
61 +END $down$;
62 +-- UP again: restores only SQL definitions and grants. Existing records immutable.
63 +\i /tmp/finora-phase1a-migrations/20261016102000_phase1a_journal_rpc_surface.sql
64 +SET LOCAL ROLE authenticated;
65 +SELECT set_config('request.jwt.claim.sub','ffff6666-6666-4666-8666-666666666666',true);
66 +DO $verify$
67 +DECLARE x jsonb;
68 +BEGIN
69 + x:=public.finora_journal_post(current_setting('finora_rb.org')::uuid,'RB_REAL_SALE_001',2);
70 + IF x#>>'{voucher,number}'<>'1' OR x->>'replayed'<>'true'
71 + THEN RAISE EXCEPTION 'FINORA_STAGE1_ROLLBACK_POST_REPLAY_MISMATCH';END IF;
72 + IF (SELECT effective_status FROM public.finora_journal_reversal_status
73 + WHERE organization_id=current_setting('finora_rb.org')::uuid
74 + AND voucher_id='RB_REAL_SALE_001')<>'reversed'
75 + THEN RAISE EXCEPTION 'FINORA_STAGE1_ROLLBACK_VIEW_MISMATCH';END IF;
76 + RAISE NOTICE 'FINORA_STAGE1_UP_DOWN_UP_PASS source=retail_sale amount=12340000 posted_original_immutable audit=preserved counter=preserved';
77 +END $verify$;
78 +RESET ROLE;
79 +ROLLBACK;