Files
MobilityOps/backend/app/models/booking.py
T

46 lines
1.9 KiB
Python

import uuid
from datetime import datetime
from sqlalchemy import Boolean, CheckConstraint, DateTime, ForeignKey, Index, Integer, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.core.db import Base
from app.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin
BOOKING_STATUSES = ("reserved", "active", "returned", "cancelled", "blocked")
class Booking(UUIDPrimaryKeyMixin, TimestampMixin, Base):
__tablename__ = "bookings"
__table_args__ = (
CheckConstraint(
"status IN ('reserved','active','returned','cancelled','blocked')",
name="ck_bookings_status",
),
CheckConstraint("ends_at > starts_at", name="ck_bookings_time_window"),
CheckConstraint(
"start_odometer_km IS NULL OR start_odometer_km >= 0",
name="ck_bookings_start_odometer",
),
CheckConstraint(
"end_odometer_km IS NULL OR end_odometer_km >= 0",
name="ck_bookings_end_odometer",
),
Index("ix_bookings_vehicle_status_window", "vehicle_id", "status", "starts_at", "ends_at"),
)
public_ref: Mapped[str] = mapped_column(String(20), unique=True, nullable=False)
customer_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("customers.id"), nullable=False
)
vehicle_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("vehicles.id"), nullable=False
)
starts_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
ends_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
status: Mapped[str] = mapped_column(String(20), nullable=False)
start_odometer_km: Mapped[int | None] = mapped_column(Integer)
end_odometer_km: Mapped[int | None] = mapped_column(Integer)
requirements_complete: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)