- auth: self-registration (role=user); first-created-user becomes super_admin - roles: super_admin may promote others; regular admin cannot see super_admin accounts - products: user-created private groups; admin 'สินค้าขององค์กร' (shared) with hidden/public; users can create groups - analytics: team + per-user weak areas, close-rate-by-difficulty buckets, 30-day default, weekly trend, trainee table, active users; dashboard redesigned - files: docx + xlsx upload support (python-docx + openpyxl) - ui: tabs การฝึก→ผลการฝึก→ภาพรวม; admin lands on ภาพรวม / user on การฝึก; guide in topbar - consolidate: weak-areas merged into Results (10/page), my-personas merged into Training - copy: บุคคลต้นแบบ→persona everywhere; clearer add-product form (A/B/C, upload-or-fill) - report: remove ดูรายงาน UI entry (endpoint kept) Backend 348 tests pass; frontend build + vitest clean.
253 lines
9.2 KiB
Python
253 lines
9.2 KiB
Python
"""Relational entities for the S4.2 persistence foundation.
|
|
|
|
The JSON stores remain the active runtime stores until the repository migration
|
|
packets are completed. These models define the tenant-safe relational contract
|
|
without wiring a cutover implicitly.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from sqlalchemy import (
|
|
JSON,
|
|
BigInteger,
|
|
Boolean,
|
|
CheckConstraint,
|
|
DateTime,
|
|
ForeignKey,
|
|
ForeignKeyConstraint,
|
|
Index,
|
|
Integer,
|
|
String,
|
|
Text,
|
|
UniqueConstraint,
|
|
false,
|
|
func,
|
|
true,
|
|
text,
|
|
)
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from ..db import Base
|
|
|
|
|
|
class Organization(Base):
|
|
__tablename__ = "organizations"
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
plan: Mapped[str] = mapped_column(
|
|
String(64), nullable=False, default="trial", server_default=text("'trial'")
|
|
)
|
|
seats: Mapped[int] = mapped_column(Integer, nullable=False, default=5, server_default=text("5"))
|
|
active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=true())
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
__table_args__ = (
|
|
UniqueConstraint("username", name="uq_users_username"),
|
|
UniqueConstraint("email", name="uq_users_email"),
|
|
UniqueConstraint("org_id", "id", name="uq_users_org_id_id"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
org_id: Mapped[str] = mapped_column(
|
|
String(64), ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
username: Mapped[str] = mapped_column(String(160), nullable=False)
|
|
password_hash: Mapped[str] = mapped_column(String(512), nullable=False)
|
|
email: Mapped[str | None] = mapped_column(String(320))
|
|
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
role: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=true())
|
|
must_setup: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())
|
|
accepted_terms: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())
|
|
accepted_terms_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
auth_version: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text("0"))
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class Group(Base):
|
|
__tablename__ = "groups"
|
|
__table_args__ = (
|
|
UniqueConstraint("org_id", "id", name="uq_groups_org_id_id"),
|
|
ForeignKeyConstraint(
|
|
["org_id", "owner_user_id"],
|
|
["users.org_id", "users.id"],
|
|
name="fk_groups_owner_same_org",
|
|
ondelete="RESTRICT",
|
|
),
|
|
ForeignKeyConstraint(
|
|
["org_id", "creator_user_id"],
|
|
["users.org_id", "users.id"],
|
|
name="fk_groups_creator_same_org",
|
|
ondelete="RESTRICT",
|
|
),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
org_id: Mapped[str] = mapped_column(
|
|
String(64), ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
owner_user_id: Mapped[str | None] = mapped_column(String(64))
|
|
creator_user_id: Mapped[str | None] = mapped_column(String(64))
|
|
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
visibility: Mapped[str] = mapped_column(
|
|
String(16), nullable=False, default="public", server_default=text("'public'")
|
|
)
|
|
status: Mapped[str] = mapped_column(
|
|
String(32), nullable=False, default="draft", server_default=text("'draft'")
|
|
)
|
|
input_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
|
sales_kit_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
|
report_text: Mapped[str | None] = mapped_column(Text)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class Persona(Base):
|
|
__tablename__ = "personas"
|
|
__table_args__ = (
|
|
UniqueConstraint("group_id", "id", name="uq_personas_group_id_id"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
group_id: Mapped[str] = mapped_column(
|
|
String(64), ForeignKey("groups.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
source_persona_id: Mapped[str | None] = mapped_column(String(64))
|
|
tier: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
public_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
|
latent_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class TrainingSession(Base):
|
|
__tablename__ = "sessions"
|
|
__table_args__ = (
|
|
ForeignKeyConstraint(
|
|
["org_id", "user_id"],
|
|
["users.org_id", "users.id"],
|
|
name="fk_sessions_user_same_org",
|
|
ondelete="RESTRICT",
|
|
),
|
|
ForeignKeyConstraint(
|
|
["org_id", "group_id"],
|
|
["groups.org_id", "groups.id"],
|
|
name="fk_sessions_group_same_org",
|
|
ondelete="RESTRICT",
|
|
),
|
|
ForeignKeyConstraint(
|
|
["group_id", "persona_id"],
|
|
["personas.group_id", "personas.id"],
|
|
name="fk_sessions_persona_in_group",
|
|
ondelete="RESTRICT",
|
|
),
|
|
Index(
|
|
"uq_sessions_non_preview_attempt",
|
|
"org_id",
|
|
"user_id",
|
|
"group_id",
|
|
"persona_id",
|
|
unique=True,
|
|
sqlite_where=text("mode <> 'preview'"),
|
|
postgresql_where=text("mode <> 'preview'"),
|
|
),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
org_id: Mapped[str] = mapped_column(
|
|
String(64), ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
user_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
group_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
persona_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
mode: Mapped[str] = mapped_column(
|
|
String(32), nullable=False, default="trainee", server_default=text("'trainee'")
|
|
)
|
|
status: Mapped[str] = mapped_column(
|
|
String(32), nullable=False, default="active", server_default=text("'active'")
|
|
)
|
|
outcome: Mapped[str | None] = mapped_column(String(32))
|
|
scenario_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
|
internal_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
|
debrief_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class Message(Base):
|
|
__tablename__ = "messages"
|
|
__table_args__ = (
|
|
UniqueConstraint("session_id", "sequence", name="uq_messages_session_sequence"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
session_id: Mapped[str] = mapped_column(
|
|
String(64), ForeignKey("sessions.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
sequence: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
role: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
text: Mapped[str] = mapped_column(Text, nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class AuditEvent(Base):
|
|
__tablename__ = "audit_events"
|
|
__table_args__ = (
|
|
CheckConstraint(
|
|
"actor_user_id IS NULL OR org_id IS NOT NULL",
|
|
name="ck_audit_actor_requires_org",
|
|
),
|
|
ForeignKeyConstraint(
|
|
["org_id", "actor_user_id"],
|
|
["users.org_id", "users.id"],
|
|
name="fk_audit_actor_same_org",
|
|
ondelete="SET NULL",
|
|
),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(
|
|
BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True
|
|
)
|
|
org_id: Mapped[str | None] = mapped_column(
|
|
String(64), ForeignKey("organizations.id", ondelete="SET NULL")
|
|
)
|
|
actor_user_id: Mapped[str | None] = mapped_column(String(64))
|
|
action: Mapped[str] = mapped_column(String(160), nullable=False)
|
|
subject: Mapped[str | None] = mapped_column(String(200))
|
|
detail_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
__all__ = [
|
|
"AuditEvent",
|
|
"Group",
|
|
"Message",
|
|
"Organization",
|
|
"Persona",
|
|
"TrainingSession",
|
|
"User",
|
|
]
|