feat(backend): a product can be listed in several categories, with one primary category

Platform v2 step 1.13 (APPS_PLATFORM D-23).
- product_categories(product_id, category_id, sort_order); products.category_id becomes
  products.primary_category_id (revision 0013 moves the data: one listing per product at its
  old position). services/catalog.py keeps the rule "the primary is one of its categories"
  and fixes up products when a category is deleted.
- Products API: category_ids + primary_category_id in and out, plus categories
  [{category_id, sort_order}]; GET /api/products/?category_id= lists a category's products in
  that category's order; PUT /reorder with category_id orders within a category. category_id
  stays as dev scaffolding (section 3.3): output = primary, input = "move the primary".
- Primary category: ticket grouping and prep-zone category order, the order-item category
  snapshot (1.12), report filters for waste-only rows and /meta/products.
- Every listing: price modifier / deal / min-quantity category targets, deal free choices,
  the QR menu snapshot (product in each category it's listed in; images pushed once).
- Catalogue export v2: each category lists all its products with "primary" and
  "category_sort_order"; import restores listings and order. v1 files are refused (D-21).
- New errors catalog.unknown_category, catalog.primary_not_listed.
This commit is contained in:
2026-11-13 12:39:02 +02:00
parent 3c5751025c
commit 6d42be95f8
20 changed files with 445 additions and 56 deletions
+6 -5
View File
@@ -35,7 +35,7 @@ class OrderContext:
"""Snapshot of order-level facts needed to evaluate conditions."""
channel: str # 'pos' | 'online' | 'qr' | 'takeaway'
now: datetime # current UTC time
cart_items: list # list of {product_id, category_id, quantity, unit_price}
cart_items: list # list of {product_id, category_id (primary), category_ids, quantity, unit_price}
active_price_group_ids: set # set of PriceGroup.id that are currently active
user_tier: Optional[str] = None # 'bronze'|'silver'|'gold' — placeholder
@@ -124,7 +124,7 @@ def _eval_condition(ctype: str, params: dict, ctx: OrderContext,
cat_id = params.get("category_id")
cart_qty = sum(
ci["quantity"] for ci in ctx.cart_items
if ci.get("category_id") == cat_id
if cat_id in (ci.get("category_ids") or [ci.get("category_id")])
)
passed = cart_qty >= params.get("min", 1)
return passed, {"min_category_qty": cart_qty}
@@ -269,7 +269,7 @@ def _modifier_targets_product(modifier, product, db: Session) -> bool:
return True
if t.target_type == "category":
ids = _parse_ids(t.target_ids) or ([t.target_id] if t.target_id else [])
if product.category_id in ids:
if any(c in ids for c in product.category_ids): # listed in a target category (D-23)
return True
if t.target_type == "prep_zone":
ids = _parse_ids(t.target_ids) or ([t.target_id] if t.target_id else [])
@@ -414,7 +414,7 @@ def _deal_targets_met(deal, ctx: OrderContext, db: Session) -> bool:
return True
if target.target_type == "category":
ids = _parse_ids(target.target_ids) or ([target.target_id] if target.target_id else [])
if any(ci.get("category_id") in ids for ci in ctx.cart_items):
if any(c in ids for ci in ctx.cart_items for c in (ci.get("category_ids") or [ci.get("category_id")])):
return True
if target.target_type == "tag":
tags = _parse_ids(target.target_tags) or ([target.target_tag] if target.target_tag else [])
@@ -483,8 +483,9 @@ def evaluate_deals(
prods = db.query(Product).filter(Product.id.in_(target_ids)).all()
offer.free_choices = [{"id": p.id, "name": p.name} for p in prods]
elif deal.action_free_target_type == "category":
from models.product import ProductCategory
prods = db.query(Product).filter(
Product.category_id.in_(target_ids),
Product.id.in_(db.query(ProductCategory.product_id).filter(ProductCategory.category_id.in_(target_ids))),
Product.lifecycle_status == "active",
Product.is_available == True, # noqa: E712
).all()