Files
rentman-clone/backend/app/models/crew.py
T

89 lines
3.1 KiB
Python
Raw Normal View History

"""Crew and CrewAvailability models for managing personnel."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, ForeignKey, func, Float, Text, Boolean
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class Crew(Base):
"""Represents a crew member within a tenant account."""
__tablename__ = "crew"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
account_id: Mapped[str] = mapped_column(
String(36), ForeignKey("accounts.id", ondelete="CASCADE"), nullable=False
)
first_name: Mapped[str] = mapped_column(String(100), nullable=False)
last_name: Mapped[str] = mapped_column(String(100), nullable=False)
email: Mapped[str | None] = mapped_column(String(255), nullable=True)
phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
role_title: Mapped[str | None] = mapped_column(String(100), nullable=True)
hourly_rate: Mapped[float | None] = mapped_column(Float, nullable=True)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
notes: Mapped[str | None] = mapped_column(Text, 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,
)
# Relationships
account: Mapped["Account"] = relationship("Account", back_populates="crew_members")
availabilities: Mapped[list["CrewAvailability"]] = relationship(
"CrewAvailability", back_populates="crew_member", cascade="all, delete-orphan"
)
def __repr__(self) -> str:
return f"<Crew {self.first_name} {self.last_name}>"
class CrewAvailability(Base):
"""Tracks crew availability time periods."""
__tablename__ = "crew_availabilities"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
crew_id: Mapped[str] = mapped_column(
String(36), ForeignKey("crew.id", ondelete="CASCADE"), nullable=False
)
start_date: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
end_date: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
status: Mapped[str] = mapped_column(
String(20), nullable=False, default="available"
) # available, booked, unavailable
notes: Mapped[str | None] = mapped_column(Text, 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,
)
# Relationships
crew_member: Mapped["Crew"] = relationship("Crew", back_populates="availabilities")
def __repr__(self) -> str:
return f"<CrewAvailability {self.crew_id}: {self.start_date}-{self.end_date}>"