diff --git a/db/migrations/20260702125108_init.down.sql b/db/migrations/20260702125108_init.down.sql index a3e03f0..8269ce4 100644 --- a/db/migrations/20260702125108_init.down.sql +++ b/db/migrations/20260702125108_init.down.sql @@ -1,29 +1,6 @@ -DROP INDEX IF EXISTS idx_activity_logs_created_at; -DROP INDEX IF EXISTS idx_activity_logs_module; -DROP INDEX IF EXISTS idx_activity_logs_user_id; -DROP TABLE IF EXISTS activity_logs; -DROP TABLE IF EXISTS visa_master_payment_orders; -DROP TABLE IF EXISTS loan_paid_off_letters; -DROP TABLE IF EXISTS loans; -DROP TABLE IF EXISTS loan_orders; -DROP TABLE IF EXISTS card_pin_orders; -DROP TABLE IF EXISTS card_orders; -DROP TABLE IF EXISTS cards; -DROP TABLE IF EXISTS loan_order_required_docs; -DROP TABLE IF EXISTS loan_types; -DROP TABLE IF EXISTS card_states; -DROP TABLE IF EXISTS card_types; -DROP TABLE IF EXISTS branch_user; -DROP TABLE IF EXISTS branches; -DROP TABLE IF EXISTS provinces; -DROP TABLE IF EXISTS permission_role; -DROP TABLE IF EXISTS role_user; -DROP TABLE IF EXISTS permissions; -DROP TABLE IF EXISTS roles; -DROP TABLE IF EXISTS registration_verifications; -DROP INDEX IF EXISTS idx_users_phone; -DROP INDEX IF EXISTS idx_users_username; -DROP TABLE IF EXISTS users; +DROP TABLE IF EXISTS visa_master_payment_logs; +DROP TABLE IF EXISTS visa_master_order_payments; +DROP TABLE IF EXISTS payment_settings; DROP INDEX IF EXISTS idx_currencies_updated_by; DROP INDEX IF EXISTS idx_currencies_created_by; @@ -69,3 +46,38 @@ DROP TABLE IF EXISTS currencies; DROP TABLE IF EXISTS backups; DROP TABLE IF EXISTS card_balances; DROP TABLE IF EXISTS card_rekwizit_orders; + +DROP INDEX IF EXISTS idx_activity_logs_created_at; +DROP INDEX IF EXISTS idx_activity_logs_module; +DROP INDEX IF EXISTS idx_activity_logs_user_id; +DROP TABLE IF EXISTS activity_logs; +DROP TABLE IF EXISTS visa_master_payment_orders; +DROP TABLE IF EXISTS loan_paid_off_letters; +DROP TABLE IF EXISTS loans; +DROP TABLE IF EXISTS loan_orders; +DROP TABLE IF EXISTS card_pin_orders; +DROP TABLE IF EXISTS card_orders; +DROP TABLE IF EXISTS cards; +DROP TABLE IF EXISTS loan_order_required_docs; +DROP TABLE IF EXISTS loan_types; +DROP TABLE IF EXISTS card_states; +DROP TABLE IF EXISTS card_types; + +DROP INDEX IF EXISTS branch_user_branch_id_user_id_idx; +DROP TABLE IF EXISTS branch_user; + +DROP INDEX IF EXISTS branches_unique_code_idx; +DROP TABLE IF EXISTS branches; + +DROP TABLE IF EXISTS provinces; + +DROP INDEX IF EXISTS permission_role_permission_id_role_id_idx; +DROP TABLE IF EXISTS permission_role; +DROP TABLE IF EXISTS role_user; +DROP TABLE IF EXISTS permissions; +DROP TABLE IF EXISTS roles; +DROP TABLE IF EXISTS registration_verifications; + +DROP INDEX IF EXISTS idx_users_phone; +DROP INDEX IF EXISTS idx_users_username; +DROP TABLE IF EXISTS users; diff --git a/db/migrations/20260702125108_init.up.sql b/db/migrations/20260702125108_init.up.sql index cc6e74d..1b24517 100644 --- a/db/migrations/20260702125108_init.up.sql +++ b/db/migrations/20260702125108_init.up.sql @@ -13,16 +13,17 @@ CREATE TABLE IF NOT EXISTS users ( role character varying(250) NOT NULL DEFAULT 'user', active boolean NOT NULL DEFAULT true, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE UNIQUE INDEX IF NOT EXISTS idx_users_username ON users (LOWER(username)) - WHERE username <> ''; + WHERE username <> '' AND deleted_at IS NULL; CREATE UNIQUE INDEX IF NOT EXISTS idx_users_phone ON users (phone) - WHERE phone <> ''; + WHERE phone <> '' AND deleted_at IS NULL; CREATE TABLE IF NOT EXISTS registration_verifications ( id bigserial PRIMARY KEY, @@ -43,7 +44,8 @@ CREATE TABLE IF NOT EXISTS roles ( name character varying(255) NOT NULL, guard_name character varying(255) NOT NULL, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); INSERT INTO roles (code, name, guard_name) @@ -57,7 +59,8 @@ CREATE TABLE IF NOT EXISTS permissions ( guard_name character varying(255) NOT NULL, module character varying(255) NOT NULL DEFAULT '', created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS role_user ( @@ -65,7 +68,8 @@ CREATE TABLE IF NOT EXISTS role_user ( role_id bigint REFERENCES roles(id), user_id bigint REFERENCES users(id), created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS permission_role ( @@ -74,21 +78,26 @@ CREATE TABLE IF NOT EXISTS permission_role ( role_id bigint REFERENCES roles(id), created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - CONSTRAINT permission_role_permission_id_role_id_key UNIQUE (permission_id, role_id) + deleted_at TIMESTAMPTZ ); +CREATE UNIQUE INDEX IF NOT EXISTS permission_role_permission_id_role_id_idx + ON permission_role (permission_id, role_id) + WHERE deleted_at IS NULL; + CREATE TABLE IF NOT EXISTS provinces ( id bigserial PRIMARY KEY, name json NOT NULL DEFAULT '{}'::json, region json NOT NULL DEFAULT '{}'::json, active boolean NOT NULL DEFAULT true, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS branches ( id bigserial PRIMARY KEY, - unique_code character varying(255) UNIQUE, + unique_code character varying(255), name json NOT NULL DEFAULT '{}'::json, address json NOT NULL DEFAULT '{}'::json, region character varying(20) NOT NULL DEFAULT '', @@ -105,18 +114,31 @@ CREATE TABLE IF NOT EXISTS branches ( active boolean NOT NULL DEFAULT true, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, CONSTRAINT branches_province_id_fkey FOREIGN KEY (province_id) REFERENCES provinces(id) ON UPDATE CASCADE ); +CREATE UNIQUE INDEX IF NOT EXISTS branches_unique_code_idx + ON branches (unique_code) + WHERE deleted_at IS NULL; + CREATE TABLE IF NOT EXISTS branch_user ( id bigserial PRIMARY KEY, branch_id bigint REFERENCES branches(id), user_id bigint REFERENCES users(id), created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); +-- branch_user is many-to-many (a user may be assigned to several branches at +-- once); this index only stops the same pair being assigned twice while +-- active, so re-assigning it can be a no-op instead of a duplicate row. +CREATE UNIQUE INDEX IF NOT EXISTS branch_user_branch_id_user_id_idx + ON branch_user (branch_id, user_id) + WHERE deleted_at IS NULL; + CREATE TABLE IF NOT EXISTS card_types ( id bigserial PRIMARY KEY, name json NOT NULL DEFAULT '{}'::json, @@ -124,7 +146,8 @@ CREATE TABLE IF NOT EXISTS card_types ( notes character varying(255) NOT NULL DEFAULT '', active boolean NOT NULL DEFAULT true, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS card_states ( @@ -134,7 +157,8 @@ CREATE TABLE IF NOT EXISTS card_states ( notes character varying(255) NOT NULL DEFAULT '', active boolean NOT NULL DEFAULT true, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS loan_types ( @@ -145,7 +169,8 @@ CREATE TABLE IF NOT EXISTS loan_types ( notes json NOT NULL DEFAULT '{}'::json, active boolean NOT NULL DEFAULT true, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS loan_order_required_docs ( @@ -154,7 +179,8 @@ CREATE TABLE IF NOT EXISTS loan_order_required_docs ( value text NOT NULL DEFAULT '', active boolean NOT NULL DEFAULT true, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS cards ( @@ -167,6 +193,7 @@ CREATE TABLE IF NOT EXISTS cards ( type character varying(250), created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, CONSTRAINT cards_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ); @@ -204,6 +231,7 @@ CREATE TABLE IF NOT EXISTS card_orders ( paid boolean NOT NULL DEFAULT false, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, CONSTRAINT card_orders_card_state_id_fkey FOREIGN KEY (card_state_id) REFERENCES card_states(id), CONSTRAINT card_orders_card_type_id_fkey @@ -237,6 +265,7 @@ CREATE TABLE IF NOT EXISTS card_pin_orders ( notes character varying(250), created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, CONSTRAINT card_pin_orders_card_type_id_fkey FOREIGN KEY (card_type_id) REFERENCES card_types(id), CONSTRAINT card_pin_orders_branch_id_fkey @@ -315,7 +344,8 @@ CREATE TABLE IF NOT EXISTS loan_orders ( satisfiable character varying(255), notes text, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS loans ( @@ -326,6 +356,7 @@ CREATE TABLE IF NOT EXISTS loans ( account_number character varying(255) NOT NULL, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, CONSTRAINT loans_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ); @@ -352,6 +383,7 @@ CREATE TABLE IF NOT EXISTS loan_paid_off_letters ( loan_file text, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, CONSTRAINT loan_paid_off_letters_branch_id_fkey FOREIGN KEY (branch_id) REFERENCES branches(id), CONSTRAINT loan_paid_off_letters_user_id_fkey @@ -384,8 +416,10 @@ CREATE TABLE IF NOT EXISTS visa_master_payment_orders ( reciever_passport_number character varying(255), reciever_deposit_account character varying(255), paid boolean NOT NULL DEFAULT false, + amount_usd numeric(18, 2), created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, CONSTRAINT visa_master_payment_orders_branch_id_fkey FOREIGN KEY (branch_id) REFERENCES branches(id), CONSTRAINT visa_master_payment_orders_user_id_fkey @@ -437,6 +471,7 @@ CREATE TABLE IF NOT EXISTS card_rekwizit_orders ( notes text, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, CONSTRAINT card_rekwizit_orders_card_type_id_fkey FOREIGN KEY (card_type_id) REFERENCES card_types(id), CONSTRAINT card_rekwizit_orders_branch_id_fkey @@ -481,12 +516,16 @@ CREATE TABLE IF NOT EXISTS backups ( created_by bigint, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - CONSTRAINT backups_file_name_key UNIQUE (file_name), + deleted_at TIMESTAMPTZ, CONSTRAINT backups_created_by_fkey FOREIGN KEY (created_by) REFERENCES users(id), CONSTRAINT backups_status_chk CHECK (status IN ('pending', 'completed', 'failed')) ); +CREATE UNIQUE INDEX IF NOT EXISTS backups_file_name_idx + ON backups (file_name) + WHERE deleted_at IS NULL; + CREATE TABLE IF NOT EXISTS currencies ( id bigserial PRIMARY KEY, currency_from character varying(10) NOT NULL, @@ -496,11 +535,101 @@ CREATE TABLE IF NOT EXISTS currencies ( updated_by bigint, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - CONSTRAINT currencies_from_to_key UNIQUE (currency_from, currency_to), + deleted_at TIMESTAMPTZ, CONSTRAINT currencies_created_by_fkey FOREIGN KEY (created_by) REFERENCES users(id), CONSTRAINT currencies_updated_by_fkey FOREIGN KEY (updated_by) REFERENCES users(id) ); +CREATE UNIQUE INDEX IF NOT EXISTS currencies_from_to_idx + ON currencies (currency_from, currency_to) + WHERE deleted_at IS NULL; + +-- Single-row, admin-configurable commission fees used on every Töleg +-- (Visa/Master/Sber money-transfer) payout. Visa/Master orders use +-- bank_fee_tmt + gbus_fee_tmt; Sber orders use their own sber_bank_fee_tmt +-- plus a USD-denominated tveb_fee_usd (converted to TMT at payout time via +-- the USD rate) - Sber has no GBÜS fee. max_amount_usd is the single +-- admin-configurable payout cap (converted to TMT at the current rate in +-- /quote and /pay) instead of a per-order value the client supplied when +-- creating the order. +CREATE TABLE IF NOT EXISTS payment_settings ( + id smallint PRIMARY KEY DEFAULT 1, + bank_fee_tmt numeric(12, 2) NOT NULL DEFAULT 100, + gbus_fee_tmt numeric(12, 2) NOT NULL DEFAULT 15, + sber_bank_fee_tmt numeric(12, 2) NOT NULL DEFAULT 100, + tveb_fee_usd numeric(12, 2) NOT NULL DEFAULT 18, + max_amount_usd numeric(12, 2) NOT NULL DEFAULT 250, + updated_by bigint, + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + CONSTRAINT payment_settings_single_row CHECK (id = 1), + CONSTRAINT payment_settings_updated_by_fkey + FOREIGN KEY (updated_by) REFERENCES users(id) +); + +INSERT INTO payment_settings (id, bank_fee_tmt, gbus_fee_tmt, sber_bank_fee_tmt, tveb_fee_usd) +VALUES (1, 100, 15, 100, 18) +ON CONFLICT (id) DO NOTHING; + +-- Every payout attempt for a visa/master/sber order, as its own row - a full +-- audit trail (including failed gateway attempts) rather than columns on the +-- order itself, so a retry after a failure doesn't erase what the previous +-- attempt did. The Sber-specific columns (exchange_rate_rub, tveb_fee_usd, +-- tveb_fee_tmt) stay null for non-Sber attempts, same as gbus_fee_tmt stays +-- null for Sber ones. gateway_status/gateway_reference/gateway_response +-- track the GBÜS register→callback round trip (pending until the callback +-- verifies the outcome server-to-server). +CREATE TABLE IF NOT EXISTS visa_master_order_payments ( + id bigserial PRIMARY KEY, + order_id bigint NOT NULL, + user_id bigint NOT NULL, + payment_period date NOT NULL, + exchange_rate_usd numeric(18, 8) NOT NULL, + exchange_rate_rub numeric(18, 8), + amount_tmt numeric(18, 2) NOT NULL, + bank_fee_tmt numeric(12, 2) NOT NULL, + gbus_fee_tmt numeric(12, 2), + tveb_fee_usd numeric(12, 2), + tveb_fee_tmt numeric(12, 2), + total_amount_tmt numeric(18, 2) NOT NULL, + gateway_status character varying(32) NOT NULL DEFAULT 'pending', + gateway_reference character varying(255), + gateway_response jsonb, + created_by bigint, + created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + deleted_at TIMESTAMPTZ, + CONSTRAINT visa_master_order_payments_order_id_fkey + FOREIGN KEY (order_id) REFERENCES visa_master_payment_orders(id), + CONSTRAINT visa_master_order_payments_user_id_fkey + FOREIGN KEY (user_id) REFERENCES users(id), + CONSTRAINT visa_master_order_payments_created_by_fkey + FOREIGN KEY (created_by) REFERENCES users(id) +); + +CREATE INDEX IF NOT EXISTS visa_master_order_payments_order_id_idx ON visa_master_order_payments (order_id); +CREATE INDEX IF NOT EXISTS visa_master_order_payments_user_id_idx ON visa_master_order_payments (user_id); + +-- Every individual step taken while processing a payout (validated, rate +-- fetched, gateway request sent, gateway response received, order marked +-- paid/attempt recorded) as its own row, so there's a step-by-step, +-- timestamped record of what actually happened for a given order - +-- evidence if a payout is later disputed, independent of (and more +-- granular than) the one summary row in visa_master_order_payments. +CREATE TABLE IF NOT EXISTS visa_master_payment_logs ( + id bigserial PRIMARY KEY, + order_id bigint NOT NULL, + step character varying(100) NOT NULL, + detail jsonb, + created_by bigint, + created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, + CONSTRAINT visa_master_payment_logs_order_id_fkey + FOREIGN KEY (order_id) REFERENCES visa_master_payment_orders(id), + CONSTRAINT visa_master_payment_logs_created_by_fkey + FOREIGN KEY (created_by) REFERENCES users(id) +); + +CREATE INDEX IF NOT EXISTS visa_master_payment_logs_order_id_idx ON visa_master_payment_logs (order_id); + -- Indexes for hot filter/join columns: role_user, permission_role and -- branch_user are queried on every authenticated request (RBAC role and -- branch resolution), and every order table below is list-filtered by diff --git a/db/migrations/20260905130000_add_soft_delete.down.sql b/db/migrations/20260905130000_add_soft_delete.down.sql deleted file mode 100644 index ffd0c70..0000000 --- a/db/migrations/20260905130000_add_soft_delete.down.sql +++ /dev/null @@ -1,48 +0,0 @@ --- Reverse 20260905130000_add_soft_delete.up.sql - -DROP INDEX IF EXISTS branch_user_branch_id_user_id_idx; - -DROP INDEX IF EXISTS permission_role_permission_id_role_id_idx; -ALTER TABLE permission_role ADD CONSTRAINT permission_role_permission_id_role_id_key UNIQUE (permission_id, role_id); - -DROP INDEX IF EXISTS currencies_from_to_idx; -ALTER TABLE currencies ADD CONSTRAINT currencies_from_to_key UNIQUE (currency_from, currency_to); - -DROP INDEX IF EXISTS backups_file_name_idx; -ALTER TABLE backups ADD CONSTRAINT backups_file_name_key UNIQUE (file_name); - -DROP INDEX IF EXISTS branches_unique_code_idx; -ALTER TABLE branches ADD CONSTRAINT branches_unique_code_key UNIQUE (unique_code); - -DROP INDEX IF EXISTS idx_users_phone; -CREATE UNIQUE INDEX idx_users_phone - ON users (phone) - WHERE phone <> ''; - -DROP INDEX IF EXISTS idx_users_username; -CREATE UNIQUE INDEX idx_users_username - ON users (LOWER(username)) - WHERE username <> ''; - -ALTER TABLE currencies DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE backups DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE card_rekwizit_orders DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE visa_master_payment_orders DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE loan_paid_off_letters DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE loans DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE loan_orders DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE card_pin_orders DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE card_orders DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE cards DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE loan_order_required_docs DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE loan_types DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE card_states DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE card_types DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE branch_user DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE branches DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE provinces DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE permission_role DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE role_user DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE permissions DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE roles DROP COLUMN IF EXISTS deleted_at; -ALTER TABLE users DROP COLUMN IF EXISTS deleted_at; diff --git a/db/migrations/20260905130000_add_soft_delete.up.sql b/db/migrations/20260905130000_add_soft_delete.up.sql deleted file mode 100644 index d319680..0000000 --- a/db/migrations/20260905130000_add_soft_delete.up.sql +++ /dev/null @@ -1,70 +0,0 @@ --- Add deleted_at (soft delete) to every table that has a DELETE endpoint but --- doesn't already support soft delete. card_balances already has deleted_at --- (see 20260702125108_init.up.sql) and is left untouched. activity_logs and --- registration_verifications have no DELETE endpoint / aren't a user-managed --- CRUD resource, so they're intentionally excluded. - -ALTER TABLE users ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE roles ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE permissions ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE role_user ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE permission_role ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE provinces ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE branches ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE branch_user ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE card_types ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE card_states ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE loan_types ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE loan_order_required_docs ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE cards ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE card_orders ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE card_pin_orders ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE loan_orders ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE loans ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE loan_paid_off_letters ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE visa_master_payment_orders ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE card_rekwizit_orders ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE backups ADD COLUMN deleted_at TIMESTAMPTZ; -ALTER TABLE currencies ADD COLUMN deleted_at TIMESTAMPTZ; - --- Rescope existing uniqueness constraints to active (non-soft-deleted) rows --- only, so a soft-deleted row's unique value (username, phone, branch code, --- backup file name, currency pair, permission-role pair) can be reused by a --- new row. - -DROP INDEX IF EXISTS idx_users_username; -CREATE UNIQUE INDEX idx_users_username - ON users (LOWER(username)) - WHERE username <> '' AND deleted_at IS NULL; - -DROP INDEX IF EXISTS idx_users_phone; -CREATE UNIQUE INDEX idx_users_phone - ON users (phone) - WHERE phone <> '' AND deleted_at IS NULL; - -ALTER TABLE branches DROP CONSTRAINT IF EXISTS branches_unique_code_key; -CREATE UNIQUE INDEX branches_unique_code_idx - ON branches (unique_code) - WHERE deleted_at IS NULL; - -ALTER TABLE backups DROP CONSTRAINT IF EXISTS backups_file_name_key; -CREATE UNIQUE INDEX backups_file_name_idx - ON backups (file_name) - WHERE deleted_at IS NULL; - -ALTER TABLE currencies DROP CONSTRAINT IF EXISTS currencies_from_to_key; -CREATE UNIQUE INDEX currencies_from_to_idx - ON currencies (currency_from, currency_to) - WHERE deleted_at IS NULL; - -ALTER TABLE permission_role DROP CONSTRAINT IF EXISTS permission_role_permission_id_role_id_key; -CREATE UNIQUE INDEX permission_role_permission_id_role_id_idx - ON permission_role (permission_id, role_id) - WHERE deleted_at IS NULL; - --- branch_user is many-to-many (a user may be assigned to several branches at --- once); this index only stops the same pair being assigned twice while --- active, so re-assigning it can be a no-op instead of a duplicate row. -CREATE UNIQUE INDEX branch_user_branch_id_user_id_idx - ON branch_user (branch_id, user_id) - WHERE deleted_at IS NULL; diff --git a/db/migrations/20260905150000_widen_loan_paid_off_letters_region.down.sql b/db/migrations/20260905150000_widen_loan_paid_off_letters_region.down.sql deleted file mode 100644 index 8a6c328..0000000 --- a/db/migrations/20260905150000_widen_loan_paid_off_letters_region.down.sql +++ /dev/null @@ -1,7 +0,0 @@ --- Reverse 20260905150000_widen_loan_paid_off_letters_region.up.sql --- --- Only safe if no row currently holds a region value longer than 2 --- characters; if the widened column is already in real use with longer --- values, narrowing it back will fail (or truncate, if forced), and that is --- exactly the failure mode this migration set out to remove. -ALTER TABLE loan_paid_off_letters ALTER COLUMN region TYPE character varying(2); diff --git a/db/migrations/20260905150000_widen_loan_paid_off_letters_region.up.sql b/db/migrations/20260905150000_widen_loan_paid_off_letters_region.up.sql deleted file mode 100644 index 6946eea..0000000 --- a/db/migrations/20260905150000_widen_loan_paid_off_letters_region.up.sql +++ /dev/null @@ -1,15 +0,0 @@ --- loan_paid_off_letters.region was originally character varying(2), mirroring --- branches' 2-letter region codes (AG/AH/BL/DZ/MR/LB). Every sibling order --- table (card_orders, card_pin_orders, card_rekwizit_orders, loan_orders, --- visa_master_payment_orders) stores region as free text instead. Sending a --- normal region name here (e.g. "Ashgabat") overflowed the column and --- surfaced to the client as a raw 500 instead of a validation error. --- --- init.up.sql has already been edited in place to varchar(255) for fresh --- databases, but per this project's migration convention (see --- 20260905130000_add_soft_delete), that edit alone does not reach a database --- that was migrated before the edit. This migration widens the column there --- too. Running it against a database created after the init.up.sql edit is a --- harmless no-op (the column is already varchar(255)). - -ALTER TABLE loan_paid_off_letters ALTER COLUMN region TYPE character varying(255); diff --git a/db/migrations/20260918120000_add_branch_user_unique_index.down.sql b/db/migrations/20260918120000_add_branch_user_unique_index.down.sql deleted file mode 100644 index e2cf66c..0000000 --- a/db/migrations/20260918120000_add_branch_user_unique_index.down.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Reverse 20260918120000_add_branch_user_unique_index.up.sql -DROP INDEX IF EXISTS branch_user_branch_id_user_id_idx; diff --git a/db/migrations/20260918120000_add_branch_user_unique_index.up.sql b/db/migrations/20260918120000_add_branch_user_unique_index.up.sql deleted file mode 100644 index 06e9dcc..0000000 --- a/db/migrations/20260918120000_add_branch_user_unique_index.up.sql +++ /dev/null @@ -1,14 +0,0 @@ --- branch_user_branch_id_user_id_idx has been added in place to --- 20260905130000_add_soft_delete.up.sql for fresh databases, but per this --- project's migration convention (see 20260905150000_widen_loan_paid_off_letters_region), --- that edit alone does not reach a database that was migrated before the --- edit. This migration adds the index there too. Running it against a --- database created after the edit is a harmless no-op. --- --- The index scopes branch_user's many-to-many pairing (a user may belong to --- several branches) to one active row per (branch_id, user_id) pair, so the --- application can upsert an assignment instead of creating duplicate rows. - -CREATE UNIQUE INDEX IF NOT EXISTS branch_user_branch_id_user_id_idx - ON branch_user (branch_id, user_id) - WHERE deleted_at IS NULL; diff --git a/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.down.sql b/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.down.sql deleted file mode 100644 index 63b1527..0000000 --- a/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.down.sql +++ /dev/null @@ -1,15 +0,0 @@ --- Reverse 20260918130000_add_payment_settings_and_visamaster_pay.up.sql, --- in reverse order of that file's sections. - --- 4. visa_master_payment_logs -DROP TABLE IF EXISTS visa_master_payment_logs; - --- 3. visa_master_order_payments -DROP TABLE IF EXISTS visa_master_order_payments; - --- 2. visa_master_payment_orders.amount_usd -ALTER TABLE visa_master_payment_orders - DROP COLUMN IF EXISTS amount_usd; - --- 1. payment_settings -DROP TABLE IF EXISTS payment_settings; diff --git a/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.up.sql b/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.up.sql deleted file mode 100644 index d337eba..0000000 --- a/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.up.sql +++ /dev/null @@ -1,90 +0,0 @@ --- Adds the "Töleg" payment feature for Visa/Master/Sber money-transfer --- orders. Each section below is a distinct, independently reversible piece --- of that feature - see the matching comment in each section for why it's --- shaped the way it is. - --- 1. Single-row, admin-configurable commission fees used on every payment. --- Visa/Master orders use bank_fee_tmt + gbus_fee_tmt; Sber orders use their --- own sber_bank_fee_tmt plus a USD-denominated tveb_fee_usd (converted to --- TMT at payout time via the USD rate) - Sber has no GBÜS fee. -CREATE TABLE IF NOT EXISTS payment_settings ( - id smallint PRIMARY KEY DEFAULT 1, - bank_fee_tmt numeric(12, 2) NOT NULL DEFAULT 100, - gbus_fee_tmt numeric(12, 2) NOT NULL DEFAULT 15, - sber_bank_fee_tmt numeric(12, 2) NOT NULL DEFAULT 100, - tveb_fee_usd numeric(12, 2) NOT NULL DEFAULT 18, - updated_by bigint, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - CONSTRAINT payment_settings_single_row CHECK (id = 1), - CONSTRAINT payment_settings_updated_by_fkey - FOREIGN KEY (updated_by) REFERENCES users(id) -); - -INSERT INTO payment_settings (id, bank_fee_tmt, gbus_fee_tmt, sber_bank_fee_tmt, tveb_fee_usd) -VALUES (1, 100, 15, 100, 18) -ON CONFLICT (id) DO NOTHING; - --- 2. The requested USD amount for a visa/master/sber order (order data, set --- when the order is created). visa_master_payment_orders already has `paid`. -ALTER TABLE visa_master_payment_orders - ADD COLUMN IF NOT EXISTS amount_usd numeric(18, 2); - --- 3. Every payout attempt for an order, as its own row - a full audit trail --- (including failed gateway attempts) rather than columns on the order --- itself, so a retry after a failure doesn't erase what the previous --- attempt did. The Sber-specific columns (exchange_rate_rub, tveb_fee_usd, --- tveb_fee_tmt) stay null for non-Sber attempts, same as gbus_fee_tmt stays --- null for Sber ones. gateway_status/gateway_reference/gateway_response --- track the GBÜS register→callback round trip (pending until the callback --- verifies the outcome server-to-server). -CREATE TABLE IF NOT EXISTS visa_master_order_payments ( - id bigserial PRIMARY KEY, - order_id bigint NOT NULL, - user_id bigint NOT NULL, - payment_period date NOT NULL, - exchange_rate_usd numeric(18, 8) NOT NULL, - exchange_rate_rub numeric(18, 8), - amount_tmt numeric(18, 2) NOT NULL, - bank_fee_tmt numeric(12, 2) NOT NULL, - gbus_fee_tmt numeric(12, 2), - tveb_fee_usd numeric(12, 2), - tveb_fee_tmt numeric(12, 2), - total_amount_tmt numeric(18, 2) NOT NULL, - gateway_status character varying(32) NOT NULL DEFAULT 'pending', - gateway_reference character varying(255), - gateway_response jsonb, - created_by bigint, - created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - deleted_at TIMESTAMPTZ, - CONSTRAINT visa_master_order_payments_order_id_fkey - FOREIGN KEY (order_id) REFERENCES visa_master_payment_orders(id), - CONSTRAINT visa_master_order_payments_user_id_fkey - FOREIGN KEY (user_id) REFERENCES users(id), - CONSTRAINT visa_master_order_payments_created_by_fkey - FOREIGN KEY (created_by) REFERENCES users(id) -); - -CREATE INDEX IF NOT EXISTS visa_master_order_payments_order_id_idx ON visa_master_order_payments (order_id); -CREATE INDEX IF NOT EXISTS visa_master_order_payments_user_id_idx ON visa_master_order_payments (user_id); - --- 4. Every individual step taken while processing a payout (validated, rate --- fetched, gateway request sent, gateway response received, order marked --- paid/attempt recorded) as its own row, so there's a step-by-step, --- timestamped record of what actually happened for a given order - --- evidence if a payout is later disputed, independent of (and more --- granular than) the one summary row in visa_master_order_payments. -CREATE TABLE IF NOT EXISTS visa_master_payment_logs ( - id bigserial PRIMARY KEY, - order_id bigint NOT NULL, - step character varying(100) NOT NULL, - detail jsonb, - created_by bigint, - created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, - CONSTRAINT visa_master_payment_logs_order_id_fkey - FOREIGN KEY (order_id) REFERENCES visa_master_payment_orders(id), - CONSTRAINT visa_master_payment_logs_created_by_fkey - FOREIGN KEY (created_by) REFERENCES users(id) -); - -CREATE INDEX IF NOT EXISTS visa_master_payment_logs_order_id_idx ON visa_master_payment_logs (order_id); diff --git a/db/migrations/20260919000000_add_max_amount_usd_to_payment_settings.down.sql b/db/migrations/20260919000000_add_max_amount_usd_to_payment_settings.down.sql deleted file mode 100644 index 1322548..0000000 --- a/db/migrations/20260919000000_add_max_amount_usd_to_payment_settings.down.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE payment_settings - DROP COLUMN IF EXISTS max_amount_usd; diff --git a/db/migrations/20260919000000_add_max_amount_usd_to_payment_settings.up.sql b/db/migrations/20260919000000_add_max_amount_usd_to_payment_settings.up.sql deleted file mode 100644 index 2a4907a..0000000 --- a/db/migrations/20260919000000_add_max_amount_usd_to_payment_settings.up.sql +++ /dev/null @@ -1,7 +0,0 @@ --- The max payable USD amount per visa/master/sber payout is now a single --- admin-configurable limit (like tveb_fee_usd, converted to TMT at the --- current rate in /quote and /pay) instead of a per-order value the client --- supplied when creating the order - so an operator can no longer inflate --- an individual order's own cap. -ALTER TABLE payment_settings - ADD COLUMN IF NOT EXISTS max_amount_usd numeric(12, 2) NOT NULL DEFAULT 250;