|
@@ -12,7 +12,7 @@ import hashlib
|
|
|
import preview_utils
|
|
import preview_utils
|
|
|
import slicer_utils
|
|
import slicer_utils
|
|
|
from fastapi import APIRouter, Request, Form, Depends, HTTPException, BackgroundTasks, UploadFile, File
|
|
from fastapi import APIRouter, Request, Form, Depends, HTTPException, BackgroundTasks, UploadFile, File
|
|
|
-from services import pricing, order_processing
|
|
|
|
|
|
|
+from services import pricing, order_processing, event_hooks
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/orders", tags=["orders"])
|
|
router = APIRouter(prefix="/orders", tags=["orders"])
|
|
|
|
|
|
|
@@ -79,6 +79,7 @@ async def create_order(
|
|
|
qty = parsed_quantities[idx] if idx < len(parsed_quantities) else 1
|
|
qty = parsed_quantities[idx] if idx < len(parsed_quantities) else 1
|
|
|
db.execute_commit("UPDATE order_files SET order_id = %s, quantity = %s WHERE id = %s", (order_insert_id, qty, f_id))
|
|
db.execute_commit("UPDATE order_files SET order_id = %s, quantity = %s WHERE id = %s", (order_insert_id, qty, f_id))
|
|
|
background_tasks.add_task(order_processing.process_order_slicing, order_insert_id)
|
|
background_tasks.add_task(order_processing.process_order_slicing, order_insert_id)
|
|
|
|
|
+ background_tasks.add_task(event_hooks.on_order_created, order_insert_id)
|
|
|
return {"status": "success", "order_id": order_insert_id, "message": "Order submitted successfully"}
|
|
return {"status": "success", "order_id": order_insert_id, "message": "Order submitted successfully"}
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
print(f"Error creating order: {e}")
|
|
print(f"Error creating order: {e}")
|
|
@@ -141,7 +142,9 @@ async def get_admin_orders(token: str = Depends(auth_utils.oauth2_scheme)):
|
|
|
ORDER BY o.created_at DESC
|
|
ORDER BY o.created_at DESC
|
|
|
"""
|
|
"""
|
|
|
results = db.execute_query(query)
|
|
results = db.execute_query(query)
|
|
|
|
|
+ import session_utils
|
|
|
for row in results:
|
|
for row in results:
|
|
|
|
|
+ row['is_online'] = session_utils.is_user_online(row['user_id']) if row.get('user_id') else False
|
|
|
if row['files']:
|
|
if row['files']:
|
|
|
try: row['files'] = json.loads(f"[{row['files']}]")
|
|
try: row['files'] = json.loads(f"[{row['files']}]")
|
|
|
except: row['files'] = []
|
|
except: row['files'] = []
|
|
@@ -151,7 +154,12 @@ async def get_admin_orders(token: str = Depends(auth_utils.oauth2_scheme)):
|
|
|
return results
|
|
return results
|
|
|
|
|
|
|
|
@router.patch("/{order_id}/admin")
|
|
@router.patch("/{order_id}/admin")
|
|
|
-async def update_order_admin(order_id: int, data: schemas.AdminOrderUpdate, token: str = Depends(auth_utils.oauth2_scheme)):
|
|
|
|
|
|
|
+async def update_order_admin(
|
|
|
|
|
+ order_id: int,
|
|
|
|
|
+ data: schemas.AdminOrderUpdate,
|
|
|
|
|
+ background_tasks: BackgroundTasks,
|
|
|
|
|
+ token: str = Depends(auth_utils.oauth2_scheme)
|
|
|
|
|
+):
|
|
|
payload = auth_utils.decode_token(token)
|
|
payload = auth_utils.decode_token(token)
|
|
|
if not payload or payload.get("role") != 'admin':
|
|
if not payload or payload.get("role") != 'admin':
|
|
|
raise HTTPException(status_code=403, detail="Admin role required")
|
|
raise HTTPException(status_code=403, detail="Admin role required")
|
|
@@ -159,9 +167,15 @@ async def update_order_admin(order_id: int, data: schemas.AdminOrderUpdate, toke
|
|
|
update_fields = []
|
|
update_fields = []
|
|
|
params = []
|
|
params = []
|
|
|
if data.status:
|
|
if data.status:
|
|
|
- order_info = db.execute_query("SELECT email, first_name FROM orders WHERE id = %s", (order_id,))
|
|
|
|
|
|
|
+ order_info = db.execute_query("SELECT * FROM orders WHERE id = %s", (order_id,))
|
|
|
if order_info:
|
|
if order_info:
|
|
|
- notifications.notify_status_change(order_info[0]['email'], order_id, data.status, order_info[0]['first_name'])
|
|
|
|
|
|
|
+ background_tasks.add_task(
|
|
|
|
|
+ event_hooks.on_order_status_changed,
|
|
|
|
|
+ order_id,
|
|
|
|
|
+ data.status,
|
|
|
|
|
+ order_info[0],
|
|
|
|
|
+ data.send_notification
|
|
|
|
|
+ )
|
|
|
update_fields.append("status = %s")
|
|
update_fields.append("status = %s")
|
|
|
params.append(data.status)
|
|
params.append(data.status)
|
|
|
if data.total_price is not None:
|
|
if data.total_price is not None:
|