Treasury & Bank Transfer Management System — سامانه مدیریت خزانهداری و حوالههای بانکی
feat: add production persistence and tenant RLS contract
Davood Mehraban committed
Oct 5, 2026 at 17:18 UTC
af2aa8faf8c3211acee77bdc21000e2bff935eb5
3 files changed
+23
migrations/001_production_core.sql
new
+18
@@ -0,0 +1,18 @@
1
+BEGIN;
2
+CREATE TABLE tenants (id text PRIMARY KEY, name text NOT NULL, created_at timestamptz NOT NULL DEFAULT now());
3
+CREATE TABLE users (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), role text NOT NULL CHECK(role IN ('MAKER','CHECKER','TREASURY_ADMIN','AUDITOR')), active boolean NOT NULL DEFAULT true);
4
+CREATE TABLE bank_accounts (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), iban text NOT NULL, name text NOT NULL, UNIQUE(tenant_id, iban));
5
+CREATE TABLE beneficiaries (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), iban text NOT NULL, name text NOT NULL, whitelisted boolean NOT NULL DEFAULT false, UNIQUE(tenant_id, iban));
6
+CREATE TABLE payments (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), maker_id text NOT NULL REFERENCES users(id), beneficiary_id text NOT NULL REFERENCES beneficiaries(id), source_account_id text NOT NULL REFERENCES bank_accounts(id), amount_irr bigint NOT NULL CHECK(amount_irr>=0), payment_date date NOT NULL, client_reference text NOT NULL, idempotency_key text NOT NULL, state text NOT NULL, version integer NOT NULL DEFAULT 1, UNIQUE (tenant_id, idempotency_key));
7
+CREATE TABLE approvals (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), payment_id text NOT NULL REFERENCES payments(id), checker_id text NOT NULL REFERENCES users(id), approved_version integer NOT NULL, created_at timestamptz NOT NULL DEFAULT now());
8
+CREATE TABLE bank_statements (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), bank_account_id text NOT NULL REFERENCES bank_accounts(id), source_record_id text NOT NULL, amount_irr bigint NOT NULL, occurred_at timestamptz NOT NULL, reference text, UNIQUE(tenant_id,bank_account_id,source_record_id));
9
+CREATE TABLE audit_events (sequence bigserial PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), actor_id text, action text NOT NULL, entity_type text NOT NULL, entity_id text NOT NULL, payload jsonb NOT NULL DEFAULT '{}'::jsonb, created_at timestamptz NOT NULL DEFAULT now());
10
+ALTER TABLE users ENABLE ROW LEVEL SECURITY; ALTER TABLE bank_accounts ENABLE ROW LEVEL SECURITY; ALTER TABLE beneficiaries ENABLE ROW LEVEL SECURITY; ALTER TABLE payments ENABLE ROW LEVEL SECURITY; ALTER TABLE approvals ENABLE ROW LEVEL SECURITY; ALTER TABLE bank_statements ENABLE ROW LEVEL SECURITY; ALTER TABLE audit_events ENABLE ROW LEVEL SECURITY;
11
+CREATE POLICY users_tenant ON users USING (tenant_id = current_setting('app.tenant_id', true));
12
+CREATE POLICY bank_accounts_tenant ON bank_accounts USING (tenant_id = current_setting('app.tenant_id', true));
13
+CREATE POLICY beneficiaries_tenant ON beneficiaries USING (tenant_id = current_setting('app.tenant_id', true));
14
+CREATE POLICY payments_tenant ON payments USING (tenant_id = current_setting('app.tenant_id', true));
15
+CREATE POLICY approvals_tenant ON approvals USING (tenant_id = current_setting('app.tenant_id', true));
16
+CREATE POLICY bank_statements_tenant ON bank_statements USING (tenant_id = current_setting('app.tenant_id', true));
17
+CREATE POLICY audit_events_tenant ON audit_events USING (tenant_id = current_setting('app.tenant_id', true));
18
+COMMIT;
src/platform/persistence/schema-contract.test.ts
new
+3
@@ -0,0 +1,3 @@
1
+import {describe,expect,it} from 'vitest'; import {verifyProductionSchema} from './schema-contract';
2
+const schema=`CREATE TABLE tenants; CREATE TABLE users; CREATE TABLE bank_accounts; CREATE TABLE beneficiaries; CREATE TABLE payments; CREATE TABLE approvals; CREATE TABLE bank_statements; CREATE TABLE audit_events; ALTER TABLE payments ENABLE ROW LEVEL SECURITY; CREATE POLICY payments_tenant ON payments; UNIQUE (tenant_id, idempotency_key);`;
3
+describe('production schema contract',()=>{it('requires core tables, payment RLS and tenant idempotency uniqueness',()=>expect(verifyProductionSchema(schema)).toEqual([]));it('reports unsafe schema',()=>expect(verifyProductionSchema('CREATE TABLE payments;')).toContain('payments-rls'))});
src/platform/persistence/schema-contract.ts
new
+2
@@ -0,0 +1,2 @@
1
+const required=['tenants','users','bank_accounts','beneficiaries','payments','approvals','bank_statements','audit_events'];
2
+export function verifyProductionSchema(sql:string){const s=sql.toLowerCase().replace(/\s+/g,' ');const errors:string[]=[];for(const table of required)if(!s.includes(`table ${table}`))errors.push(`table-${table}`);if(!s.includes('alter table payments enable row level security'))errors.push('payments-rls');if(!s.includes('policy payments_tenant'))errors.push('payments-tenant-policy');if(!/unique\s*\(\s*tenant_id\s*,\s*idempotency_key\s*\)/.test(s))errors.push('tenant-idempotency-unique');return errors}