Add backups, currencies and dashboard APIs, and scope access by guard_name.
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

Web users may only CRUD their own records, operators stay in assigned branches, and admin/manager/superadmin keep full access.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-31 18:14:47 +05:00
parent 0d000f9af6
commit 28509eb4ca
65 changed files with 9132 additions and 296 deletions

View File

@@ -16,6 +16,7 @@ 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_user;
DROP TABLE IF EXISTS permission_role;
DROP TABLE IF EXISTS role_user;
DROP TABLE IF EXISTS permissions;
@@ -24,3 +25,46 @@ 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 INDEX IF EXISTS idx_currencies_updated_by;
DROP INDEX IF EXISTS idx_currencies_created_by;
DROP INDEX IF EXISTS idx_currencies_to;
DROP INDEX IF EXISTS idx_currencies_from;
DROP INDEX IF EXISTS idx_backups_created_by;
DROP INDEX IF EXISTS idx_backups_created_at;
DROP INDEX IF EXISTS idx_visa_master_payment_orders_branch_id;
DROP INDEX IF EXISTS idx_visa_master_payment_orders_user_id;
DROP INDEX IF EXISTS idx_loan_paid_off_letters_branch_id;
DROP INDEX IF EXISTS idx_loan_paid_off_letters_user_id;
DROP INDEX IF EXISTS idx_loan_orders_branch_id;
DROP INDEX IF EXISTS idx_loan_orders_user_id;
DROP INDEX IF EXISTS idx_card_rekwizit_orders_branch_id;
DROP INDEX IF EXISTS idx_card_rekwizit_orders_user_id;
DROP INDEX IF EXISTS idx_card_pin_orders_branch_id;
DROP INDEX IF EXISTS idx_card_pin_orders_user_id;
DROP INDEX IF EXISTS idx_card_orders_branch_id;
DROP INDEX IF EXISTS idx_card_orders_user_id;
DROP INDEX IF EXISTS idx_cards_user_id;
DROP INDEX IF EXISTS idx_branches_province_id;
DROP INDEX IF EXISTS idx_branch_user_branch_id;
DROP INDEX IF EXISTS idx_branch_user_user_id;
DROP INDEX IF EXISTS idx_permissions_code;
DROP INDEX IF EXISTS idx_roles_code;
DROP INDEX IF EXISTS idx_permission_role_role_id;
DROP INDEX IF EXISTS idx_permission_user_user_id;
DROP INDEX IF EXISTS idx_role_user_user_id;
DROP TABLE IF EXISTS currencies;
DROP TABLE IF EXISTS backups;
DROP TABLE IF EXISTS card_balances;
DROP TABLE IF EXISTS card_rekwizit_orders;

View File

