Binrushd-Medical-Center commited on
Commit
0acb762
·
verified ·
1 Parent(s): df4c148

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +45 -0
main.py CHANGED
@@ -618,6 +618,51 @@ def list_doctors(limit: int = 100, offset: int = 0, order: str = "id.asc"):
618
  rows = supa_select("doctors", params)
619
  return {"count": len(rows), "data": rows}
620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
621
 
622
  @app.get("/api/doctors/{doctor_id}/details")
623
  def doctor_details(doctor_id: int):
 
618
  rows = supa_select("doctors", params)
619
  return {"count": len(rows), "data": rows}
620
 
621
+ @app.get("/api/doctors/with-favorites")
622
+ def list_doctors_with_favorites(
623
+ payload=Depends(require_scopes(["user"])),
624
+ limit: int = 100,
625
+ offset: int = 0,
626
+ order: str = "id.asc",
627
+ client_email: Optional[str] = None,
628
+ client_mobile: Optional[str] = None,
629
+ ):
630
+ """
631
+ يرجّع قائمة الدكاترة + is_favorite لكل دكتور بحسب مفضّلات العميل.
632
+ تحديد العميل:
633
+ - افتراضيًا: من التوكن (payload["sub"]) كـ client_email
634
+ - أو صراحةً عبر client_email / client_mobile (اختياري)
635
+ """
636
+
637
+ # 1) هات الدكاترة
638
+ docs_params = {
639
+ "select": "id,fname,lname,brief,image,created_at",
640
+ "limit": str(limit),
641
+ "offset": str(offset),
642
+ "order": order,
643
+ }
644
+ doctors = supa_select("doctors", docs_params)
645
+
646
+ # 2) حدّد العميل
647
+ client_email = (client_email or "").strip() or payload.get("sub")
648
+ client_mobile = (client_mobile or "").strip() or None
649
+ if not client_email and not client_mobile:
650
+ raise HTTPException(400, "لم يتم تحديد العميل: استخدم التوكن أو أرسل client_email/client_mobile")
651
+
652
+ # 3) هات المفضّلات (doctor_id فقط)
653
+ fav_params = {"select": "doctor_id", "limit": "10000", "order": "created_at.desc"}
654
+ if client_email:
655
+ fav_params["client_email"] = f"eq.{client_email}"
656
+ if client_mobile:
657
+ fav_params["client_mobile"] = f"eq.{client_mobile}"
658
+
659
+ favorites = supa_select("favorites", fav_params)
660
+ fav_ids = {row.get("doctor_id") for row in favorites if row.get("doctor_id") is not None}
661
+
662
+ # 4) دمج is_favorite
663
+ data = [{**d, "is_favorite": d.get("id") in fav_ids} for d in doctors]
664
+ return {"count": len(data), "data": data}
665
+
666
 
667
  @app.get("/api/doctors/{doctor_id}/details")
668
  def doctor_details(doctor_id: int):