40 lines
1.8 KiB
SQL
40 lines
1.8 KiB
SQL
-- AI-тренажёр (CLT-7). Тексты переписки в БД не хранятся — только в шифрованном хранилище загрузок.
|
|
CREATE TABLE trainer_seats (
|
|
user_id uuid PRIMARY KEY REFERENCES users (id) ON DELETE CASCADE,
|
|
org_id uuid NOT NULL REFERENCES organizations (id) ON DELETE CASCADE,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX trainer_seats_org_id_idx ON trainer_seats (org_id);
|
|
|
|
-- Одноразовые приглашения в Telegram: только SHA-256 кода.
|
|
CREATE TABLE trainer_invites (
|
|
code_hash bytea PRIMARY KEY,
|
|
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
|
org_id uuid NOT NULL REFERENCES organizations (id) ON DELETE CASCADE,
|
|
expires_at timestamptz NOT NULL,
|
|
used_at timestamptz,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- Привязка Telegram-аккаунта (только числовой id, без имени и username).
|
|
CREATE TABLE trainer_telegram_links (
|
|
user_id uuid PRIMARY KEY REFERENCES users (id) ON DELETE CASCADE,
|
|
telegram_user_id bigint NOT NULL UNIQUE,
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE trainer_sessions (
|
|
id uuid PRIMARY KEY,
|
|
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
|
org_id uuid NOT NULL REFERENCES organizations (id) ON DELETE CASCADE,
|
|
scenario_id text NOT NULL,
|
|
status text NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'finished', 'abandoned')),
|
|
turns integer NOT NULL DEFAULT 0,
|
|
scores jsonb,
|
|
transcript_upload_id uuid,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
finished_at timestamptz
|
|
);
|
|
CREATE UNIQUE INDEX trainer_sessions_one_active_idx ON trainer_sessions (user_id) WHERE status = 'active';
|
|
CREATE INDEX trainer_sessions_org_idx ON trainer_sessions (org_id, status);
|