Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

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 NOT NULL, 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, PRIMARY KEY(id), UNIQUE(tenant_id,id));
4 CREATE TABLE bank_accounts (id text NOT NULL, tenant_id text NOT NULL REFERENCES tenants(id), iban text NOT NULL, name text NOT NULL, PRIMARY KEY(id), UNIQUE(tenant_id,id), UNIQUE(tenant_id,iban));
5 CREATE TABLE beneficiaries (id text NOT NULL, tenant_id text NOT NULL REFERENCES tenants(id), iban text NOT NULL, name text NOT NULL, whitelisted boolean NOT NULL DEFAULT false, PRIMARY KEY(id), UNIQUE(tenant_id,id), UNIQUE(tenant_id,iban));
6 CREATE TABLE payments (id text NOT NULL, tenant_id text NOT NULL REFERENCES tenants(id), maker_id text NOT NULL, beneficiary_id text NOT NULL, source_account_id text NOT NULL, 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 CHECK(version>0), PRIMARY KEY(id), UNIQUE(tenant_id,id), UNIQUE (tenant_id, idempotency_key), FOREIGN KEY (tenant_id, maker_id) REFERENCES users(tenant_id,id), FOREIGN KEY (tenant_id, beneficiary_id) REFERENCES beneficiaries(tenant_id,id), FOREIGN KEY (tenant_id, source_account_id) REFERENCES bank_accounts(tenant_id,id));
7 CREATE TABLE approvals (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), payment_id text NOT NULL, checker_id text NOT NULL, approved_version integer NOT NULL CHECK(approved_version>0), created_at timestamptz NOT NULL DEFAULT now(), FOREIGN KEY(tenant_id,payment_id) REFERENCES payments(tenant_id,id), FOREIGN KEY(tenant_id,checker_id) REFERENCES users(tenant_id,id));
8 CREATE TABLE bank_statements (id text PRIMARY KEY, tenant_id text NOT NULL REFERENCES tenants(id), bank_account_id text NOT NULL, 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), FOREIGN KEY(tenant_id,bank_account_id) REFERENCES bank_accounts(tenant_id,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(), FOREIGN KEY(tenant_id,actor_id) REFERENCES users(tenant_id,id));
10 ALTER TABLE users ENABLE ROW LEVEL SECURITY; ALTER TABLE users FORCE ROW LEVEL SECURITY;
11 ALTER TABLE bank_accounts ENABLE ROW LEVEL SECURITY; ALTER TABLE bank_accounts FORCE ROW LEVEL SECURITY;
12 ALTER TABLE beneficiaries ENABLE ROW LEVEL SECURITY; ALTER TABLE beneficiaries FORCE ROW LEVEL SECURITY;
13 ALTER TABLE payments ENABLE ROW LEVEL SECURITY; ALTER TABLE payments FORCE ROW LEVEL SECURITY;
14 ALTER TABLE approvals ENABLE ROW LEVEL SECURITY; ALTER TABLE approvals FORCE ROW LEVEL SECURITY;
15 ALTER TABLE bank_statements ENABLE ROW LEVEL SECURITY; ALTER TABLE bank_statements FORCE ROW LEVEL SECURITY;
16 ALTER TABLE audit_events ENABLE ROW LEVEL SECURITY; ALTER TABLE audit_events FORCE ROW LEVEL SECURITY;
17 CREATE POLICY users_tenant ON users USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
18 CREATE POLICY bank_accounts_tenant ON bank_accounts USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
19 CREATE POLICY beneficiaries_tenant ON beneficiaries USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
20 CREATE POLICY payments_tenant ON payments USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
21 CREATE POLICY approvals_tenant ON approvals USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
22 CREATE POLICY bank_statements_tenant ON bank_statements USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
23 CREATE POLICY audit_events_tenant ON audit_events USING (tenant_id = current_setting('app.tenant_id', true)) WITH CHECK (tenant_id = current_setting('app.tenant_id', true));
24 COMMIT;