Add backups, currencies and dashboard APIs, and scope access by guard_name.
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:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
1
db/migrations/20260831172100_role_guard_names.down.sql
Normal file
1
db/migrations/20260831172100_role_guard_names.down.sql
Normal file
@@ -0,0 +1 @@
|
||||
UPDATE roles SET guard_name = 'admin' WHERE code = 'operator';
|
||||
7
db/migrations/20260831172100_role_guard_names.up.sql
Normal file
7
db/migrations/20260831172100_role_guard_names.up.sql
Normal 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');
|
||||
@@ -11,10 +11,13 @@ BEGIN;
|
||||
|
||||
TRUNCATE TABLE
|
||||
activity_logs,
|
||||
backups,
|
||||
currencies,
|
||||
visa_master_payment_orders,
|
||||
loan_paid_off_letters,
|
||||
loans,
|
||||
loan_orders,
|
||||
card_rekwizit_orders,
|
||||
card_pin_orders,
|
||||
card_orders,
|
||||
cards,
|
||||
@@ -107,7 +110,7 @@ INSERT INTO roles (code, name, guard_name) VALUES
|
||||
('superadmin', 'Super Administrator', 'admin'),
|
||||
('admin', 'Administrator', 'admin'),
|
||||
('manager', 'Branch Manager', 'admin'),
|
||||
('operator', 'Operator', 'admin'),
|
||||
('operator', 'Operator', 'operator'),
|
||||
('user', 'User', 'web');
|
||||
|
||||
INSERT INTO permissions (code, name, guard_name) VALUES
|
||||
@@ -125,7 +128,11 @@ INSERT INTO permissions (code, name, guard_name) VALUES
|
||||
('card-orders.manage', 'Manage card orders', 'admin'),
|
||||
('loans.view', 'View loans', 'admin'),
|
||||
('loans.manage', 'Manage loans', 'admin'),
|
||||
('activity-logs.view', 'View activity logs', 'admin');
|
||||
('activity-logs.view', 'View activity logs', 'admin'),
|
||||
('backups.view', 'View backups', 'admin'),
|
||||
('backups.manage', 'Manage backups', 'admin'),
|
||||
('currencies.view', 'View currencies', 'admin'),
|
||||
('currencies.manage', 'Manage currencies', 'admin');
|
||||
|
||||
INSERT INTO role_user (role_id, user_id)
|
||||
SELECT r.id, u.id
|
||||
@@ -392,12 +399,12 @@ INSERT INTO loan_order_required_docs (name, value, active) VALUES
|
||||
-- Cards belonging to users
|
||||
-- ---------------------------------------------------------------------------
|
||||
INSERT INTO cards (user_id, number, name, month, year, type) VALUES
|
||||
(5, '4169740012345678', 'SERDAR MYRADOW', '12', '28', 'visa'),
|
||||
(6, '4169740087654321', 'GULNAR ORAZOWA', '06', '27', 'visa'),
|
||||
(8, '5536910011223344', 'MAYA HALYLOWA', '03', '29', 'mastercard'),
|
||||
(7, '9860010055667788', 'DOVRAN ANNAYEW', '09', '26', 'altyn'),
|
||||
(9, '4169740011112222', 'KERIM ATAYEW', '08', '28', 'visa'),
|
||||
(10, '5536910055556666', 'LEYLA GURBANOWA', '11', '29', 'mastercard');
|
||||
(5, '4169740012345678', 'SERDAR MYRADOW', '12', '28', '1'),
|
||||
(6, '4169740087654321', 'GULNAR ORAZOWA', '06', '27', '1'),
|
||||
(8, '5536910011223344', 'MAYA HALYLOWA', '03', '29', '3'),
|
||||
(7, '9860010055667788', 'DOVRAN ANNAYEW', '09', '26', '4'),
|
||||
(9, '4169740011112222', 'KERIM ATAYEW', '08', '28', '1'),
|
||||
(10, '5536910055556666', 'LEYLA GURBANOWA', '11', '29', '3');
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Card orders
|
||||
@@ -479,6 +486,42 @@ INSERT INTO card_pin_orders (
|
||||
'rejected', 'Passport mismatch'
|
||||
);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Card rekwizit orders
|
||||
-- ---------------------------------------------------------------------------
|
||||
INSERT INTO card_rekwizit_orders (
|
||||
unique_id, user_id, card_type_id, card_number, card_month, card_year,
|
||||
region, branch_id,
|
||||
customer_name, customer_surname, customer_patronic_name, born_at, phone,
|
||||
passport_serie, passport_id,
|
||||
passport_one, passport_two, passport_three, passport_four,
|
||||
status, notes
|
||||
) VALUES
|
||||
(
|
||||
'CRO-2026-0001', 6, 1, '4169740088776655', '08', '2028',
|
||||
'AG', 1,
|
||||
'Gulnar', 'Orazowa', 'Batyrowna', '1988-11-03', '+993 61 92 92 48',
|
||||
'I-AG', '654321',
|
||||
'rek1.jpg', 'rek2.jpg', 'rek3.jpg', 'rek4.jpg',
|
||||
'pending', 'Card details request'
|
||||
),
|
||||
(
|
||||
'CRO-2026-0002', 8, 3, '9860010011223344', '11', '2027',
|
||||
'BL', 4,
|
||||
'Maya', 'Halylowa', 'Saparowna', '1992-07-19', '+993 61 92 92 48',
|
||||
'I-BL', '445566',
|
||||
'rek1.jpg', 'rek2.jpg', 'rek3.jpg', 'rek4.jpg',
|
||||
'approved', 'Details issued'
|
||||
),
|
||||
(
|
||||
'CRO-2026-0003', NULL, 2, '4169740077001122', '01', '2030',
|
||||
'AH', 3,
|
||||
'Leyla', 'Garayeva', 'Meredowna', '1993-04-08', '+993 61 92 92 48',
|
||||
'I-AH', '778899',
|
||||
'rek1.jpg', 'rek2.jpg', 'rek3.jpg', 'rek4.jpg',
|
||||
'rejected', 'Expired card'
|
||||
);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Loan orders
|
||||
-- ---------------------------------------------------------------------------
|
||||
@@ -666,6 +709,19 @@ INSERT INTO visa_master_payment_orders (
|
||||
true
|
||||
);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Currency rates (Walýuta kursy)
|
||||
-- ---------------------------------------------------------------------------
|
||||
INSERT INTO currencies (currency_from, currency_to, value, created_by, updated_by) VALUES
|
||||
('EUR', 'TMT', 3.812, 2, 2),
|
||||
('USD', 'TMT', 3.5175, 2, 2),
|
||||
('EUR', 'RUB', 80.23, 2, 1),
|
||||
('USD', 'RUB', 74.0455, 2, 2),
|
||||
('RUB', 'TMT', 0.047504, 2, 2),
|
||||
('USD', 'USD', 250, 1, 1),
|
||||
('GBP', 'TMT', 4.45, 2, 2),
|
||||
('CNY', 'TMT', 0.485, 2, 3);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Activity logs
|
||||
-- ---------------------------------------------------------------------------
|
||||
@@ -701,11 +757,14 @@ UNION ALL SELECT 'loan_order_required_docs', count(*) FROM loan_order_required_d
|
||||
UNION ALL SELECT 'cards', count(*) FROM cards
|
||||
UNION ALL SELECT 'card_orders', count(*) FROM card_orders
|
||||
UNION ALL SELECT 'card_pin_orders', count(*) FROM card_pin_orders
|
||||
UNION ALL SELECT 'card_rekwizit_orders', count(*) FROM card_rekwizit_orders
|
||||
UNION ALL SELECT 'loan_orders', count(*) FROM loan_orders
|
||||
UNION ALL SELECT 'loans', count(*) FROM loans
|
||||
UNION ALL SELECT 'loan_paid_off_letters', count(*) FROM loan_paid_off_letters
|
||||
UNION ALL SELECT 'visa_master_payment_orders', count(*) FROM visa_master_payment_orders
|
||||
UNION ALL SELECT 'registration_verifications', count(*) FROM registration_verifications
|
||||
UNION ALL SELECT 'activity_logs', count(*) FROM activity_logs
|
||||
UNION ALL SELECT 'backups', count(*) FROM backups
|
||||
UNION ALL SELECT 'currencies', count(*) FROM currencies
|
||||
ORDER BY 1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user