feat: the menu snapshot carries a price per sales point; pulled orders keep the cloud's sales point and table (platform-v2 3S, S.2)
- services/cloud_sync.menu_snapshot(db) (split out of the push, tested): sold_at + prices per customer-app sales point from the pricing engine; digital_price = the QR menu's. - store_online_orders: sales_point from the cloud (older clouds: from the type), takeaway orders typed takeaway, the QR menu's table in front of the customer's note. - manager e2e: waits for the live connection (html[data-live]) instead of a fixed 1.5 s, and for the server to record "read" (two timing races seen in a full run). - Tests: +2 in test_sales_points.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -249,65 +249,64 @@ async def _push_product_images(db, products):
|
||||
logger.warning("Image push failed for product %d: %s", p.id, e)
|
||||
|
||||
|
||||
def menu_snapshot(db) -> tuple[dict, list]:
|
||||
"""The customer apps' menu (contract C-2): top-level categories with every product listed
|
||||
in them (D-23) that is sold at a customer-app sales point (phase 3S), each with a price
|
||||
per such sales point from the pricing engine at this moment (`prices`, what the cloud
|
||||
charges since S.2), `digital_price` = the QR menu's (older clouds) and its quick options.
|
||||
→ ({"categories": [...]}, the products once each)"""
|
||||
from models.product import Category, Product, ProductCategory
|
||||
from services.pos_cart import unit_prices
|
||||
categories = db.query(Category).filter(Category.parent_id == None).all() # noqa: E711
|
||||
payload_categories = []
|
||||
all_products = []
|
||||
for cat in categories:
|
||||
products = (
|
||||
db.query(Product)
|
||||
.join(ProductCategory, ProductCategory.product_id == Product.id)
|
||||
.filter(ProductCategory.category_id == cat.id, Product.lifecycle_status == "active")
|
||||
.order_by(ProductCategory.sort_order, Product.id)
|
||||
.all()
|
||||
)
|
||||
products = [p for p in products if set(sales_points.sold_at(p)) & set(sales_points.DIGITAL)]
|
||||
by_point = {sp: unit_prices(db, products, sp) for sp in sales_points.DIGITAL}
|
||||
all_products.extend(p for p in products if p not in all_products) # once, even if listed twice
|
||||
product_list = []
|
||||
for i, p in enumerate(products):
|
||||
sold = [sp for sp in sales_points.sold_at(p) if sp in sales_points.DIGITAL]
|
||||
product_list.append({
|
||||
"id": p.id,
|
||||
"name": p.name,
|
||||
"digital_name": p.digital_name,
|
||||
"digital_description": p.digital_description,
|
||||
"digital_price": round(by_point[sales_points.QR_TABLE][i], 2),
|
||||
"base_price": p.base_price,
|
||||
"digital_discount": 0,
|
||||
"sold_at": sold,
|
||||
"prices": {sp: round(by_point[sp][i], 2) for sp in sold},
|
||||
"digital_available": bool(p.digital_available),
|
||||
"digital_image_url": p.digital_image_url,
|
||||
"image_url": p.image_url,
|
||||
"quick_options": [{"id": o.id, "name": o.name, "price": o.price} for o in p.quick_options],
|
||||
})
|
||||
if product_list:
|
||||
payload_categories.append({"id": cat.id, "name": cat.name, "sort_order": cat.sort_order,
|
||||
"products": product_list})
|
||||
return {"categories": payload_categories}, all_products
|
||||
|
||||
|
||||
async def _push_menu_snapshot():
|
||||
"""Serialize all digital-visible products+categories and POST to cloud."""
|
||||
"""Build the customer apps' menu (menu_snapshot) and POST it to the cloud."""
|
||||
if not settings.SITE_ID or not settings.CLOUD_URL:
|
||||
return
|
||||
|
||||
try:
|
||||
from database import SessionLocal
|
||||
from models.product import Category, Product, ProductCategory
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
categories = db.query(Category).filter(Category.parent_id == None).all()
|
||||
payload_categories = []
|
||||
all_products = []
|
||||
for cat in categories:
|
||||
# every product LISTED in this category, in its order there (D-23)
|
||||
products = (
|
||||
db.query(Product)
|
||||
.join(ProductCategory, ProductCategory.product_id == Product.id)
|
||||
.filter(
|
||||
ProductCategory.category_id == cat.id,
|
||||
Product.lifecycle_status == "active",
|
||||
)
|
||||
.order_by(ProductCategory.sort_order, Product.id)
|
||||
.all()
|
||||
)
|
||||
# sold by the customer apps (phase 3S); priced by the engine for the QR menu
|
||||
# (S.2 sends a price per sales point)
|
||||
products = [p for p in products if set(sales_points.sold_at(p)) & set(sales_points.DIGITAL)]
|
||||
from services.pos_cart import unit_prices
|
||||
qr_prices = unit_prices(db, products, sales_points.QR_TABLE)
|
||||
all_products.extend(p for p in products if p not in all_products) # once, even if listed twice
|
||||
product_list = []
|
||||
for p, qr_price in zip(products, qr_prices):
|
||||
product_list.append({
|
||||
"id": p.id,
|
||||
"name": p.name,
|
||||
"digital_name": p.digital_name,
|
||||
"digital_description": p.digital_description,
|
||||
"digital_price": round(qr_price, 2),
|
||||
"base_price": p.base_price,
|
||||
"digital_discount": 0,
|
||||
"sold_at": [sp for sp in sales_points.sold_at(p) if sp in sales_points.DIGITAL],
|
||||
"digital_available": bool(p.digital_available),
|
||||
"digital_image_url": p.digital_image_url,
|
||||
"image_url": p.image_url,
|
||||
"quick_options": [
|
||||
{"id": o.id, "name": o.name, "price": o.price}
|
||||
for o in p.quick_options
|
||||
],
|
||||
})
|
||||
if product_list:
|
||||
payload_categories.append({
|
||||
"id": cat.id,
|
||||
"name": cat.name,
|
||||
"sort_order": cat.sort_order,
|
||||
"products": product_list,
|
||||
})
|
||||
|
||||
snapshot, all_products = menu_snapshot(db)
|
||||
payload_categories = snapshot["categories"]
|
||||
snapshot_json = json.dumps({"categories": payload_categories})
|
||||
|
||||
# Resolve numeric site_id from license state (set by heartbeat response)
|
||||
@@ -333,6 +332,16 @@ async def _push_menu_snapshot():
|
||||
logger.warning("Menu snapshot push failed: %s", e)
|
||||
|
||||
|
||||
def _with_table(cloud_order: dict):
|
||||
"""The customer's note, with the QR menu's table in front (S.2; S.7 puts the order on the
|
||||
table itself)."""
|
||||
table = (cloud_order.get("table_label") or "").strip()
|
||||
notes = (cloud_order.get("customer_notes") or "").strip()
|
||||
if not table:
|
||||
return notes or None
|
||||
return f"Table {table}" + (f" · {notes}" if notes else "")
|
||||
|
||||
|
||||
def store_online_orders(db, orders: list[dict]) -> dict:
|
||||
"""Store online orders as the cloud hands them over (GET /api/orders/pending/{site}):
|
||||
one local order each, waiting for acceptance (plan step 3.3d). An order already stored
|
||||
@@ -358,8 +367,11 @@ def store_online_orders(db, orders: list[dict]) -> dict:
|
||||
opened_by=opener_id,
|
||||
status="open",
|
||||
source="online",
|
||||
order_type="delivery" if cloud_order.get("order_type") == "delivery" else "here",
|
||||
sales_point=sales_points.for_order(None, "online", cloud_order.get("order_type")),
|
||||
order_type="delivery" if cloud_order.get("order_type") == "delivery" else (
|
||||
"takeaway" if cloud_order.get("order_type") == "takeaway" else "here"),
|
||||
# the cloud says where it was sold since S.2 (older clouds: from the order type)
|
||||
sales_point=(cloud_order.get("sales_point") if cloud_order.get("sales_point") in sales_points.DIGITAL
|
||||
else sales_points.for_order(None, "online", cloud_order.get("order_type"))),
|
||||
business_day_id=day.id if day else None,
|
||||
online_order_ref=cloud_order["public_ref"],
|
||||
online_order_cloud_id=cloud_order["id"],
|
||||
@@ -367,7 +379,7 @@ def store_online_orders(db, orders: list[dict]) -> dict:
|
||||
online_customer_name=cloud_order.get("customer_name"),
|
||||
online_customer_phone=cloud_order.get("customer_phone"),
|
||||
online_customer_address=cloud_order.get("customer_address"),
|
||||
online_customer_notes=cloud_order.get("customer_notes"),
|
||||
online_customer_notes=_with_table(cloud_order),
|
||||
online_order_type=cloud_order.get("order_type"),
|
||||
)
|
||||
db.add(local_order)
|
||||
|
||||
Reference in New Issue
Block a user