diff --git a/cloud_backend/migrations/versions/0004_order_area_id.py b/cloud_backend/migrations/versions/0004_order_area_id.py new file mode 100644 index 0000000..93712a2 --- /dev/null +++ b/cloud_backend/migrations/versions/0004_order_area_id.py @@ -0,0 +1,27 @@ +"""online orders: delivery_area_id (plan phase 3S, S.5) + +The site checks the cloud's delivery fee with its own engine when it pulls an order, and +needs the area by id (a name can change between a menu push and the order). + +Revision ID: 0004_order_area_id +Revises: 0003_order_sales_point +Create Date: 2026-11-16 18:00:00 +""" +from alembic import op +import sqlalchemy as sa + + +revision = '0004_order_area_id' +down_revision = '0003_order_sales_point' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + with op.batch_alter_table('online_orders', schema=None) as batch_op: + batch_op.add_column(sa.Column('delivery_area_id', sa.Integer(), nullable=True)) + + +def downgrade() -> None: + with op.batch_alter_table('online_orders', schema=None) as batch_op: + batch_op.drop_column('delivery_area_id') diff --git a/cloud_backend/models/online_order.py b/cloud_backend/models/online_order.py index 68c5d33..4dd1f73 100644 --- a/cloud_backend/models/online_order.py +++ b/cloud_backend/models/online_order.py @@ -26,6 +26,7 @@ class OnlineOrder(Base): customer_notes = Column(Text, nullable=True) table_label = Column(String, nullable=True) # the QR menu's table, as the customer gave it delivery_area = Column(String, nullable=True) # the delivery area's name (S.3) + delivery_area_id = Column(Integer, nullable=True) # the site's area id (S.5: the site checks the fee) requested_for = Column(DateTime(timezone=True), nullable=True) # the time slot picked (S.4); null = asap sales_point = Column(String, nullable=True) # the site's sales point (phase 3S, S.2) diff --git a/cloud_backend/routers/orders.py b/cloud_backend/routers/orders.py index 555b089..abce640 100644 --- a/cloud_backend/routers/orders.py +++ b/cloud_backend/routers/orders.py @@ -122,6 +122,7 @@ def create_order(site_slug: str, body: OnlineOrderCreate, request: Request, db: customer_notes=body.customer_notes, table_label=(body.customer_table or "").strip() or None, delivery_area=priced.area_name, + delivery_area_id=priced.area_id, # stored as UTC: SQLite keeps no time zone (read back naive = UTC, schemas mark it) requested_for=(body.requested_for.astimezone(timezone.utc).replace(tzinfo=None) if body.order_type in ("takeaway", "delivery") and body.requested_for else None), diff --git a/cloud_backend/schemas/online_order.py b/cloud_backend/schemas/online_order.py index 103c7be..f496f42 100644 --- a/cloud_backend/schemas/online_order.py +++ b/cloud_backend/schemas/online_order.py @@ -83,6 +83,7 @@ class PendingOrderOut(BaseModel): customer_notes: Optional[str] = None table_label: Optional[str] = None # the QR menu's table (S.2) delivery_area: Optional[str] = None # the delivery area's name (S.3) + delivery_area_id: Optional[int] = None # the site's area id (S.5) requested_for: Optional[datetime] = None # when the customer wants it (S.4); None = as soon as possible sales_point: Optional[str] = None # the site's sales point (S.2) items_json: str diff --git a/cloud_backend/tests/test_fees.py b/cloud_backend/tests/test_fees.py index 0d885fa..b8f9021 100644 --- a/cloud_backend/tests/test_fees.py +++ b/cloud_backend/tests/test_fees.py @@ -57,7 +57,7 @@ def test_the_cloud_charges_the_venues_fee(client, admin_h): ok = _order(client, site, "delivery", customer_address="Main sq. 3", area_id=1, total=7.6) assert ok.status_code == 201, ok.text stored = next(o for o in client.get(f"/api/orders/pending/{numeric}", headers=_h(site)).json() if o["public_ref"] == ok.json()["public_ref"]) - assert (stored["delivery_fee"], stored["total"], stored["delivery_area"]) == (2.0, 7.6, "Centre") + assert (stored["delivery_fee"], stored["total"], stored["delivery_area"], stored["delivery_area_id"]) == (2.0, 7.6, "Centre", 1) # takeaway: the delivery rules don't apply assert client.post(f"/api/orders/{site['site_id']}/quote", json={"order_type": "takeaway", "items": [{"product_id": COLA, "quantity": 1}]}).json()["delivery_fee"] == 0