diff --git a/alembic/versions/0036_add_message_author_admin_user_id.py b/alembic/versions/0036_add_message_author_admin_user_id.py new file mode 100644 index 0000000..13af732 --- /dev/null +++ b/alembic/versions/0036_add_message_author_admin_user_id.py @@ -0,0 +1,50 @@ +"""add author admin user id to messages + +Revision ID: 0036_message_author_admin_id +Revises: 0035_workspace_perf_indexes +Create Date: 2026-03-17 +""" + +from __future__ import annotations + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + + +revision = "0036_message_author_admin_id" +down_revision = "0035_workspace_perf_indexes" +branch_labels = None +depends_on = None + + +def _has_column(inspector: sa.Inspector, table: str, column_name: str) -> bool: + return any(str(column.get("name")) == column_name for column in inspector.get_columns(table)) + + +def _has_index(inspector: sa.Inspector, table: str, index_name: str) -> bool: + return any(str(index.get("name")) == index_name for index in inspector.get_indexes(table)) + + +def upgrade() -> None: + bind = op.get_bind() + inspector = sa.inspect(bind) + + if not _has_column(inspector, "messages", "author_admin_user_id"): + op.add_column("messages", sa.Column("author_admin_user_id", postgresql.UUID(as_uuid=True), nullable=True)) + + inspector = sa.inspect(bind) + if not _has_index(inspector, "messages", "ix_messages_author_admin_user_id"): + op.create_index("ix_messages_author_admin_user_id", "messages", ["author_admin_user_id"], unique=False) + + +def downgrade() -> None: + bind = op.get_bind() + inspector = sa.inspect(bind) + + if _has_index(inspector, "messages", "ix_messages_author_admin_user_id"): + op.drop_index("ix_messages_author_admin_user_id", table_name="messages") + + inspector = sa.inspect(bind) + if _has_column(inspector, "messages", "author_admin_user_id"): + op.drop_column("messages", "author_admin_user_id") diff --git a/app/models/message.py b/app/models/message.py index 99d646d..10d17e3 100644 --- a/app/models/message.py +++ b/app/models/message.py @@ -16,6 +16,7 @@ class Message(Base, UUIDMixin, TimestampMixin): ) request_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), index=True, nullable=False) author_type: Mapped[str] = mapped_column(String(20), nullable=False) # CLIENT|LAWYER|SYSTEM + author_admin_user_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True, index=True) author_name: Mapped[str | None] = mapped_column(String(200), nullable=True) body: Mapped[str | None] = mapped_column(Text, nullable=True) immutable: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) diff --git a/app/services/chat_secure_service.py b/app/services/chat_secure_service.py index b7ffdba..e7d9967 100644 --- a/app/services/chat_secure_service.py +++ b/app/services/chat_secure_service.py @@ -225,6 +225,7 @@ def serialize_message(row: Message, *, body: str | None = None, body_loaded: boo "id": str(row.id), "request_id": str(row.request_id), "author_type": row.author_type, + "author_admin_user_id": str(row.author_admin_user_id) if row.author_admin_user_id else None, "author_name": row.author_name, "body": body, "body_loaded": bool(body_loaded), @@ -281,6 +282,16 @@ def _normalize_admin_uuid(value: str | None) -> str | None: return None +def _normalize_admin_uuid_value(value: str | None) -> uuid.UUID | None: + normalized = _normalize_admin_uuid(value) + if not normalized: + return None + try: + return uuid.UUID(normalized) + except (TypeError, ValueError): + return None + + def _register_chat_participant(request: Request, admin_user_id: str | None) -> None: normalized = _normalize_admin_uuid(admin_user_id) if not normalized: @@ -525,6 +536,7 @@ def create_admin_or_lawyer_message( row = Message( request_id=request.id, author_type=author_type, + author_admin_user_id=_normalize_admin_uuid_value(actor_admin_user_id), author_name=str(actor_name or "").strip() or author_type, body=message_body, responsible=responsible, diff --git a/app/web/admin.html b/app/web/admin.html index 7993b5a..acc99c0 100644 --- a/app/web/admin.html +++ b/app/web/admin.html @@ -5,12 +5,12 @@