orders.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. import db
  2. import schemas
  3. import auth_utils
  4. import notifications
  5. import config
  6. from typing import List, Optional
  7. import json
  8. import os
  9. import uuid
  10. import shutil
  11. import hashlib
  12. import preview_utils
  13. import slicer_utils
  14. from fastapi import APIRouter, Request, Form, Depends, HTTPException, BackgroundTasks, UploadFile, File
  15. from services import pricing, order_processing, event_hooks
  16. from services.audit_service import audit_service
  17. from services.rate_limit_service import rate_limit_service
  18. from dependencies import get_current_user, require_admin, get_current_user_optional
  19. from pydantic import BaseModel
  20. from datetime import datetime
  21. import locales
  22. from services.global_manager import global_manager
  23. router = APIRouter(prefix="/orders", tags=["orders"])
  24. @router.post("")
  25. async def create_order(
  26. request: Request,
  27. background_tasks: BackgroundTasks,
  28. first_name: str = Form(...),
  29. last_name: str = Form(...),
  30. phone: str = Form(...),
  31. email: str = Form(...),
  32. shipping_address: str = Form(...),
  33. model_link: Optional[str] = Form(None),
  34. allow_portfolio: bool = Form(False),
  35. notes: Optional[str] = Form(None),
  36. material_id: int = Form(...),
  37. file_ids: str = Form("[]"),
  38. file_quantities: str = Form("[]"),
  39. quantity: int = Form(1),
  40. color_name: Optional[str] = Form(None),
  41. is_company: bool = Form(False),
  42. company_name: Optional[str] = Form(None),
  43. company_pib: Optional[str] = Form(None),
  44. company_address: Optional[str] = Form(None),
  45. user: Optional[dict] = Depends(get_current_user_optional)
  46. ):
  47. ip = request.client.host if request.client else "unknown"
  48. email_addr = email.lower()
  49. lang = request.query_params.get("lang", "en")
  50. is_admin = user.get("role") == "admin" if user else False
  51. if not is_admin and rate_limit_service.is_order_flooding(email_addr, ip):
  52. raise HTTPException(
  53. status_code=429,
  54. detail=locales.translate_error("flood_control", lang)
  55. )
  56. user_id = user.get("id") if user else None
  57. parsed_ids = []
  58. parsed_quantities = []
  59. if file_ids:
  60. try:
  61. parsed_ids = json.loads(file_ids)
  62. parsed_quantities = json.loads(file_quantities)
  63. except:
  64. pass
  65. name_col = f"name_{lang}" if lang in ["en", "ru", "me"] else "name_en"
  66. mat_info = db.execute_query(f"SELECT {name_col}, price_per_cm3 FROM materials WHERE id = %s", (material_id,))
  67. mat_name = mat_info[0][name_col] if mat_info else "Unknown"
  68. mat_price = mat_info[0]['price_per_cm3'] if mat_info else 0.0
  69. file_sizes = []
  70. if parsed_ids:
  71. format_strings = ','.join(['%s'] * len(parsed_ids))
  72. file_rows = db.execute_query(f"SELECT file_size FROM order_files WHERE id IN ({format_strings})", tuple(parsed_ids))
  73. file_sizes = [r['file_size'] for r in file_rows]
  74. estimated_price, item_prices = pricing.calculate_estimated_price(material_id, file_sizes, parsed_quantities if parsed_quantities else None, return_details=True)
  75. # Snapshoting initial parameters
  76. original_params = json.dumps({
  77. "material_name": mat_name,
  78. "material_price": float(mat_price) if mat_price is not None else 0.0,
  79. "estimated_price": float(estimated_price) if estimated_price is not None else 0.0,
  80. "quantity": quantity,
  81. "color_name": color_name,
  82. "notes": notes,
  83. "first_name": first_name,
  84. "last_name": last_name,
  85. "phone": phone,
  86. "email": email,
  87. "shipping_address": shipping_address,
  88. "model_link": model_link,
  89. "is_company": is_company,
  90. "company_name": company_name,
  91. "company_pib": company_pib,
  92. "company_address": company_address
  93. })
  94. order_query = """
  95. INSERT INTO orders (user_id, material_id, first_name, last_name, phone, email, shipping_address, model_link, status, is_company, company_name, company_pib, company_address, allow_portfolio, estimated_price, material_name, material_price, color_name, quantity, notes, original_params)
  96. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, 'pending', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  97. """
  98. order_params = (user_id, material_id, first_name, last_name, phone, email, shipping_address, model_link, is_company, company_name, company_pib, company_address, allow_portfolio, estimated_price, mat_name, mat_price, color_name, quantity, notes, original_params)
  99. try:
  100. order_insert_id = db.execute_commit(order_query, order_params)
  101. if parsed_ids:
  102. for idx, f_id in enumerate(parsed_ids):
  103. qty = parsed_quantities[idx] if idx < len(parsed_quantities) else 1
  104. unit_p = item_prices[idx] if idx < len(item_prices) else 0.0
  105. db.execute_commit("UPDATE order_files SET order_id = %s, quantity = %s, unit_price = %s WHERE id = %s", (order_insert_id, qty, unit_p, f_id))
  106. # Create corresponding fixed order item
  107. file_row = db.execute_query("SELECT filename FROM order_files WHERE id = %s", (f_id,))
  108. fname = file_row[0]['filename'] if file_row else f"File #{f_id}"
  109. db.execute_commit(
  110. "INSERT INTO order_items (order_id, description, quantity, unit_price, total_price) VALUES (%s, %s, %s, %s, %s)",
  111. (order_insert_id, f"3D Print: {fname}", qty, unit_p, round(qty * unit_p, 2))
  112. )
  113. background_tasks.add_task(order_processing.process_order_slicing, order_insert_id)
  114. background_tasks.add_task(event_hooks.on_order_created, order_insert_id)
  115. # Record placement for rate limiting
  116. rate_limit_service.record_order_placement(email_addr, ip)
  117. return {"status": "success", "order_id": order_insert_id, "message": "Order submitted successfully"}
  118. except Exception as e:
  119. print(f"Error creating order: {e}")
  120. raise HTTPException(status_code=500, detail="Internal server error occurred while processing order")
  121. @router.get("/my")
  122. async def get_my_orders(
  123. page: int = 1,
  124. size: int = 10,
  125. user: dict = Depends(get_current_user)
  126. ):
  127. user_id = user.get("id")
  128. offset = (page - 1) * size
  129. # Get total count
  130. count_query = "SELECT COUNT(*) as total FROM orders WHERE user_id = %s"
  131. count_res = db.execute_query(count_query, (user_id,))
  132. total = count_res[0]['total'] if count_res else 0
  133. query = """
  134. SELECT o.*,
  135. (SELECT count(*) FROM order_messages om WHERE om.order_id = o.id AND om.is_from_admin = TRUE AND om.is_read = FALSE) as unread_count,
  136. GROUP_CONCAT(IF(f.id IS NOT NULL, JSON_OBJECT('id', f.id, 'filename', f.filename, 'file_path', f.file_path, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g), NULL)) as files
  137. FROM orders o
  138. LEFT JOIN order_files f ON o.id = f.order_id
  139. WHERE o.user_id = %s
  140. GROUP BY o.id
  141. ORDER BY
  142. CASE
  143. WHEN status IN ('pending', 'processing', 'shipped') THEN 0
  144. ELSE 1
  145. END ASC,
  146. o.created_at DESC
  147. LIMIT %s OFFSET %s
  148. """
  149. results = db.execute_query(query, (user_id, size, offset))
  150. for row in results:
  151. if row['files']:
  152. try: row['files'] = json.loads(f"[{row['files']}]")
  153. except: row['files'] = []
  154. else: row['files'] = []
  155. row['items'] = db.execute_query("SELECT * FROM order_items WHERE order_id = %s", (row['id'],))
  156. return {"orders": results, "total": total}
  157. @router.post("/{order_id}/review")
  158. async def post_order_review(order_id: int, review: schemas.OrderReview, user: dict = Depends(get_current_user)):
  159. # Check if order belongs to user and is in appropriate status
  160. order = db.execute_query("SELECT id, status FROM orders WHERE id = %s AND user_id = %s", (order_id, user['id']))
  161. if not order:
  162. raise HTTPException(status_code=404, detail="Order not found or access denied")
  163. if order[0]['status'] not in ['shipped', 'completed']:
  164. raise HTTPException(status_code=400, detail="Reviews can only be posted for shipped or completed orders")
  165. db.execute_commit(
  166. "UPDATE orders SET review_text = %s, rating = %s, review_approved = FALSE WHERE id = %s",
  167. (review.review_text, review.rating, order_id)
  168. )
  169. # Create audit log
  170. audit_service.log(user['id'], "ORDER_REVIEW", f"Posted review for order {order_id}", order_id)
  171. return {"message": "Review submitted successfully and is awaiting moderation"}
  172. @router.patch("/{order_id}/review/approve")
  173. async def approve_order_review(order_id: int, admin: dict = Depends(require_admin)):
  174. db.execute_commit("UPDATE orders SET review_approved = TRUE WHERE id = %s", (order_id,))
  175. audit_service.log(admin['id'], "ORDER_REVIEW_APPROVE", f"Approved review for order {order_id}", order_id)
  176. return {"message": "Review approved successfully"}
  177. @router.get("/reviews/public", response_model=List[schemas.PublicReview])
  178. async def get_public_reviews():
  179. # Only return approved reviews, anonymized (strictly only the first word of the first name)
  180. query = "SELECT SUBSTRING_INDEX(first_name, ' ', 1) as first_name, rating, review_text FROM orders WHERE review_approved = TRUE ORDER BY created_at DESC LIMIT 10"
  181. return db.execute_query(query)
  182. @router.get("/admin/reviews")
  183. async def get_admin_reviews(
  184. page: int = 1,
  185. size: int = 50,
  186. admin: dict = Depends(require_admin)
  187. ):
  188. offset = (page - 1) * size
  189. query = """
  190. SELECT id, first_name, last_name, email, rating, review_text, review_approved, created_at
  191. FROM orders
  192. WHERE review_text IS NOT NULL AND review_text != ''
  193. ORDER BY created_at DESC
  194. LIMIT %s OFFSET %s
  195. """
  196. results = db.execute_query(query, (size, offset))
  197. count_res = db.execute_query("SELECT COUNT(*) as total FROM orders WHERE review_text IS NOT NULL AND review_text != ''")
  198. total = count_res[0]['total'] if count_res else 0
  199. return {"reviews": results, "total": total}
  200. @router.post("/estimate")
  201. async def get_price_estimate(data: schemas.EstimateRequest):
  202. material = db.execute_query("SELECT price_per_cm3 FROM materials WHERE id = %s", (data.material_id,))
  203. price_per_cm3 = float(material[0]['price_per_cm3']) if material else 0.0
  204. file_prices = []
  205. base_fee = 5.0
  206. for size in data.file_sizes:
  207. total_size_mb = size / (1024 * 1024)
  208. estimated_volume = total_size_mb * 8.0
  209. file_cost = base_fee + (estimated_volume * price_per_cm3)
  210. file_prices.append(round(file_cost, 2))
  211. qts = data.file_quantities if data.file_quantities else [1]*len(data.file_sizes)
  212. return {"file_prices": file_prices, "total_estimate": round(sum(p * q for p, q in zip(file_prices, qts)), 2)}
  213. # --- ADMIN ORDER ENDPOINTS ---
  214. @router.get("/admin/list") # Using /admin/list to avoid conflict with /my
  215. async def get_admin_orders(
  216. search: Optional[str] = None,
  217. status: Optional[str] = None,
  218. date_from: Optional[str] = None,
  219. date_to: Optional[str] = None,
  220. admin: dict = Depends(require_admin)
  221. ):
  222. where_clauses = []
  223. params = []
  224. if search:
  225. search_term = f"%{search}%"
  226. where_clauses.append("(o.id LIKE %s OR o.email LIKE %s OR o.first_name LIKE %s OR o.last_name LIKE %s OR o.company_name LIKE %s OR o.phone LIKE %s OR o.shipping_address LIKE %s)")
  227. params.extend([search_term] * 7)
  228. if status and status != 'all':
  229. where_clauses.append("o.status = %s")
  230. params.append(status)
  231. if date_from:
  232. where_clauses.append("o.created_at >= %s")
  233. params.append(date_from)
  234. if date_to:
  235. where_clauses.append("o.created_at <= %s")
  236. params.append(date_to)
  237. where_sql = f"WHERE {' AND '.join(where_clauses)}" if where_clauses else ""
  238. query = f"""
  239. SELECT o.*, u.can_chat,
  240. (SELECT count(*) FROM order_messages om WHERE om.order_id = o.id AND om.is_from_admin = FALSE AND om.is_read = FALSE) as unread_count,
  241. GROUP_CONCAT(IF(f.id IS NOT NULL, JSON_OBJECT('id', f.id, 'filename', f.filename, 'file_path', f.file_path, 'file_size', f.file_size, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g), NULL)) as files
  242. FROM orders o
  243. LEFT JOIN users u ON o.user_id = u.id
  244. LEFT JOIN order_files f ON o.id = f.order_id
  245. {where_sql}
  246. GROUP BY o.id
  247. ORDER BY o.created_at DESC
  248. """
  249. results = db.execute_query(query, tuple(params))
  250. import session_utils
  251. for row in results:
  252. row['is_online'] = session_utils.is_user_online(row['user_id']) if row.get('user_id') else False
  253. if row['files']:
  254. try: row['files'] = json.loads(f"[{row['files']}]")
  255. except: row['files'] = []
  256. else: row['files'] = []
  257. row['items'] = db.execute_query("SELECT * FROM order_items WHERE order_id = %s", (row['id'],))
  258. photos = db.execute_query("SELECT id, file_path, is_public FROM order_photos WHERE order_id = %s", (row['id'],))
  259. row['photos'] = photos
  260. return results
  261. @router.patch("/{order_id}")
  262. async def update_order(
  263. request: Request,
  264. order_id: int,
  265. data: schemas.AdminOrderUpdate,
  266. background_tasks: BackgroundTasks,
  267. admin: dict = Depends(require_admin)
  268. ):
  269. order_info = db.execute_query("SELECT * FROM orders WHERE id = %s", (order_id,))
  270. if not order_info: raise HTTPException(status_code=404, detail="Order not found")
  271. update_fields = []
  272. params = []
  273. if data.status:
  274. if order_info:
  275. background_tasks.add_task(
  276. event_hooks.on_order_status_changed,
  277. order_id,
  278. data.status,
  279. order_info[0],
  280. data.send_notification
  281. )
  282. # Generate Payment Documents based on status transitions
  283. from services.invoice_service import InvoiceService
  284. o = order_info[0]
  285. price_val = data.total_price if data.total_price is not None else (o.get('total_price') or o.get('estimated_price') or 0)
  286. price = float(price_val)
  287. # 1. Proforma / Payment Slip (on 'processing' or any initial active state)
  288. if data.status in ['processing', 'shipped']:
  289. try:
  290. if o.get('is_company'):
  291. pdf_path = InvoiceService.generate_document(o, doc_type="predracun", override_price=price)
  292. else:
  293. payer_name = f"{o['first_name']} {o.get('last_name', '')}".strip()
  294. pdf_path = InvoiceService.generate_uplatnica(order_id, payer_name, o.get('shipping_address', ''), price)
  295. update_fields.append("proforma_path = %s")
  296. params.append(pdf_path)
  297. except Exception as e:
  298. print(f"Failed to generate proforma: {e}")
  299. # 2. Final Invoice (only on 'shipped')
  300. if data.status == 'shipped':
  301. try:
  302. pdf_path = InvoiceService.generate_document(o, doc_type="faktura", override_price=price)
  303. update_fields.append("invoice_path = %s")
  304. params.append(pdf_path)
  305. except Exception as e:
  306. print(f"Failed to generate final invoice: {e}")
  307. if data.status:
  308. update_fields.append("status = %s")
  309. params.append(data.status)
  310. if data.total_price is not None:
  311. update_fields.append("total_price = %s")
  312. params.append(data.total_price)
  313. if data.material_id is not None:
  314. update_fields.append("material_id = %s")
  315. params.append(data.material_id)
  316. # Also update snapshot names and prices from handbook
  317. mat_info = db.execute_query("SELECT name_en, price_per_cm3 FROM materials WHERE id = %s", (data.material_id,))
  318. if mat_info:
  319. update_fields.append("material_name = %s")
  320. params.append(mat_info[0]['name_en'])
  321. update_fields.append("material_price = %s")
  322. params.append(mat_info[0]['price_per_cm3'])
  323. elif data.material_name is not None:
  324. update_fields.append("material_name = %s")
  325. params.append(data.material_name)
  326. if data.color_name is not None:
  327. update_fields.append("color_name = %s")
  328. params.append(data.color_name)
  329. if data.quantity is not None:
  330. update_fields.append("quantity = %s")
  331. params.append(data.quantity)
  332. if data.fiscal_qr_url is not None:
  333. update_fields.append("fiscal_qr_url = %s")
  334. params.append(data.fiscal_qr_url)
  335. # Auto-set fiscalized_at if adding URL for the first time
  336. if order_info[0].get('fiscalized_at') is None:
  337. update_fields.append("fiscalized_at = %s")
  338. params.append(datetime.now())
  339. if data.ikof is not None:
  340. update_fields.append("ikof = %s")
  341. params.append(data.ikof)
  342. if data.jikr is not None:
  343. update_fields.append("jikr = %s")
  344. params.append(data.jikr)
  345. if update_fields:
  346. query = f"UPDATE orders SET {', '.join(update_fields)} WHERE id = %s"
  347. params.append(order_id)
  348. db.execute_commit(query, tuple(params))
  349. # LOG ACTION
  350. await audit_service.log(
  351. user_id=admin.get("id"),
  352. action="update_order",
  353. target_type="order",
  354. target_id=order_id,
  355. details={"updated_fields": {k.split(" = ")[0]: v for k, v in zip(update_fields, params)}},
  356. request=request
  357. )
  358. # NOTIFY USER VIA WEBSOCKET
  359. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  360. return {"id": order_id, "status": "updated"}
  361. @router.post("/{order_id}/attach-file")
  362. async def admin_attach_file(
  363. order_id: int,
  364. file: UploadFile = File(...),
  365. admin: dict = Depends(require_admin)
  366. ):
  367. unique_filename = f"{uuid.uuid4()}{os.path.splitext(file.filename)[1]}"
  368. file_path = os.path.join(config.UPLOAD_DIR, unique_filename)
  369. db_file_path = f"uploads/{unique_filename}"
  370. sha256_hash = hashlib.sha256()
  371. with open(file_path, "wb") as buffer:
  372. while chunk := file.file.read(8192):
  373. sha256_hash.update(chunk)
  374. buffer.write(chunk)
  375. preview_path = None
  376. db_preview_path = None
  377. if file_path.lower().endswith(".stl"):
  378. preview_filename = f"{uuid.uuid4()}.png"
  379. preview_path = os.path.join(config.PREVIEW_DIR, preview_filename)
  380. db_preview_path = f"uploads/previews/{preview_filename}"
  381. preview_utils.generate_stl_preview(file_path, preview_path)
  382. filament_g = None
  383. print_time = None
  384. if file_path.lower().endswith(".stl"):
  385. result = slicer_utils.slice_model(file_path)
  386. if result and result.get('success'):
  387. filament_g = result.get('filament_g')
  388. print_time = result.get('print_time_str')
  389. query = "INSERT INTO order_files (order_id, filename, file_path, file_size, quantity, file_hash, print_time, filament_g, preview_path) VALUES (%s, %s, %s, %s, 1, %s, %s, %s, %s)"
  390. f_id = db.execute_commit(query, (order_id, file.filename, db_file_path, file.size, sha256_hash.hexdigest(), print_time, filament_g, db_preview_path))
  391. # NOTIFY USER VIA WEBSOCKET
  392. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  393. if order_info:
  394. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  395. return {"file_id": f_id, "filename": file.filename, "preview_path": db_preview_path, "filament_g": filament_g, "print_time": print_time}
  396. @router.delete("/{order_id}/files/{file_id}")
  397. async def admin_delete_file(
  398. order_id: int,
  399. file_id: int,
  400. request: Request,
  401. admin: dict = Depends(require_admin)
  402. ):
  403. file_record = db.execute_query("SELECT file_path, preview_path FROM order_files WHERE id = %s AND order_id = %s", (file_id, order_id))
  404. if not file_record:
  405. raise HTTPException(status_code=404, detail="File not found")
  406. base_dir = config.BASE_DIR
  407. try:
  408. if file_record[0]['file_path']:
  409. os.remove(os.path.join(base_dir, file_record[0]['file_path']))
  410. if file_record[0]['preview_path']:
  411. os.remove(os.path.join(base_dir, file_record[0]['preview_path']))
  412. except Exception as e:
  413. print(f"Error removing file from disk: {e}")
  414. db.execute_commit("DELETE FROM order_files WHERE id = %s AND order_id = %s", (file_id, order_id))
  415. # LOG ACTION
  416. await audit_service.log(
  417. user_id=admin.get("id"),
  418. action="delete_order_file",
  419. target_type="order",
  420. target_id=order_id,
  421. details={"file_id": file_id},
  422. request=request
  423. )
  424. # NOTIFY USER VIA WEBSOCKET
  425. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  426. if order_info:
  427. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  428. return {"status": "success"}
  429. class OrderItemSchema(BaseModel):
  430. description: str
  431. quantity: int
  432. unit_price: float
  433. @router.get("/{order_id}/items")
  434. async def get_order_items(order_id: int):
  435. items = db.execute_query("SELECT * FROM order_items WHERE order_id = %s", (order_id,))
  436. return items
  437. @router.put("/{order_id}/items")
  438. async def update_order_items(order_id: int, items: List[OrderItemSchema], admin: dict = Depends(require_admin)):
  439. db.execute_commit("DELETE FROM order_items WHERE order_id = %s", (order_id,))
  440. total_order_price = 0
  441. for item in items:
  442. tot_p = round(item.quantity * item.unit_price, 2)
  443. total_order_price += tot_p
  444. db.execute_commit(
  445. "INSERT INTO order_items (order_id, description, quantity, unit_price, total_price) VALUES (%s, %s, %s, %s, %s)",
  446. (order_id, item.description, item.quantity, item.unit_price, tot_p)
  447. )
  448. # Sync main order total_price
  449. db.execute_commit("UPDATE orders SET total_price = %s WHERE id = %s", (total_order_price, order_id))
  450. # NOTIFY USER VIA WEBSOCKET
  451. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  452. if order_info:
  453. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  454. return {"status": "success", "total_price": total_order_price}
  455. @router.delete("/{order_id}/admin")
  456. async def delete_order_admin(
  457. order_id: int,
  458. request: Request,
  459. admin: dict = Depends(require_admin)
  460. ):
  461. # Fetch user_id before deletion to notify later
  462. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  463. if not order_info: raise HTTPException(status_code=404, detail="Order not found")
  464. customer_id = order_info[0]['user_id']
  465. # 1. Find all related files to delete from disk
  466. files = db.execute_query("SELECT file_path, preview_path FROM order_files WHERE order_id = %s", (order_id,))
  467. photos = db.execute_query("SELECT file_path FROM order_photos WHERE order_id = %s", (order_id,))
  468. base_dir = config.BASE_DIR
  469. # Delete order_files from disk
  470. for f in files:
  471. try:
  472. if f.get('file_path'):
  473. fpath = os.path.join(base_dir, f['file_path'])
  474. if os.path.exists(fpath): os.remove(fpath)
  475. if f.get('preview_path'):
  476. ppath = os.path.join(base_dir, f['preview_path'])
  477. if os.path.exists(ppath): os.remove(ppath)
  478. except Exception as e:
  479. print(f"Error deleting order file {f.get('file_path')}: {e}")
  480. # Delete photos from disk
  481. for p in photos:
  482. try:
  483. if p.get('file_path'):
  484. fpath = os.path.join(base_dir, p['file_path'])
  485. if os.path.exists(fpath): os.remove(fpath)
  486. except Exception as e:
  487. print(f"Error deleting order photo {p.get('file_path')}: {e}")
  488. # 2. Delete from DB tables (due to possible lack of CASCADE or to be explicit)
  489. try:
  490. db.execute_commit("DELETE FROM order_messages WHERE order_id = %s", (order_id,))
  491. db.execute_commit("DELETE FROM order_items WHERE order_id = %s", (order_id,))
  492. db.execute_commit("DELETE FROM order_files WHERE order_id = %s", (order_id,))
  493. db.execute_commit("DELETE FROM order_photos WHERE order_id = %s", (order_id,))
  494. db.execute_commit("DELETE FROM orders WHERE id = %s", (order_id,))
  495. # LOG ACTION
  496. await audit_service.log(
  497. user_id=admin.get("id"),
  498. action="delete_order_entirely",
  499. target_type="order",
  500. target_id=order_id,
  501. details={"order_id": order_id},
  502. request=request
  503. )
  504. # NOTIFY USER VIA WEBSOCKET
  505. await global_manager.notify_order_update(customer_id, order_id)
  506. return {"status": "success", "message": f"Order {order_id} deleted entirely"}
  507. except Exception as e:
  508. print(f"Failed to delete order {order_id}: {e}")
  509. raise HTTPException(status_code=500, detail=str(e))