Files
backend/migrations/00008_create_complaints_and_blocks_tables.sql
2025-12-13 22:34:01 +05:00

111 lines
4.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- +goose Up
-- +goose StatementBegin
-- =========================================
-- ENUM: complaint_status - Статусы жалобы
-- =========================================
CREATE TYPE complaint_status AS ENUM (
'pending', -- Ожидает рассмотрения
'in_review', -- На рассмотрении модератором
'resolved', -- Разрешена
'rejected' -- Отклонена
);
COMMENT ON TYPE complaint_status IS 'Статусы жизненного цикла жалобы';
-- =========================================
-- ENUM: complaint_type - Типы жалоб
-- =========================================
CREATE TYPE complaint_type AS ENUM (
'inappropriate_behavior', -- Неподобающее поведение
'no_show', -- Не явился
'fraud', -- Мошенничество
'spam', -- Спам
'other' -- Другое
);
COMMENT ON TYPE complaint_type IS 'Типы жалоб на пользователей';
-- =========================================
-- ТАБЛИЦА: complaints - Жалобы
-- =========================================
CREATE TABLE complaints (
id BIGSERIAL PRIMARY KEY,
-- Связи
complainant_id BIGINT NOT NULL REFERENCES users(id), -- Кто жалуется
defendant_id BIGINT NOT NULL REFERENCES users(id), -- На кого жалуются
request_id BIGINT REFERENCES requests(id), -- Связанная заявка (опционально)
-- Содержание жалобы
type complaint_type NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
-- Статус и обработка
status complaint_status DEFAULT 'pending',
moderator_id BIGINT REFERENCES users(id),
moderator_comment TEXT,
resolved_at TIMESTAMP WITH TIME ZONE,
-- Аудит
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Комментарии
COMMENT ON TABLE complaints IS 'Жалобы пользователей друг на друга';
COMMENT ON COLUMN complaints.complainant_id IS 'Пользователь, подающий жалобу';
COMMENT ON COLUMN complaints.defendant_id IS 'Пользователь, на которого жалуются';
-- Индексы
CREATE INDEX idx_complaints_defendant_id ON complaints(defendant_id);
CREATE INDEX idx_complaints_complainant_id ON complaints(complainant_id);
CREATE INDEX idx_complaints_status ON complaints(status);
CREATE INDEX idx_complaints_type ON complaints(type);
CREATE INDEX idx_complaints_moderator_id ON complaints(moderator_id) WHERE moderator_id IS NOT NULL;
-- =========================================
-- ТАБЛИЦА: user_blocks - Блокировки пользователей
-- =========================================
CREATE TABLE user_blocks (
id BIGSERIAL PRIMARY KEY,
-- Связи
user_id BIGINT NOT NULL REFERENCES users(id),
blocked_by BIGINT NOT NULL REFERENCES users(id),
complaint_id BIGINT REFERENCES complaints(id), -- Связанная жалоба (опционально)
-- Детали блокировки
reason TEXT NOT NULL,
blocked_until TIMESTAMP WITH TIME ZONE, -- NULL = бессрочная блокировка
-- Статус
is_active BOOLEAN DEFAULT TRUE,
-- Аудит
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
unblocked_at TIMESTAMP WITH TIME ZONE,
unblocked_by BIGINT REFERENCES users(id)
);
-- Комментарии
COMMENT ON TABLE user_blocks IS 'Блокировки пользователей модераторами';
COMMENT ON COLUMN user_blocks.blocked_until IS 'Дата окончания блокировки (NULL = бессрочная)';
COMMENT ON COLUMN user_blocks.is_active IS 'Активна ли блокировка в данный момент';
-- Индексы
CREATE INDEX idx_user_blocks_user_id_active ON user_blocks(user_id, is_active) WHERE is_active = TRUE;
CREATE INDEX idx_user_blocks_blocked_by ON user_blocks(blocked_by);
CREATE INDEX idx_user_blocks_created_at ON user_blocks(created_at DESC);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS user_blocks CASCADE;
DROP TABLE IF EXISTS complaints CASCADE;
DROP TYPE IF EXISTS complaint_type CASCADE;
DROP TYPE IF EXISTS complaint_status CASCADE;
-- +goose StatementEnd