import uuid from datetime import datetime from sqlalchemy import DateTime, Integer, String, Text from sqlalchemy.dialects.postgresql import JSONB, UUID from sqlalchemy.orm import Mapped, mapped_column from app.core.db import Base from app.models.mixins import TimestampMixin DELIVERY_STATUSES = ("pending", "delivering", "succeeded", "failed") class OutboxEvent(TimestampMixin, Base): __tablename__ = "outbox_events" event_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) event_type: Mapped[str] = mapped_column(String(60), nullable=False) aggregate_type: Mapped[str] = mapped_column(String(30), nullable=False) aggregate_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False) payload_json: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict) occurred_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) delivery_status: Mapped[str] = mapped_column(String(20), nullable=False, default="pending") attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0) next_attempt_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) last_error: Mapped[str | None] = mapped_column(Text) # Stable, localizable classification of last_error -- the frontend renders a # localized summary from this code as the primary text and shows last_error itself # only under "Technical details" (section 10 of docs/fleet-ops-correction/ # current-gap-audit.md). Kept alongside the raw message for backward compatibility. last_error_code: Mapped[str | None] = mapped_column(String(60)) external_run_id: Mapped[str | None] = mapped_column(String(120))