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:
@@ -8,7 +8,7 @@ from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from database import get_db
|
||||
from models.product import Product, Category, ProductOption, ProductQuickOption, ProductIngredient, ProductPreferenceSet, ProductPreferenceChoice, ProductModifierGroup
|
||||
from models.product import Product, Category, ProductCategory, ProductOption, ProductQuickOption, ProductIngredient, ProductPreferenceSet, ProductPreferenceChoice, ProductModifierGroup
|
||||
from models.prep_zone import PrepZone
|
||||
from models.order import OrderItem
|
||||
from models.user import User
|
||||
@@ -253,6 +253,8 @@ def delete_category(category_id: int, db: Session = Depends(get_db), user: User
|
||||
cat = db.query(Category).filter(Category.id == category_id).first()
|
||||
if not cat:
|
||||
raise HTTPException(status_code=404, detail="Category not found")
|
||||
from services.catalog import remove_category
|
||||
remove_category(db, category_id) # its listings go; products whose main category it was get their next one
|
||||
db.delete(cat)
|
||||
db.commit()
|
||||
_broadcast_products_changed()
|
||||
@@ -261,15 +263,39 @@ def delete_category(category_id: int, db: Session = Depends(get_db), user: User
|
||||
# ── Products ──────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/", response_model=List[ProductOut])
|
||||
def list_products(all: bool = False, db: Session = Depends(get_db), user: User = Depends(get_current_user)):
|
||||
q = db.query(Product)
|
||||
def list_products(all: bool = False, category_id: int | None = None, db: Session = Depends(get_db),
|
||||
user: User = Depends(get_current_user)):
|
||||
"""Every product once, with `category_ids` / `categories` (D-23). With `category_id`:
|
||||
the products LISTED in that category (primary or not), in that category's order."""
|
||||
from sqlalchemy.orm import selectinload
|
||||
q = db.query(Product).options(selectinload(Product.memberships), selectinload(Product.prep_zones))
|
||||
has_dashboard = holds(user, "core.dashboard.access")
|
||||
if not all or not has_dashboard:
|
||||
# Waiters only see active, available products
|
||||
q = q.filter(Product.is_available == True, Product.lifecycle_status == "active")
|
||||
if category_id is not None:
|
||||
q = q.join(ProductCategory, ProductCategory.product_id == Product.id) \
|
||||
.filter(ProductCategory.category_id == category_id) \
|
||||
.order_by(ProductCategory.sort_order, Product.id)
|
||||
return q.all()
|
||||
return q.order_by(Product.sort_order, Product.id).all()
|
||||
|
||||
|
||||
def _apply_categories(db, product, body, creating: bool) -> None:
|
||||
"""category_ids / primary_category_id, or the scaffolding category_id (§3.3)."""
|
||||
from services.catalog import set_categories
|
||||
sent = body.model_fields_set
|
||||
if body.category_ids is not None:
|
||||
set_categories(db, product, body.category_ids, body.primary_category_id)
|
||||
elif body.primary_category_id is not None:
|
||||
set_categories(db, product, product.category_ids + [body.primary_category_id], body.primary_category_id)
|
||||
elif "category_id" in sent or (creating and body.category_id is not None):
|
||||
# current screens: "the product's category" = replace the primary
|
||||
rest = [c for c in product.category_ids if c != product.primary_category_id]
|
||||
new = body.category_id
|
||||
set_categories(db, product, ([new] if new else []) + rest, new)
|
||||
|
||||
|
||||
class BulkPrepZoneBody(BaseModel):
|
||||
product_ids: List[int]
|
||||
prep_zone_ids: List[int]
|
||||
@@ -289,6 +315,11 @@ def bulk_assign_prep_zones(body: BulkPrepZoneBody, db: Session = Depends(get_db)
|
||||
@router.put("/reorder", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def reorder_products(items: List[ProductReorderItem], db: Session = Depends(get_db), user: User = Depends(require_menu_manager)):
|
||||
for item in items:
|
||||
if item.category_id is not None:
|
||||
m = db.get(ProductCategory, (item.id, item.category_id))
|
||||
if m:
|
||||
m.sort_order = item.sort_order
|
||||
continue
|
||||
product = db.query(Product).filter(Product.id == item.id).first()
|
||||
if product:
|
||||
product.sort_order = item.sort_order
|
||||
@@ -298,7 +329,8 @@ def reorder_products(items: List[ProductReorderItem], db: Session = Depends(get_
|
||||
|
||||
@router.post("/", response_model=ProductOut, status_code=status.HTTP_201_CREATED)
|
||||
def create_product(body: ProductCreate, db: Session = Depends(get_db), user: User = Depends(require_menu_manager)):
|
||||
data = body.model_dump(exclude={"quick_options", "options", "ingredients", "preference_sets", "cost_breakdown", "prep_zone_ids", "tags"})
|
||||
data = body.model_dump(exclude={"quick_options", "options", "ingredients", "preference_sets", "cost_breakdown", "prep_zone_ids", "tags",
|
||||
"category_id", "primary_category_id", "category_ids"})
|
||||
if data.get("sort_order") == 0:
|
||||
data["sort_order"] = db.query(Product).count()
|
||||
if body.cost_breakdown is not None:
|
||||
@@ -307,6 +339,7 @@ def create_product(body: ProductCreate, db: Session = Depends(get_db), user: Use
|
||||
product = Product(**data)
|
||||
db.add(product)
|
||||
db.flush()
|
||||
_apply_categories(db, product, body, creating=True)
|
||||
# Assign prep zones
|
||||
if body.prep_zone_ids:
|
||||
zones = db.query(PrepZone).filter(PrepZone.id.in_(body.prep_zone_ids)).all()
|
||||
@@ -356,10 +389,12 @@ def update_product(product_id: int, body: ProductUpdate, db: Session = Depends(g
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
scalar_fields = body.model_dump(
|
||||
exclude_none=True,
|
||||
exclude={"quick_options", "options", "ingredients", "preference_sets", "cost_breakdown", "prep_zone_ids", "tags", "modifier_groups"},
|
||||
exclude={"quick_options", "options", "ingredients", "preference_sets", "cost_breakdown", "prep_zone_ids", "tags", "modifier_groups",
|
||||
"category_id", "primary_category_id", "category_ids"},
|
||||
)
|
||||
for field, value in scalar_fields.items():
|
||||
setattr(product, field, value)
|
||||
_apply_categories(db, product, body, creating=False)
|
||||
# Update prep zones if provided
|
||||
if body.prep_zone_ids is not None:
|
||||
zones = db.query(PrepZone).filter(PrepZone.id.in_(body.prep_zone_ids)).all()
|
||||
|
||||
Reference in New Issue
Block a user