orders.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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.post("/estimate")
  183. async def get_price_estimate(data: schemas.EstimateRequest):
  184. material = db.execute_query("SELECT price_per_cm3 FROM materials WHERE id = %s", (data.material_id,))
  185. price_per_cm3 = float(material[0]['price_per_cm3']) if material else 0.0
  186. file_prices = []
  187. base_fee = 5.0
  188. for size in data.file_sizes:
  189. total_size_mb = size / (1024 * 1024)
  190. estimated_volume = total_size_mb * 8.0
  191. file_cost = base_fee + (estimated_volume * price_per_cm3)
  192. file_prices.append(round(file_cost, 2))
  193. qts = data.file_quantities if data.file_quantities else [1]*len(data.file_sizes)
  194. return {"file_prices": file_prices, "total_estimate": round(sum(p * q for p, q in zip(file_prices, qts)), 2)}
  195. # --- ADMIN ORDER ENDPOINTS ---
  196. @router.get("/admin/list") # Using /admin/list to avoid conflict with /my
  197. async def get_admin_orders(
  198. search: Optional[str] = None,
  199. status: Optional[str] = None,
  200. date_from: Optional[str] = None,
  201. date_to: Optional[str] = None,
  202. admin: dict = Depends(require_admin)
  203. ):
  204. where_clauses = []
  205. params = []
  206. if search:
  207. search_term = f"%{search}%"
  208. 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)")
  209. params.extend([search_term] * 7)
  210. if status and status != 'all':
  211. where_clauses.append("o.status = %s")
  212. params.append(status)
  213. if date_from:
  214. where_clauses.append("o.created_at >= %s")
  215. params.append(date_from)
  216. if date_to:
  217. where_clauses.append("o.created_at <= %s")
  218. params.append(date_to)
  219. where_sql = f"WHERE {' AND '.join(where_clauses)}" if where_clauses else ""
  220. query = f"""
  221. SELECT o.*, u.can_chat,
  222. (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,
  223. 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
  224. FROM orders o
  225. LEFT JOIN users u ON o.user_id = u.id
  226. LEFT JOIN order_files f ON o.id = f.order_id
  227. {where_sql}
  228. GROUP BY o.id
  229. ORDER BY o.created_at DESC
  230. """
  231. results = db.execute_query(query, tuple(params))
  232. import session_utils
  233. for row in results:
  234. row['is_online'] = session_utils.is_user_online(row['user_id']) if row.get('user_id') else False
  235. if row['files']:
  236. try: row['files'] = json.loads(f"[{row['files']}]")
  237. except: row['files'] = []
  238. else: row['files'] = []
  239. row['items'] = db.execute_query("SELECT * FROM order_items WHERE order_id = %s", (row['id'],))
  240. photos = db.execute_query("SELECT id, file_path, is_public FROM order_photos WHERE order_id = %s", (row['id'],))
  241. row['photos'] = photos
  242. return results
  243. @router.patch("/{order_id}")
  244. async def update_order(
  245. request: Request,
  246. order_id: int,
  247. data: schemas.AdminOrderUpdate,
  248. background_tasks: BackgroundTasks,
  249. admin: dict = Depends(require_admin)
  250. ):
  251. order_info = db.execute_query("SELECT * FROM orders WHERE id = %s", (order_id,))
  252. if not order_info: raise HTTPException(status_code=404, detail="Order not found")
  253. update_fields = []
  254. params = []
  255. if data.status:
  256. if order_info:
  257. background_tasks.add_task(
  258. event_hooks.on_order_status_changed,
  259. order_id,
  260. data.status,
  261. order_info[0],
  262. data.send_notification
  263. )
  264. # Generate Payment Documents based on status transitions
  265. from services.invoice_service import InvoiceService
  266. o = order_info[0]
  267. price_val = data.total_price if data.total_price is not None else (o.get('total_price') or o.get('estimated_price') or 0)
  268. price = float(price_val)
  269. # 1. Proforma / Payment Slip (on 'processing' or any initial active state)
  270. if data.status in ['processing', 'shipped']:
  271. try:
  272. if o.get('is_company'):
  273. pdf_path = InvoiceService.generate_document(o, doc_type="predracun", override_price=price)
  274. else:
  275. payer_name = f"{o['first_name']} {o.get('last_name', '')}".strip()
  276. pdf_path = InvoiceService.generate_uplatnica(order_id, payer_name, o.get('shipping_address', ''), price)
  277. update_fields.append("proforma_path = %s")
  278. params.append(pdf_path)
  279. except Exception as e:
  280. print(f"Failed to generate proforma: {e}")
  281. # 2. Final Invoice (only on 'shipped')
  282. if data.status == 'shipped':
  283. try:
  284. pdf_path = InvoiceService.generate_document(o, doc_type="faktura", override_price=price)
  285. update_fields.append("invoice_path = %s")
  286. params.append(pdf_path)
  287. except Exception as e:
  288. print(f"Failed to generate final invoice: {e}")
  289. if data.status:
  290. update_fields.append("status = %s")
  291. params.append(data.status)
  292. if data.total_price is not None:
  293. update_fields.append("total_price = %s")
  294. params.append(data.total_price)
  295. if data.material_id is not None:
  296. update_fields.append("material_id = %s")
  297. params.append(data.material_id)
  298. # Also update snapshot names and prices from handbook
  299. mat_info = db.execute_query("SELECT name_en, price_per_cm3 FROM materials WHERE id = %s", (data.material_id,))
  300. if mat_info:
  301. update_fields.append("material_name = %s")
  302. params.append(mat_info[0]['name_en'])
  303. update_fields.append("material_price = %s")
  304. params.append(mat_info[0]['price_per_cm3'])
  305. elif data.material_name is not None:
  306. update_fields.append("material_name = %s")
  307. params.append(data.material_name)
  308. if data.color_name is not None:
  309. update_fields.append("color_name = %s")
  310. params.append(data.color_name)
  311. if data.quantity is not None:
  312. update_fields.append("quantity = %s")
  313. params.append(data.quantity)
  314. if data.fiscal_qr_url is not None:
  315. update_fields.append("fiscal_qr_url = %s")
  316. params.append(data.fiscal_qr_url)
  317. # Auto-set fiscalized_at if adding URL for the first time
  318. if order_info[0].get('fiscalized_at') is None:
  319. update_fields.append("fiscalized_at = %s")
  320. params.append(datetime.now())
  321. if data.ikof is not None:
  322. update_fields.append("ikof = %s")
  323. params.append(data.ikof)
  324. if data.jikr is not None:
  325. update_fields.append("jikr = %s")
  326. params.append(data.jikr)
  327. if update_fields:
  328. query = f"UPDATE orders SET {', '.join(update_fields)} WHERE id = %s"
  329. params.append(order_id)
  330. db.execute_commit(query, tuple(params))
  331. # LOG ACTION
  332. await audit_service.log(
  333. user_id=admin.get("id"),
  334. action="update_order",
  335. target_type="order",
  336. target_id=order_id,
  337. details={"updated_fields": {k.split(" = ")[0]: v for k, v in zip(update_fields, params)}},
  338. request=request
  339. )
  340. # NOTIFY USER VIA WEBSOCKET
  341. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  342. return {"id": order_id, "status": "updated"}
  343. @router.post("/{order_id}/attach-file")
  344. async def admin_attach_file(
  345. order_id: int,
  346. file: UploadFile = File(...),
  347. admin: dict = Depends(require_admin)
  348. ):
  349. unique_filename = f"{uuid.uuid4()}{os.path.splitext(file.filename)[1]}"
  350. file_path = os.path.join(config.UPLOAD_DIR, unique_filename)
  351. db_file_path = f"uploads/{unique_filename}"
  352. sha256_hash = hashlib.sha256()
  353. with open(file_path, "wb") as buffer:
  354. while chunk := file.file.read(8192):
  355. sha256_hash.update(chunk)
  356. buffer.write(chunk)
  357. preview_path = None
  358. db_preview_path = None
  359. if file_path.lower().endswith(".stl"):
  360. preview_filename = f"{uuid.uuid4()}.png"
  361. preview_path = os.path.join(config.PREVIEW_DIR, preview_filename)
  362. db_preview_path = f"uploads/previews/{preview_filename}"
  363. preview_utils.generate_stl_preview(file_path, preview_path)
  364. filament_g = None
  365. print_time = None
  366. if file_path.lower().endswith(".stl"):
  367. result = slicer_utils.slice_model(file_path)
  368. if result and result.get('success'):
  369. filament_g = result.get('filament_g')
  370. print_time = result.get('print_time_str')
  371. 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)"
  372. 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))
  373. # NOTIFY USER VIA WEBSOCKET
  374. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  375. if order_info:
  376. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  377. return {"file_id": f_id, "filename": file.filename, "preview_path": db_preview_path, "filament_g": filament_g, "print_time": print_time}
  378. @router.delete("/{order_id}/files/{file_id}")
  379. async def admin_delete_file(
  380. order_id: int,
  381. file_id: int,
  382. request: Request,
  383. admin: dict = Depends(require_admin)
  384. ):
  385. file_record = db.execute_query("SELECT file_path, preview_path FROM order_files WHERE id = %s AND order_id = %s", (file_id, order_id))
  386. if not file_record:
  387. raise HTTPException(status_code=404, detail="File not found")
  388. base_dir = config.BASE_DIR
  389. try:
  390. if file_record[0]['file_path']:
  391. os.remove(os.path.join(base_dir, file_record[0]['file_path']))
  392. if file_record[0]['preview_path']:
  393. os.remove(os.path.join(base_dir, file_record[0]['preview_path']))
  394. except Exception as e:
  395. print(f"Error removing file from disk: {e}")
  396. db.execute_commit("DELETE FROM order_files WHERE id = %s AND order_id = %s", (file_id, order_id))
  397. # LOG ACTION
  398. await audit_service.log(
  399. user_id=admin.get("id"),
  400. action="delete_order_file",
  401. target_type="order",
  402. target_id=order_id,
  403. details={"file_id": file_id},
  404. request=request
  405. )
  406. # NOTIFY USER VIA WEBSOCKET
  407. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  408. if order_info:
  409. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  410. return {"status": "success"}
  411. class OrderItemSchema(BaseModel):
  412. description: str
  413. quantity: int
  414. unit_price: float
  415. @router.get("/{order_id}/items")
  416. async def get_order_items(order_id: int):
  417. items = db.execute_query("SELECT * FROM order_items WHERE order_id = %s", (order_id,))
  418. return items
  419. @router.put("/{order_id}/items")
  420. async def update_order_items(order_id: int, items: List[OrderItemSchema], admin: dict = Depends(require_admin)):
  421. db.execute_commit("DELETE FROM order_items WHERE order_id = %s", (order_id,))
  422. total_order_price = 0
  423. for item in items:
  424. tot_p = round(item.quantity * item.unit_price, 2)
  425. total_order_price += tot_p
  426. db.execute_commit(
  427. "INSERT INTO order_items (order_id, description, quantity, unit_price, total_price) VALUES (%s, %s, %s, %s, %s)",
  428. (order_id, item.description, item.quantity, item.unit_price, tot_p)
  429. )
  430. # Sync main order total_price
  431. db.execute_commit("UPDATE orders SET total_price = %s WHERE id = %s", (total_order_price, order_id))
  432. # NOTIFY USER VIA WEBSOCKET
  433. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  434. if order_info:
  435. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  436. return {"status": "success", "total_price": total_order_price}
  437. @router.delete("/{order_id}/admin")
  438. async def delete_order_admin(
  439. order_id: int,
  440. request: Request,
  441. admin: dict = Depends(require_admin)
  442. ):
  443. # Fetch user_id before deletion to notify later
  444. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  445. if not order_info: raise HTTPException(status_code=404, detail="Order not found")
  446. customer_id = order_info[0]['user_id']
  447. # 1. Find all related files to delete from disk
  448. files = db.execute_query("SELECT file_path, preview_path FROM order_files WHERE order_id = %s", (order_id,))
  449. photos = db.execute_query("SELECT file_path FROM order_photos WHERE order_id = %s", (order_id,))
  450. base_dir = config.BASE_DIR
  451. # Delete order_files from disk
  452. for f in files:
  453. try:
  454. if f.get('file_path'):
  455. fpath = os.path.join(base_dir, f['file_path'])
  456. if os.path.exists(fpath): os.remove(fpath)
  457. if f.get('preview_path'):
  458. ppath = os.path.join(base_dir, f['preview_path'])
  459. if os.path.exists(ppath): os.remove(ppath)
  460. except Exception as e:
  461. print(f"Error deleting order file {f.get('file_path')}: {e}")
  462. # Delete photos from disk
  463. for p in photos:
  464. try:
  465. if p.get('file_path'):
  466. fpath = os.path.join(base_dir, p['file_path'])
  467. if os.path.exists(fpath): os.remove(fpath)
  468. except Exception as e:
  469. print(f"Error deleting order photo {p.get('file_path')}: {e}")
  470. # 2. Delete from DB tables (due to possible lack of CASCADE or to be explicit)
  471. try:
  472. db.execute_commit("DELETE FROM order_messages WHERE order_id = %s", (order_id,))
  473. db.execute_commit("DELETE FROM order_items WHERE order_id = %s", (order_id,))
  474. db.execute_commit("DELETE FROM order_files WHERE order_id = %s", (order_id,))
  475. db.execute_commit("DELETE FROM order_photos WHERE order_id = %s", (order_id,))
  476. db.execute_commit("DELETE FROM orders WHERE id = %s", (order_id,))
  477. # LOG ACTION
  478. await audit_service.log(
  479. user_id=admin.get("id"),
  480. action="delete_order_entirely",
  481. target_type="order",
  482. target_id=order_id,
  483. details={"order_id": order_id},
  484. request=request
  485. )
  486. # NOTIFY USER VIA WEBSOCKET
  487. await global_manager.notify_order_update(customer_id, order_id)
  488. return {"status": "success", "message": f"Order {order_id} deleted entirely"}
  489. except Exception as e:
  490. print(f"Failed to delete order {order_id}: {e}")
  491. raise HTTPException(status_code=500, detail=str(e))