Files
celtic-life-science/app/organization/models.py
T
Shubhankar45 0d30c58bf0 Server backup 2026-11-07 of uncommitted work in projects/pharma-web-celtic
Snapshot of the working tree on srv1071960 (branch main), taken during
server cleanup. .env files, node_modules, builds, logs and media were excluded.
2026-11-07 05:43:12 +00:00

88 lines
3.3 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 Business 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 Business Manager currently manages which headquarters.
`effective_to IS NULL` means "current". A headquarters may have several Area Business Managers
and an Area Business Manager several headquarters; the partial unique index only stops the same
pair being active twice.
"""
__tablename__ = "am_headquarters"
__table_args__ = (
Index(
"uq_active_am_hq_pair",
"headquarters_id",
"area_manager_user_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