86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
"""Company, Headquarters and the Area-Manager ↔ Headquarters mapping.
|
|
|
|
Design notes (report §13.2):
|
|
- `area_territory` was merged into `headquarters` — the brief states HQ *is* the
|
|
territory, and two tables for one concept invites drift.
|
|
- An MR's Area Manager is DERIVED through the headquarters, not stored on the MR.
|
|
An HQ handover therefore re-points every MR in that HQ with no data migration.
|
|
"""
|
|
|
|
import uuid
|
|
from datetime import date
|
|
|
|
from sqlalchemy import Date, ForeignKey, Index, String, UniqueConstraint, text
|
|
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database.base import Base, TimestampMixin, UUIDMixin
|
|
|
|
|
|
class Company(Base, UUIDMixin, TimestampMixin):
|
|
"""Tenant root. Single company in the MVP (A17), but `company_id` is carried on
|
|
every table so multi-tenancy needs no migration later."""
|
|
|
|
__tablename__ = "company"
|
|
|
|
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
address: Mapped[str | None] = mapped_column(String(500))
|
|
is_active: Mapped[bool] = mapped_column(default=True, nullable=False)
|
|
|
|
headquarters: Mapped[list["Headquarters"]] = relationship(back_populates="company")
|
|
|
|
|
|
class Headquarters(Base, UUIDMixin, TimestampMixin):
|
|
"""A territory/location. NOT a login role (R8)."""
|
|
|
|
__tablename__ = "headquarters"
|
|
__table_args__ = (
|
|
UniqueConstraint("company_id", "code", name="uq_hq_company_code"),
|
|
)
|
|
|
|
company_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("company.id", ondelete="RESTRICT"), nullable=False
|
|
)
|
|
code: Mapped[str] = mapped_column(String(20), nullable=False)
|
|
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
district: Mapped[str | None] = mapped_column(String(100))
|
|
state: Mapped[str | None] = mapped_column(String(100))
|
|
is_active: Mapped[bool] = mapped_column(default=True, nullable=False)
|
|
|
|
company: Mapped["Company"] = relationship(back_populates="headquarters")
|
|
am_mappings: Mapped[list["AMHeadquarters"]] = relationship(
|
|
back_populates="headquarters"
|
|
)
|
|
|
|
|
|
class AMHeadquarters(Base, UUIDMixin, TimestampMixin):
|
|
"""Which Area Manager currently manages which headquarters.
|
|
|
|
`effective_to IS NULL` means "current". A partial unique index guarantees at most
|
|
one active Area Manager per headquarters at any time.
|
|
"""
|
|
|
|
__tablename__ = "am_headquarters"
|
|
__table_args__ = (
|
|
Index(
|
|
"uq_active_am_per_hq",
|
|
"headquarters_id",
|
|
unique=True,
|
|
postgresql_where=text("effective_to IS NULL"),
|
|
),
|
|
)
|
|
|
|
area_manager_user_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="RESTRICT"), nullable=False
|
|
)
|
|
headquarters_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True),
|
|
ForeignKey("headquarters.id", ondelete="RESTRICT"),
|
|
nullable=False,
|
|
)
|
|
effective_from: Mapped[date] = mapped_column(Date, nullable=False)
|
|
effective_to: Mapped[date | None] = mapped_column(Date)
|
|
|
|
headquarters: Mapped["Headquarters"] = relationship(back_populates="am_mappings")
|
|
area_manager: Mapped["User"] = relationship() # noqa: F821
|