73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
"""SQLAlchemy models for order persistence."""
|
|
|
|
from datetime import datetime
|
|
from typing import Any
|
|
from uuid import UUID, uuid4
|
|
|
|
from sqlalchemy import BigInteger, DateTime, Integer, String, UniqueConstraint, Uuid, func
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
from sqlalchemy.types import JSON
|
|
|
|
|
|
def _json_payload_type() -> JSON:
|
|
return JSON().with_variant(JSONB, "postgresql")
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
class Order(Base):
|
|
__tablename__ = "orders"
|
|
__table_args__ = (
|
|
UniqueConstraint("order_uuid", name="uq_orders_order_uuid"),
|
|
)
|
|
|
|
id: Mapped[UUID] = mapped_column(
|
|
Uuid(as_uuid=True),
|
|
primary_key=True,
|
|
default=uuid4,
|
|
)
|
|
order_uuid: Mapped[str] = mapped_column(String(128), nullable=False)
|
|
payment_url: Mapped[str] = mapped_column(String(2048), nullable=False)
|
|
price: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
delivery_type: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
tariff_code: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
sender: Mapped[dict[str, Any]] = mapped_column(_json_payload_type(), nullable=False)
|
|
recipient: Mapped[dict[str, Any]] = mapped_column(
|
|
_json_payload_type(),
|
|
nullable=False,
|
|
)
|
|
from_location: Mapped[dict[str, Any]] = mapped_column(
|
|
_json_payload_type(),
|
|
nullable=False,
|
|
)
|
|
to_location: Mapped[dict[str, Any]] = mapped_column(
|
|
_json_payload_type(),
|
|
nullable=False,
|
|
)
|
|
packages: Mapped[list[dict[str, Any]]] = mapped_column(
|
|
_json_payload_type(),
|
|
nullable=False,
|
|
)
|
|
services: Mapped[list[dict[str, Any]] | None] = mapped_column(
|
|
_json_payload_type(),
|
|
nullable=True,
|
|
)
|
|
comment: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
|
payment_status: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
tbank_payment_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
|
|
cdek_order_uuid: Mapped[str | None] = mapped_column(String(128), 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,
|
|
)
|