"""Domain registration ORM model — see migration 100_domain_registrations.""" from __future__ import annotations import uuid from datetime import datetime from sqlalchemy import DateTime, ForeignKey, String, Text, func from sqlalchemy.orm import Mapped, mapped_column from app.core.database import Base DOMAIN_REG_STATUS_PENDING = "pending" DOMAIN_REG_STATUS_APPROVED = "approved" DOMAIN_REG_STATUS_REJECTED = "rejected" DOMAIN_REG_STATUS_CANCELLED = "cancelled" DOMAIN_REG_STATUS_REVOKED = "revoked" class DomainRegistration(Base): """An employee-owned root domain registration record. Pattern mirrors :class:`SsoGrant` — request lands in pending, Feishu approval flips to approved (and stamps ``granted_at``), and the leaver flow demands a handover before the requester can be marked off. """ __tablename__ = "domain_registrations" id: Mapped[str] = mapped_column( String(36), primary_key=True, default=lambda: str(uuid.uuid4()), ) person_id: Mapped[str] = mapped_column( String(36), ForeignKey("identity_persons.id"), nullable=False, ) domain_name: Mapped[str] = mapped_column(String(253), nullable=False) purpose: Mapped[str] = mapped_column(Text, nullable=False) status: Mapped[str] = mapped_column( String(32), nullable=False, default=DOMAIN_REG_STATUS_PENDING, ) feishu_instance_code: Mapped[str | None] = mapped_column( String(64), nullable=True, ) approval_request_id: Mapped[str | None] = mapped_column( String(36), ForeignKey("approval_requests.id", ondelete="SET NULL"), nullable=True, ) granted_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True, ) revoked_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True, ) revoke_reason: Mapped[str | None] = mapped_column(String(255), nullable=True) handover_from_person_id: Mapped[str | None] = mapped_column( String(36), ForeignKey("identity_persons.id", ondelete="SET NULL"), nullable=True, ) handover_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True, ) handover_actor_id: Mapped[str | None] = mapped_column( String(36), ForeignKey("identity_persons.id", ondelete="SET NULL"), nullable=True, ) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False, ) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False, )