53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
"""Chemist master data and chemist → MR assignment (same shape as doctors)."""
|
|
|
|
import uuid
|
|
from datetime import date
|
|
|
|
from sqlalchemy import Boolean, 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 Chemist(Base, UUIDMixin, TimestampMixin):
|
|
__tablename__ = "chemist"
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"headquarters_id", "name", "shop_address", name="uq_chemist_hq_name_shop"
|
|
),
|
|
)
|
|
|
|
headquarters_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("headquarters.id", ondelete="RESTRICT"), nullable=False
|
|
)
|
|
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
shop_address: Mapped[str] = mapped_column(String(500), nullable=False)
|
|
contact_number: Mapped[str | None] = mapped_column(String(20))
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
|
|
|
assignments: Mapped[list["ChemistAssignment"]] = relationship(back_populates="chemist")
|
|
|
|
|
|
class ChemistAssignment(Base, UUIDMixin, TimestampMixin):
|
|
__tablename__ = "chemist_assignment"
|
|
__table_args__ = (
|
|
Index(
|
|
"uq_active_chemist_assignment",
|
|
"chemist_id",
|
|
unique=True,
|
|
postgresql_where=text("effective_to IS NULL"),
|
|
),
|
|
)
|
|
|
|
chemist_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("chemist.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
mr_user_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="RESTRICT"), nullable=False
|
|
)
|
|
effective_from: Mapped[date] = mapped_column(Date, nullable=False)
|
|
effective_to: Mapped[date | None] = mapped_column(Date)
|
|
|
|
chemist: Mapped["Chemist"] = relationship(back_populates="assignments")
|