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:
@@ -3,6 +3,7 @@ import logging
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
from ..database import get_sqlite, get_sqlite_sync
|
||||
from ..constants import OrderStatus
|
||||
|
||||
# Re-export so other services can import get_sqlite from sqlite_service
|
||||
__all__ = ["get_sqlite", "get_sqlite_sync"]
|
||||
@@ -68,7 +69,7 @@ async def upsert_order(sync_run_id: str, order_number: str, order_date: str,
|
||||
"""Upsert a single order — one row per order_number, status updated in place."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
await db.execute("""
|
||||
await db.execute(f"""
|
||||
INSERT INTO orders
|
||||
(order_number, order_date, customer_name, status,
|
||||
id_comanda, id_partener, error_message, missing_skus, items_count,
|
||||
@@ -79,7 +80,7 @@ async def upsert_order(sync_run_id: str, order_number: str, order_date: str,
|
||||
ON CONFLICT(order_number) DO UPDATE SET
|
||||
customer_name = excluded.customer_name,
|
||||
status = CASE
|
||||
WHEN orders.status = 'IMPORTED' AND excluded.status = 'ALREADY_IMPORTED'
|
||||
WHEN orders.status = '{OrderStatus.IMPORTED.value}' AND excluded.status = '{OrderStatus.ALREADY_IMPORTED.value}'
|
||||
THEN orders.status
|
||||
ELSE excluded.status
|
||||
END,
|
||||
@@ -88,7 +89,7 @@ async def upsert_order(sync_run_id: str, order_number: str, order_date: str,
|
||||
items_count = excluded.items_count,
|
||||
id_comanda = COALESCE(excluded.id_comanda, orders.id_comanda),
|
||||
id_partener = COALESCE(excluded.id_partener, orders.id_partener),
|
||||
times_skipped = CASE WHEN excluded.status = 'SKIPPED'
|
||||
times_skipped = CASE WHEN excluded.status = '{OrderStatus.SKIPPED.value}'
|
||||
THEN orders.times_skipped + 1
|
||||
ELSE orders.times_skipped END,
|
||||
last_sync_run_id = excluded.last_sync_run_id,
|
||||
@@ -140,7 +141,7 @@ async def save_orders_batch(orders_data: list[dict]):
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
# 1. Upsert orders
|
||||
await db.executemany("""
|
||||
await db.executemany(f"""
|
||||
INSERT INTO orders
|
||||
(order_number, order_date, customer_name, status,
|
||||
id_comanda, id_partener, error_message, missing_skus, items_count,
|
||||
@@ -151,7 +152,7 @@ async def save_orders_batch(orders_data: list[dict]):
|
||||
ON CONFLICT(order_number) DO UPDATE SET
|
||||
customer_name = excluded.customer_name,
|
||||
status = CASE
|
||||
WHEN orders.status = 'IMPORTED' AND excluded.status = 'ALREADY_IMPORTED'
|
||||
WHEN orders.status = '{OrderStatus.IMPORTED.value}' AND excluded.status = '{OrderStatus.ALREADY_IMPORTED.value}'
|
||||
THEN orders.status
|
||||
ELSE excluded.status
|
||||
END,
|
||||
@@ -160,7 +161,7 @@ async def save_orders_batch(orders_data: list[dict]):
|
||||
items_count = excluded.items_count,
|
||||
id_comanda = COALESCE(excluded.id_comanda, orders.id_comanda),
|
||||
id_partener = COALESCE(excluded.id_partener, orders.id_partener),
|
||||
times_skipped = CASE WHEN excluded.status = 'SKIPPED'
|
||||
times_skipped = CASE WHEN excluded.status = '{OrderStatus.SKIPPED.value}'
|
||||
THEN orders.times_skipped + 1
|
||||
ELSE orders.times_skipped END,
|
||||
last_sync_run_id = excluded.last_sync_run_id,
|
||||
@@ -400,17 +401,17 @@ async def get_dashboard_stats():
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute(
|
||||
"SELECT COUNT(*) FROM orders WHERE status = 'IMPORTED'"
|
||||
f"SELECT COUNT(*) FROM orders WHERE status = '{OrderStatus.IMPORTED.value}'"
|
||||
)
|
||||
imported = (await cursor.fetchone())[0]
|
||||
|
||||
cursor = await db.execute(
|
||||
"SELECT COUNT(*) FROM orders WHERE status = 'SKIPPED'"
|
||||
f"SELECT COUNT(*) FROM orders WHERE status = '{OrderStatus.SKIPPED.value}'"
|
||||
)
|
||||
skipped = (await cursor.fetchone())[0]
|
||||
|
||||
cursor = await db.execute(
|
||||
"SELECT COUNT(*) FROM orders WHERE status = 'ERROR'"
|
||||
f"SELECT COUNT(*) FROM orders WHERE status = '{OrderStatus.ERROR.value}'"
|
||||
)
|
||||
errors = (await cursor.fetchone())[0]
|
||||
|
||||
@@ -694,11 +695,11 @@ async def get_run_orders_filtered(run_id: str, status_filter: str = "all",
|
||||
"per_page": per_page,
|
||||
"pages": (total + per_page - 1) // per_page if total > 0 else 0,
|
||||
"counts": {
|
||||
"imported": status_counts.get("IMPORTED", 0),
|
||||
"skipped": status_counts.get("SKIPPED", 0),
|
||||
"error": status_counts.get("ERROR", 0),
|
||||
"already_imported": status_counts.get("ALREADY_IMPORTED", 0),
|
||||
"cancelled": status_counts.get("CANCELLED", 0),
|
||||
"imported": status_counts.get(OrderStatus.IMPORTED.value, 0),
|
||||
"skipped": status_counts.get(OrderStatus.SKIPPED.value, 0),
|
||||
"error": status_counts.get(OrderStatus.ERROR.value, 0),
|
||||
"already_imported": status_counts.get(OrderStatus.ALREADY_IMPORTED.value, 0),
|
||||
"cancelled": status_counts.get(OrderStatus.CANCELLED.value, 0),
|
||||
"total": sum(status_counts.values())
|
||||
}
|
||||
}
|
||||
@@ -738,8 +739,8 @@ async def get_orders(page: int = 1, per_page: int = 50,
|
||||
data_params = list(base_params)
|
||||
|
||||
if status_filter and status_filter not in ("all", "UNINVOICED"):
|
||||
if status_filter.upper() == "IMPORTED":
|
||||
data_clauses.append("UPPER(status) IN ('IMPORTED', 'ALREADY_IMPORTED')")
|
||||
if status_filter.upper() == OrderStatus.IMPORTED.value:
|
||||
data_clauses.append(f"UPPER(status) IN ('{OrderStatus.IMPORTED.value}', '{OrderStatus.ALREADY_IMPORTED.value}')")
|
||||
elif status_filter.upper() == "DIFFS":
|
||||
data_clauses.append(
|
||||
"(anaf_cod_fiscal_adjusted = 1 OR anaf_denumire_mismatch = 1"
|
||||
@@ -785,7 +786,7 @@ async def get_orders(page: int = 1, per_page: int = 50,
|
||||
|
||||
# Uninvoiced count: IMPORTED/ALREADY_IMPORTED with no cached invoice, same period+search
|
||||
uninv_clauses = list(base_clauses) + [
|
||||
"UPPER(status) IN ('IMPORTED', 'ALREADY_IMPORTED')",
|
||||
f"UPPER(status) IN ('{OrderStatus.IMPORTED.value}', '{OrderStatus.ALREADY_IMPORTED.value}')",
|
||||
"(factura_numar IS NULL OR factura_numar = '')",
|
||||
]
|
||||
uninv_where = "WHERE " + " AND ".join(uninv_clauses)
|
||||
@@ -794,7 +795,7 @@ async def get_orders(page: int = 1, per_page: int = 50,
|
||||
|
||||
# Uninvoiced > 3 days old
|
||||
uninv_old_clauses = list(base_clauses) + [
|
||||
"UPPER(status) IN ('IMPORTED', 'ALREADY_IMPORTED')",
|
||||
f"UPPER(status) IN ('{OrderStatus.IMPORTED.value}', '{OrderStatus.ALREADY_IMPORTED.value}')",
|
||||
"(factura_numar IS NULL OR factura_numar = '')",
|
||||
"order_date < datetime('now', '-3 days')",
|
||||
]
|
||||
@@ -828,12 +829,12 @@ async def get_orders(page: int = 1, per_page: int = 50,
|
||||
"per_page": per_page,
|
||||
"pages": (total + per_page - 1) // per_page if total > 0 else 0,
|
||||
"counts": {
|
||||
"imported": status_counts.get("IMPORTED", 0),
|
||||
"already_imported": status_counts.get("ALREADY_IMPORTED", 0),
|
||||
"imported_all": status_counts.get("IMPORTED", 0) + status_counts.get("ALREADY_IMPORTED", 0),
|
||||
"skipped": status_counts.get("SKIPPED", 0),
|
||||
"error": status_counts.get("ERROR", 0),
|
||||
"cancelled": status_counts.get("CANCELLED", 0),
|
||||
"imported": status_counts.get(OrderStatus.IMPORTED.value, 0),
|
||||
"already_imported": status_counts.get(OrderStatus.ALREADY_IMPORTED.value, 0),
|
||||
"imported_all": status_counts.get(OrderStatus.IMPORTED.value, 0) + status_counts.get(OrderStatus.ALREADY_IMPORTED.value, 0),
|
||||
"skipped": status_counts.get(OrderStatus.SKIPPED.value, 0),
|
||||
"error": status_counts.get(OrderStatus.ERROR.value, 0),
|
||||
"cancelled": status_counts.get(OrderStatus.CANCELLED.value, 0),
|
||||
"total": sum(status_counts.values()),
|
||||
"uninvoiced_sqlite": uninvoiced_sqlite,
|
||||
"uninvoiced_old": uninvoiced_old,
|
||||
@@ -869,9 +870,9 @@ async def get_uninvoiced_imported_orders() -> list:
|
||||
"""Get all imported orders that don't yet have invoice data cached."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute("""
|
||||
cursor = await db.execute(f"""
|
||||
SELECT order_number, id_comanda FROM orders
|
||||
WHERE status IN ('IMPORTED', 'ALREADY_IMPORTED')
|
||||
WHERE status IN ('{OrderStatus.IMPORTED.value}', '{OrderStatus.ALREADY_IMPORTED.value}')
|
||||
AND id_comanda IS NOT NULL
|
||||
AND factura_numar IS NULL
|
||||
""")
|
||||
@@ -923,9 +924,9 @@ async def get_invoiced_imported_orders() -> list:
|
||||
"""Get imported orders that HAVE cached invoice data (for re-verification)."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute("""
|
||||
cursor = await db.execute(f"""
|
||||
SELECT order_number, id_comanda FROM orders
|
||||
WHERE status IN ('IMPORTED', 'ALREADY_IMPORTED')
|
||||
WHERE status IN ('{OrderStatus.IMPORTED.value}', '{OrderStatus.ALREADY_IMPORTED.value}')
|
||||
AND id_comanda IS NOT NULL
|
||||
AND factura_numar IS NOT NULL AND factura_numar != ''
|
||||
""")
|
||||
@@ -939,9 +940,9 @@ async def get_all_imported_orders() -> list:
|
||||
"""Get ALL imported orders with id_comanda (for checking if deleted in ROA)."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute("""
|
||||
cursor = await db.execute(f"""
|
||||
SELECT order_number, id_comanda FROM orders
|
||||
WHERE status IN ('IMPORTED', 'ALREADY_IMPORTED')
|
||||
WHERE status IN ('{OrderStatus.IMPORTED.value}', '{OrderStatus.ALREADY_IMPORTED.value}')
|
||||
AND id_comanda IS NOT NULL
|
||||
""")
|
||||
rows = await cursor.fetchall()
|
||||
@@ -976,9 +977,9 @@ async def mark_order_deleted_in_roa(order_number: str):
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
await db.execute("DELETE FROM order_items WHERE order_number = ?", (order_number,))
|
||||
await db.execute("""
|
||||
await db.execute(f"""
|
||||
UPDATE orders SET
|
||||
status = 'DELETED_IN_ROA',
|
||||
status = '{OrderStatus.DELETED_IN_ROA.value}',
|
||||
id_comanda = NULL,
|
||||
id_partener = NULL,
|
||||
factura_serie = NULL,
|
||||
@@ -1001,9 +1002,9 @@ async def mark_order_cancelled(order_number: str, web_status: str = "Anulata"):
|
||||
"""Mark an order as cancelled from GoMag. Clears id_comanda and invoice cache."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
await db.execute("""
|
||||
await db.execute(f"""
|
||||
UPDATE orders SET
|
||||
status = 'CANCELLED',
|
||||
status = '{OrderStatus.CANCELLED.value}',
|
||||
id_comanda = NULL,
|
||||
id_partener = NULL,
|
||||
factura_serie = NULL,
|
||||
@@ -1055,11 +1056,11 @@ async def get_skipped_orders_with_sku(sku: str) -> list[str]:
|
||||
"""Get order_numbers of SKIPPED orders that contain the given SKU."""
|
||||
db = await get_sqlite()
|
||||
try:
|
||||
cursor = await db.execute("""
|
||||
cursor = await db.execute(f"""
|
||||
SELECT DISTINCT oi.order_number
|
||||
FROM order_items oi
|
||||
JOIN orders o ON o.order_number = oi.order_number
|
||||
WHERE oi.sku = ? AND o.status = 'SKIPPED'
|
||||
WHERE oi.sku = ? AND o.status = '{OrderStatus.SKIPPED.value}'
|
||||
""", (sku,))
|
||||
rows = await cursor.fetchall()
|
||||
return [row[0] for row in rows]
|
||||
@@ -1314,7 +1315,7 @@ async def get_orders_missing_anaf() -> list[dict]:
|
||||
WHERE cod_fiscal_roa IS NOT NULL
|
||||
AND cod_fiscal_roa != ''
|
||||
AND anaf_platitor_tva IS NULL
|
||||
AND status IN ('IMPORTED', 'ALREADY_IMPORTED')
|
||||
AND status IN ('{OrderStatus.IMPORTED.value}', '{OrderStatus.ALREADY_IMPORTED.value}')
|
||||
""")
|
||||
rows = await cursor.fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
Reference in New Issue
Block a user