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:
@@ -16,7 +16,8 @@ from routers.deps import require_manager
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
EXPORT_VERSION = 1
|
||||
EXPORT_VERSION = 1 # tables bundle
|
||||
CATALOG_EXPORT_VERSION = 2 # catalogue bundle: v2 = products listed in several categories (D-23)
|
||||
|
||||
|
||||
def _serialize_product(p: Product) -> dict:
|
||||
@@ -62,7 +63,12 @@ def _serialize_product(p: Product) -> dict:
|
||||
|
||||
|
||||
def _serialize_category(cat: Category) -> dict:
|
||||
products = [_serialize_product(p) for p in cat.products if p.lifecycle_status != "archived"]
|
||||
# Every product LISTED in this category (D-23), in its order there. A product listed in
|
||||
# several categories appears in each; "primary" marks its main one.
|
||||
listings = sorted(cat.memberships, key=lambda m: (m.sort_order, m.product_id))
|
||||
products = [{**_serialize_product(m.product), "primary": m.product.primary_category_id == cat.id,
|
||||
"category_sort_order": m.sort_order}
|
||||
for m in listings if m.product.lifecycle_status != "archived"]
|
||||
return {
|
||||
"name": cat.name, "color": cat.color, "sort_order": cat.sort_order,
|
||||
"parent_name": cat.parent.name if cat.parent else None,
|
||||
@@ -76,7 +82,7 @@ def export_catalog(db: Session = Depends(get_db), user: User = Depends(require_m
|
||||
categories = db.query(Category).order_by(Category.sort_order).all()
|
||||
orphan_products = (
|
||||
db.query(Product)
|
||||
.filter(Product.category_id == None, Product.lifecycle_status != "archived")
|
||||
.filter(~Product.memberships.any(), Product.lifecycle_status != "archived")
|
||||
.order_by(Product.sort_order)
|
||||
.all()
|
||||
)
|
||||
@@ -85,7 +91,7 @@ def export_catalog(db: Session = Depends(get_db), user: User = Depends(require_m
|
||||
"uncategorized_products": [_serialize_product(p) for p in orphan_products],
|
||||
}
|
||||
payload = {
|
||||
"xenia_export_version": EXPORT_VERSION,
|
||||
"xenia_export_version": CATALOG_EXPORT_VERSION,
|
||||
"bundle": "catalog",
|
||||
"exported_at": utcnow().isoformat(),
|
||||
"data": data,
|
||||
@@ -139,7 +145,7 @@ def export_tables(db: Session = Depends(get_db), user: User = Depends(require_ma
|
||||
def import_catalog(payload: dict, db: Session = Depends(get_db), user: User = Depends(require_manager)):
|
||||
if payload.get("bundle") != "catalog":
|
||||
raise HTTPException(status_code=400, detail="Wrong bundle type. Expected 'catalog'.")
|
||||
if payload.get("xenia_export_version", 0) != EXPORT_VERSION:
|
||||
if payload.get("xenia_export_version", 0) != CATALOG_EXPORT_VERSION:
|
||||
raise HTTPException(status_code=400, detail="Unsupported export version.")
|
||||
|
||||
data = payload.get("data", {})
|
||||
@@ -170,14 +176,13 @@ def import_catalog(payload: dict, db: Session = Depends(get_db), user: User = De
|
||||
db.flush()
|
||||
return cat
|
||||
|
||||
def _upsert_product(prod_data: dict, category_id=None):
|
||||
def _upsert_product(prod_data: dict):
|
||||
existing = db.query(Product).filter(Product.name == prod_data["name"]).first()
|
||||
if existing:
|
||||
existing.base_price = prod_data["base_price"]
|
||||
existing.is_available = prod_data.get("is_available", True)
|
||||
existing.lifecycle_status = prod_data.get("lifecycle_status", "active")
|
||||
existing.sort_order = prod_data.get("sort_order", 0)
|
||||
existing.category_id = category_id
|
||||
db.flush()
|
||||
product = existing
|
||||
else:
|
||||
@@ -187,7 +192,6 @@ def import_catalog(payload: dict, db: Session = Depends(get_db), user: User = De
|
||||
is_available=prod_data.get("is_available", True),
|
||||
lifecycle_status=prod_data.get("lifecycle_status", "active"),
|
||||
sort_order=prod_data.get("sort_order", 0),
|
||||
category_id=category_id,
|
||||
)
|
||||
db.add(product)
|
||||
db.flush()
|
||||
@@ -242,12 +246,19 @@ def import_catalog(payload: dict, db: Session = Depends(get_db), user: User = De
|
||||
if idx is not None and 0 <= idx < len(created_choices):
|
||||
ps.default_choice_id = created_choices[idx].id
|
||||
|
||||
# Listings: product name -> [(category id, position, primary, data)]. Products are
|
||||
# upserted once (from their primary listing) after all categories exist (D-23).
|
||||
listings: dict = {}
|
||||
|
||||
def _collect(cat, cat_data):
|
||||
for i, prod_data in enumerate(cat_data.get("products", [])):
|
||||
listings.setdefault(prod_data["name"], []).append(
|
||||
(cat.id, prod_data.get("category_sort_order", i), bool(prod_data.get("primary")), prod_data))
|
||||
|
||||
# First pass: top-level categories (parent_name is None)
|
||||
for cat_data in categories_data:
|
||||
if cat_data.get("parent_name") is None:
|
||||
cat = _upsert_category(cat_data, parent_id=None)
|
||||
for prod_data in cat_data.get("products", []):
|
||||
_upsert_product(prod_data, category_id=cat.id)
|
||||
_collect(_upsert_category(cat_data, parent_id=None), cat_data)
|
||||
|
||||
# Second pass: sub-categories (parent must already exist from first pass)
|
||||
for cat_data in categories_data:
|
||||
@@ -257,13 +268,24 @@ def import_catalog(payload: dict, db: Session = Depends(get_db), user: User = De
|
||||
Category.parent_id == None,
|
||||
).first()
|
||||
parent_id = parent.id if parent else None
|
||||
cat = _upsert_category(cat_data, parent_id=parent_id)
|
||||
for prod_data in cat_data.get("products", []):
|
||||
_upsert_product(prod_data, category_id=cat.id)
|
||||
_collect(_upsert_category(cat_data, parent_id=parent_id), cat_data)
|
||||
|
||||
from models.product import ProductCategory
|
||||
from services.catalog import set_categories
|
||||
for name, rows in listings.items():
|
||||
primary = next((r for r in rows if r[2]), rows[0])
|
||||
_upsert_product(primary[3])
|
||||
product = db.query(Product).filter(Product.name == name).first()
|
||||
set_categories(db, product, [r[0] for r in rows], primary[0])
|
||||
db.flush()
|
||||
for cat_id, pos, _p, _d in rows:
|
||||
db.get(ProductCategory, (product.id, cat_id)).sort_order = pos
|
||||
|
||||
# Uncategorized products (no category)
|
||||
for prod_data in data.get("uncategorized_products", []):
|
||||
_upsert_product(prod_data, category_id=None)
|
||||
_upsert_product(prod_data)
|
||||
product = db.query(Product).filter(Product.name == prod_data["name"]).first()
|
||||
set_categories(db, product, [], None)
|
||||
|
||||
db.commit()
|
||||
return {"ok": True, "message": "Catalog imported successfully."}
|
||||
|
||||
Reference in New Issue
Block a user