This commit is contained in:
2026-03-23 18:55:27 +07:00
parent 6dc0322fe5
commit 3626c12319
47 changed files with 2741 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS user_roles;
DROP TABLE IF EXISTS roles;
DROP TABLE IF EXISTS users;

View File

@@ -0,0 +1,121 @@
CREATE EXTENSION IF NOT EXISTS postgis;
-- CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuidv7(),
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
google_id VARCHAR(255) UNIQUE,
auth_provider VARCHAR(50) NOT NULL DEFAULT 'local',
is_verified BOOLEAN NOT NULL DEFAULT false,
is_deleted BOOLEAN NOT NULL DEFAULT false,
token_version INT NOT NULL DEFAULT 1,
refresh_token TEXT,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE user_profiles (
user_id UUID PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
display_name TEXT,
full_name TEXT,
avatar_url TEXT,
bio TEXT,
location TEXT,
website TEXT,
country_code CHAR(2),
phone TEXT,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE user_verifications (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
verify_type SMALLINT NOT NULL, -- 1 = ID_CARD, 2 = EDUCATION, 3 = EXPERT
document_url TEXT NOT NULL,
status SMALLINT NOT NULL DEFAULT 1, -- 1 pending, 2 approved, 3 rejected
reviewed_by UUID REFERENCES users(id),
reviewed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS roles (
id UUID PRIMARY KEY DEFAULT uuidv7(),
name TEXT UNIQUE NOT NULL,
is_deleted BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS user_roles (
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
role_id UUID REFERENCES roles(id) ON DELETE CASCADE,
PRIMARY KEY (user_id, role_id)
);
CREATE INDEX idx_users_active_created_at
ON users (created_at DESC)
WHERE is_deleted = false;
CREATE INDEX idx_users_email_active
ON users (email)
WHERE is_deleted = false;
CREATE INDEX idx_users_active_verified
ON users (is_active, is_verified)
WHERE is_deleted = false;
CREATE INDEX idx_user_roles_user_id
ON user_roles (user_id);
CREATE INDEX idx_user_roles_role_id
ON user_roles (role_id);
CREATE INDEX idx_roles_active
ON roles (name)
WHERE is_deleted = false;
INSERT INTO roles (name) VALUES
('USER'),
('ADMIN'),
('MOD'),
('HISTORIAN'),
('BANNED')
ON CONFLICT (name) DO NOTHING;
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at();
CREATE TABLE IF NOT EXISTS user_tokens (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR(255) NOT NULL UNIQUE,
token_type SMALLINT NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX idx_user_tokens_token
ON user_tokens(token);
CREATE INDEX idx_user_tokens_user_id
ON user_tokens(user_id);
CREATE INDEX idx_user_tokens_type
ON user_tokens(token_type);
CREATE INDEX idx_user_tokens_expires_at
ON user_tokens(expires_at);

61
db/query/roles.sql Normal file
View File

@@ -0,0 +1,61 @@
-- name: CreateRole :one
INSERT INTO roles (name)
VALUES ($1)
RETURNING *;
-- name: GetRoleByName :one
SELECT id, name, is_deleted, created_at, updated_at FROM roles
WHERE name = $1 AND is_deleted = false;
-- name: GetRoleByID :one
SELECT id, name, is_deleted, created_at, updated_at FROM roles
WHERE id = $1 AND is_deleted = false;
-- name: AddUserRole :exec
INSERT INTO user_roles (user_id, role_id)
SELECT $1, r.id
FROM roles r
WHERE r.name = $2
ON CONFLICT DO NOTHING;
-- name: RemoveUserRole :exec
DELETE FROM user_roles ur
USING roles r
WHERE ur.role_id = r.id
AND ur.user_id = $1
AND r.name = $2;
-- name: RemoveAllRolesFromUser :exec
DELETE FROM user_roles
WHERE user_id = $1;
-- name: RemoveAllUsersFromRole :exec
DELETE FROM user_roles
WHERE role_id = $1;
-- name: GetRoles :many
SELECT *
FROM roles
WHERE is_deleted = false;
-- name: UpdateRole :one
UPDATE roles
SET
name = $1,
updated_at = now()
WHERE id = $2 AND is_deleted = false
RETURNING *;
-- name: DeleteRole :exec
UPDATE roles
SET
is_deleted = true,
updated_at = now()
WHERE id = $1;
-- name: RestoreRole :exec
UPDATE roles
SET
is_deleted = false,
updated_at = now()
WHERE id = $1;

157
db/query/users.sql Normal file
View File

@@ -0,0 +1,157 @@
-- name: CreateUser :one
INSERT INTO users (
name,
email,
password_hash,
avatar_url
) VALUES (
$1, $2, $3, $4
)
RETURNING *;
-- name: UpdateUser :one
UPDATE users
SET
name = $1,
avatar_url = $2,
is_active = $3,
is_verified = $4,
updated_at = now()
WHERE users.id = $5 AND users.is_deleted = false
RETURNING
users.id,
users.name,
users.email,
users.password_hash,
users.avatar_url,
users.is_active,
users.is_verified,
users.token_version,
users.refresh_token,
users.is_deleted,
users.created_at,
users.updated_at,
(
SELECT COALESCE(json_agg(json_build_object('id', roles.id, 'name', roles.name)), '[]')::json
FROM user_roles
JOIN roles ON user_roles.role_id = roles.id
WHERE user_roles.user_id = users.id
) AS roles;
-- name: UpdateUserPassword :exec
UPDATE users
SET
password_hash = $2
WHERE id = $1
AND is_deleted = false;
-- name: UpdateUserRefreshToken :exec
UPDATE users
SET
refresh_token = $2
WHERE id = $1
AND is_deleted = false;
-- name: VerifyUser :exec
UPDATE users
SET
is_verified = true
WHERE id = $1
AND is_deleted = false;
-- name: DeleteUser :exec
UPDATE users
SET
is_deleted = true
WHERE id = $1;
-- name: RestoreUser :exec
UPDATE users
SET
is_deleted = false
WHERE id = $1;
-- name: ExistsUserByEmail :one
SELECT EXISTS (
SELECT 1 FROM users
WHERE email = $1
AND is_deleted = false
);
-- name: GetUsers :many
SELECT
u.id,
u.name,
u.email,
u.password_hash,
u.avatar_url,
u.is_active,
u.is_verified,
u.token_version,
u.refresh_token,
u.is_deleted,
u.created_at,
u.updated_at,
COALESCE(
json_agg(
json_build_object('id', r.id, 'name', r.name)
) FILTER (WHERE r.id IS NOT NULL),
'[]'
)::json AS roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.is_deleted = false
GROUP BY u.id;
-- name: GetUserByID :one
SELECT
u.id,
u.name,
u.email,
u.password_hash,
u.avatar_url,
u.is_active,
u.is_verified,
u.token_version,
u.refresh_token,
u.is_deleted,
u.created_at,
u.updated_at,
COALESCE(
json_agg(
json_build_object('id', r.id, 'name', r.name)
) FILTER (WHERE r.id IS NOT NULL),
'[]'
)::json AS roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.id = $1 AND u.is_deleted = false
GROUP BY u.id;
-- name: GetUserByEmail :one
SELECT
u.id,
u.name,
u.email,
u.password_hash,
u.avatar_url,
u.is_active,
u.is_verified,
u.token_version,
u.is_deleted,
u.created_at,
u.updated_at,
COALESCE(
json_agg(
json_build_object('id', r.id, 'name', r.name)
) FILTER (WHERE r.id IS NOT NULL),
'[]'
)::json AS roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.email = $1 AND u.is_deleted = false
GROUP BY u.id;

61
db/schema.sql Normal file
View File

@@ -0,0 +1,61 @@
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuidv7(),
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
google_id VARCHAR(255) UNIQUE,
auth_provider VARCHAR(50) NOT NULL DEFAULT 'local',
is_verified BOOLEAN DEFAULT false,
token_version INT NOT NULL DEFAULT 1,
refresh_token TEXT,
is_deleted BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE user_profiles (
user_id UUID PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
display_name TEXT,
full_name TEXT,
avatar_url TEXT,
bio TEXT,
location TEXT,
website TEXT,
country_code CHAR(2),
phone TEXT,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE user_verifications (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
verify_type SMALLINT NOT NULL, -- 1 = ID_CARD, 2 = EDUCATION, 3 = EXPERT
document_url TEXT NOT NULL,
status SMALLINT NOT NULL DEFAULT 1, -- 1 pending, 2 approved, 3 rejected
reviewed_by UUID REFERENCES users(id),
reviewed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS roles (
id UUID PRIMARY KEY DEFAULT uuidv7(),
name TEXT UNIQUE NOT NULL,
is_deleted BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS user_roles (
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
role_id UUID REFERENCES roles(id) ON DELETE CASCADE,
PRIMARY KEY (user_id, role_id)
);
CREATE TABLE IF NOT EXISTS user_tokens (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR(255) NOT NULL UNIQUE, --1 = PasswordReset, 2 = EmailVerify, 3 = MagicLink, 4 = RefreshToken
token_type SMALLINT NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);