@@ -76,6 +76,22 @@ CREATE TABLE IF NOT EXISTS permission_role (
CONSTRAINT permission_role_permission_id_role_id_key UNIQUE (permission_id, role_id)
);
-- Per-user permission override on top of the role's baseline: 'allow' grants
-- a permission the user's role does not have, 'deny' revokes one the role
-- does grant. An admin can flip their mind by re-assigning the same
-- permission/user pair with a different effect (upsert), rather than having
-- to delete the old override first.
CREATE TABLE IF NOT EXISTS permission_user (
id bigserial PRIMARY KEY,
permission_id bigint REFERENCES permissions(id),
user_id bigint REFERENCES users(id),
effect character varying(10) NOT NULL DEFAULT 'allow',
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_user_permission_id_user_id_key UNIQUE (permission_id, user_id),
CONSTRAINT permission_user_effect_chk CHECK (effect IN ('allow', 'deny'))
);
CREATE TABLE IF NOT EXISTS provinces (
id bigserial PRIMARY KEY,
name json NOT NULL DEFAULT '{}'::json,
@@ -409,3 +425,134 @@ CREATE TABLE IF NOT EXISTS activity_logs (
CREATE INDEX IF NOT EXISTS idx_activity_logs_user_id ON activity_logs(user_id);
CREATE INDEX IF NOT EXISTS idx_activity_logs_module ON activity_logs(module);
CREATE INDEX IF NOT EXISTS idx_activity_logs_created_at ON activity_logs(created_at);
CREATE TABLE IF NOT EXISTS card_rekwizit_orders (
id bigserial PRIMARY KEY,
unique_id character varying(255),
user_id bigint,
card_type_id bigint NOT NULL,
card_number character varying(255) NOT NULL,
card_month character varying(255) NOT NULL,
card_year character varying(255) NOT NULL,
region character varying(255) NOT NULL,
branch_id bigint NOT NULL,
customer_name character varying(255) NOT NULL,
customer_surname character varying(255) NOT NULL,
customer_patronic_name character varying(255),
born_at date NOT NULL,
phone character varying(255) NOT NULL,
passport_serie character varying(255) NOT NULL,
passport_id character varying(255) NOT NULL,
passport_one text NOT NULL,
passport_two text NOT NULL,
passport_three text NOT NULL,
passport_four text NOT NULL,
status character varying(255) DEFAULT 'pending'::character varying,
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,
CONSTRAINT card_rekwizit_orders_card_type_id_fkey
FOREIGN KEY (card_type_id) REFERENCES card_types(id),
CONSTRAINT card_rekwizit_orders_branch_id_fkey
FOREIGN KEY (branch_id) REFERENCES branches(id),
CONSTRAINT card_rekwizit_orders_user_id_fkey
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE card_balances (
id bigserial PRIMARY KEY,
passport_number TEXT NOT NULL,
card_number TEXT NOT NULL,
card_last4 CHAR(4) GENERATED ALWAYS AS (right(card_number, 4)) STORED,
expiry_month SMALLINT NOT NULL,
expiry_year SMALLINT NOT NULL,
expiry_at DATE GENERATED ALWAYS AS
(make_date(expiry_year, expiry_month, 1)) STORED,
balance_amount NUMERIC(18,2),
balance_currency CHAR(3) DEFAULT 'TMT',
status TEXT NOT NULL DEFAULT 'pending',
checked_at TIMESTAMPTZ,
created_by BIGINT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
deleted_at TIMESTAMPTZ,
CONSTRAINT chk_month CHECK (expiry_month BETWEEN 1 AND 12),
CONSTRAINT chk_card CHECK (card_number ~ '^[0-9]{16,19}$'),
CONSTRAINT chk_status CHECK (status IN ('pending','success','failed','expired'))
);
CREATE TABLE IF NOT EXISTS backups (
id bigserial PRIMARY KEY,
file_name character varying(255) NOT NULL,
size_bytes bigint NOT NULL DEFAULT 0,
status character varying(50) NOT NULL DEFAULT 'pending',
error_message text,
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),
CONSTRAINT backups_created_by_fkey
FOREIGN KEY (created_by) REFERENCES users(id),
CONSTRAINT backups_status_chk CHECK (status IN ('pending', 'completed', 'failed'))
);
CREATE TABLE IF NOT EXISTS currencies (
id bigserial PRIMARY KEY,
currency_from character varying(10) NOT NULL,
currency_to character varying(10) NOT NULL,
value numeric(18, 8) NOT NULL,
created_by bigint,
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),
CONSTRAINT currencies_created_by_fkey FOREIGN KEY (created_by) REFERENCES users(id),
CONSTRAINT currencies_updated_by_fkey FOREIGN KEY (updated_by) REFERENCES users(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
-- user_id/branch_id. Postgres does not index foreign key columns
-- automatically, so without these every one of those lookups was a full
-- table scan.
CREATE INDEX IF NOT EXISTS idx_role_user_user_id ON role_user(user_id);
CREATE INDEX IF NOT EXISTS idx_permission_role_role_id ON permission_role(role_id);
CREATE INDEX IF NOT EXISTS idx_permission_user_user_id ON permission_user(user_id);
CREATE INDEX IF NOT EXISTS idx_roles_code ON roles(code);
CREATE INDEX IF NOT EXISTS idx_permissions_code ON permissions(code);
CREATE INDEX IF NOT EXISTS idx_branch_user_user_id ON branch_user(user_id);
CREATE INDEX IF NOT EXISTS idx_branch_user_branch_id ON branch_user(branch_id);
CREATE INDEX IF NOT EXISTS idx_branches_province_id ON branches(province_id);
CREATE INDEX IF NOT EXISTS idx_cards_user_id ON cards(user_id);
CREATE INDEX IF NOT EXISTS idx_card_orders_user_id ON card_orders(user_id);
CREATE INDEX IF NOT EXISTS idx_card_orders_branch_id ON card_orders(branch_id);
CREATE INDEX IF NOT EXISTS idx_card_pin_orders_user_id ON card_pin_orders(user_id);
CREATE INDEX IF NOT EXISTS idx_card_pin_orders_branch_id ON card_pin_orders(branch_id);
CREATE INDEX IF NOT EXISTS idx_card_rekwizit_orders_user_id ON card_rekwizit_orders(user_id);
CREATE INDEX IF NOT EXISTS idx_card_rekwizit_orders_branch_id ON card_rekwizit_orders(branch_id);
CREATE INDEX IF NOT EXISTS idx_loan_orders_user_id ON loan_orders(user_id);
CREATE INDEX IF NOT EXISTS idx_loan_orders_branch_id ON loan_orders(branch_id);
CREATE INDEX IF NOT EXISTS idx_loan_paid_off_letters_user_id ON loan_paid_off_letters(user_id);
CREATE INDEX IF NOT EXISTS idx_loan_paid_off_letters_branch_id ON loan_paid_off_letters(branch_id);
CREATE INDEX IF NOT EXISTS idx_visa_master_payment_orders_user_id ON visa_master_payment_orders(user_id);
CREATE INDEX IF NOT EXISTS idx_visa_master_payment_orders_branch_id ON visa_master_payment_orders(branch_id);
CREATE INDEX IF NOT EXISTS idx_backups_created_at ON backups(created_at);
CREATE INDEX IF NOT EXISTS idx_backups_created_by ON backups(created_by);
CREATE INDEX IF NOT EXISTS idx_currencies_from ON currencies(currency_from);
CREATE INDEX IF NOT EXISTS idx_currencies_to ON currencies(currency_to);
CREATE INDEX IF NOT EXISTS idx_currencies_created_by ON currencies(created_by);
CREATE INDEX IF NOT EXISTS idx_currencies_updated_by ON currencies(updated_by);

View File

@@ -0,0 +1 @@
UPDATE roles SET guard_name = 'admin' WHERE code = 'operator';

View File

@@ -0,0 +1,7 @@
-- Guard enforces API access by roles.guard_name:
-- web = customer (own data)
-- operator = branch staff
-- admin = admin / manager / superadmin
UPDATE roles SET guard_name = 'web' WHERE code = 'user';
UPDATE roles SET guard_name = 'operator' WHERE code = 'operator';
UPDATE roles SET guard_name = 'admin' WHERE code IN ('admin', 'superadmin', 'manager');