refactor(status): introduce OrderStatus enum, replace string literals
Centralized order status values in api/app/constants.py via a str-valued Enum so comparisons keep working. Replaced literals in: - services: sync_service, sqlite_service, retry_service - routers: sync, dashboard - templates: dashboard.html, logs.html - static JS: shared (ORDER_STATUS mirror), dashboard, logs - tests: requirements, order_items_overwrite, business_rules MALFORMED intentionally NOT added — introduced in follow-up PR2 (per-order failure isolation). Full test suite: 231 unit + 33 e2e pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,9 +4,11 @@ from fastapi.responses import HTMLResponse
|
||||
from pathlib import Path
|
||||
|
||||
from ..services import sqlite_service
|
||||
from ..constants import OrderStatus
|
||||
|
||||
router = APIRouter()
|
||||
templates = Jinja2Templates(directory=str(Path(__file__).parent.parent / "templates"))
|
||||
templates.env.globals["OrderStatus"] = OrderStatus
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
async def dashboard(request: Request):
|
||||
|
||||
@@ -14,9 +14,11 @@ from typing import Optional
|
||||
|
||||
from ..services import sync_service, scheduler_service, sqlite_service, invoice_service
|
||||
from .. import database
|
||||
from ..constants import OrderStatus
|
||||
|
||||
router = APIRouter(tags=["sync"])
|
||||
templates = Jinja2Templates(directory=str(Path(__file__).parent.parent / "templates"))
|
||||
templates.env.globals["OrderStatus"] = OrderStatus
|
||||
|
||||
|
||||
async def _enrich_items_with_codmat(items: list) -> None:
|
||||
@@ -231,13 +233,13 @@ def _format_text_log_from_detail(detail: dict) -> str:
|
||||
customer = o.get("customer_name", "?")
|
||||
order_date = o.get("order_date") or "?"
|
||||
|
||||
if status == "IMPORTED":
|
||||
if status == OrderStatus.IMPORTED.value:
|
||||
id_cmd = o.get("id_comanda", "?")
|
||||
lines.append(f"#{number} [{order_date}] {customer} → IMPORTAT (ID: {id_cmd})")
|
||||
elif status == "ALREADY_IMPORTED":
|
||||
elif status == OrderStatus.ALREADY_IMPORTED.value:
|
||||
id_cmd = o.get("id_comanda", "?")
|
||||
lines.append(f"#{number} [{order_date}] {customer} → DEJA IMPORTAT (ID: {id_cmd})")
|
||||
elif status == "SKIPPED":
|
||||
elif status == OrderStatus.SKIPPED.value:
|
||||
missing = o.get("missing_skus", "")
|
||||
if isinstance(missing, str):
|
||||
try:
|
||||
@@ -246,7 +248,7 @@ def _format_text_log_from_detail(detail: dict) -> str:
|
||||
missing = [missing] if missing else []
|
||||
skus_str = ", ".join(missing) if isinstance(missing, list) else str(missing)
|
||||
lines.append(f"#{number} [{order_date}] {customer} → OMIS (lipsa: {skus_str})")
|
||||
elif status == "ERROR":
|
||||
elif status == OrderStatus.ERROR.value:
|
||||
err = o.get("error_message", "necunoscuta")
|
||||
lines.append(f"#{number} [{order_date}] {customer} → EROARE: {err}")
|
||||
|
||||
@@ -618,7 +620,7 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
is_invoiced_filter = (status == "INVOICED")
|
||||
|
||||
# For UNINVOICED/INVOICED: fetch all IMPORTED orders, then filter post-invoice-check
|
||||
fetch_status = "IMPORTED" if (is_uninvoiced_filter or is_invoiced_filter) else status
|
||||
fetch_status = OrderStatus.IMPORTED.value if (is_uninvoiced_filter or is_invoiced_filter) else status
|
||||
fetch_per_page = 10000 if (is_uninvoiced_filter or is_invoiced_filter) else per_page
|
||||
fetch_page = 1 if (is_uninvoiced_filter or is_invoiced_filter) else page
|
||||
|
||||
@@ -687,7 +689,7 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
newly_invoiced = sum(1 for o in uncached_orders if o.get("invoice") and o["invoice"].get("facturat"))
|
||||
uninvoiced_base = counts.get("uninvoiced_sqlite", sum(
|
||||
1 for o in all_orders
|
||||
if o.get("status") in ("IMPORTED", "ALREADY_IMPORTED") and not o.get("invoice")
|
||||
if o.get("status") in (OrderStatus.IMPORTED.value, OrderStatus.ALREADY_IMPORTED.value) and not o.get("invoice")
|
||||
))
|
||||
counts["nefacturate"] = max(0, uninvoiced_base - newly_invoiced)
|
||||
imported_total = counts.get("imported_all") or counts.get("imported", 0)
|
||||
@@ -713,7 +715,7 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
|
||||
# For UNINVOICED filter: apply server-side filtering + pagination
|
||||
if is_uninvoiced_filter:
|
||||
filtered = [o for o in all_orders if o.get("status") in ("IMPORTED", "ALREADY_IMPORTED") and not o.get("invoice")]
|
||||
filtered = [o for o in all_orders if o.get("status") in (OrderStatus.IMPORTED.value, OrderStatus.ALREADY_IMPORTED.value) and not o.get("invoice")]
|
||||
total = len(filtered)
|
||||
offset = (page - 1) * per_page
|
||||
result["orders"] = filtered[offset:offset + per_page]
|
||||
@@ -722,7 +724,7 @@ async def dashboard_orders(page: int = 1, per_page: int = 50,
|
||||
result["per_page"] = per_page
|
||||
result["pages"] = (total + per_page - 1) // per_page if total > 0 else 0
|
||||
elif is_invoiced_filter:
|
||||
filtered = [o for o in all_orders if o.get("status") in ("IMPORTED", "ALREADY_IMPORTED") and o.get("invoice")]
|
||||
filtered = [o for o in all_orders if o.get("status") in (OrderStatus.IMPORTED.value, OrderStatus.ALREADY_IMPORTED.value) and o.get("invoice")]
|
||||
total = len(filtered)
|
||||
offset = (page - 1) * per_page
|
||||
result["orders"] = filtered[offset:offset + per_page]
|
||||
|
||||
Reference in New Issue
Block a user