Binrushd-Medical-Center commited on
Commit
4e17a7b
·
verified ·
1 Parent(s): 4c54616

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -12
main.py CHANGED
@@ -894,25 +894,56 @@ def get_user_by_email_or_404(email: str) -> Dict[str, Any]:
894
  raise HTTPException(404, "User not found")
895
  return rows[0]
896
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
897
  def validate_fk_or_400(doctor_id: int, branch_id: int, offer_id: Optional[int]):
 
898
  d = supa_select("doctors", {**eq("id", doctor_id), "select": "id", "limit": "1"})
899
  if not d:
900
  raise HTTPException(400, "Invalid doctor_id")
901
 
902
- # تحقق الفرع من JSON
903
- path = BASE_DIR / "branches_summary.json"
904
- branches = json.loads(path.read_text(encoding="utf-8")).get("data", [])
905
- b = [x for x in branches if x.get("id") == branch_id]
906
- if not b:
907
  raise HTTPException(400, "Invalid branch_id")
908
 
909
- if offer_id is not None:
910
- try:
911
- o = supa_select("offers", {**eq("id", offer_id), "select": "id", "limit": "1"})
912
- except Exception:
913
- o = []
914
- if not o:
915
- raise HTTPException(400, "Invalid offer_id")
916
 
917
  # ====== Create Booking ======
918
  @app.post("/api/bookings", response_model=BookingOut)
 
894
  raise HTTPException(404, "User not found")
895
  return rows[0]
896
 
897
+ # ====== Bookings Validation via JSON files ======
898
+ def _load_json(path: Path, not_found_msg: str):
899
+ if not path.exists():
900
+ raise HTTPException(500, not_found_msg)
901
+ try:
902
+ return json.loads(path.read_text(encoding="utf-8"))
903
+ except json.JSONDecodeError:
904
+ raise HTTPException(500, f"{path.name} contains invalid JSON")
905
+
906
+ def _branch_exists_in_json(branch_id: int) -> bool:
907
+ path = BASE_DIR / "branches_summary.json"
908
+ data = _load_json(path, "branches_summary.json not found")
909
+ items = data.get("data", data)
910
+ if isinstance(items, list):
911
+ return any(it.get("id") == branch_id for it in items)
912
+ if isinstance(items, dict):
913
+ # في حالة تكون dict keyed-by-id
914
+ return str(branch_id) in items
915
+ return False
916
+
917
+ def _offer_exists_in_json(offer_id: int) -> bool:
918
+ path = BASE_DIR / "offers.json"
919
+ data = _load_json(path, "offers.json not found")
920
+ payload = data.get("data", data)
921
+
922
+ # قد تكون العروض تحت campaigns أو offers
923
+ if isinstance(payload, dict):
924
+ campaigns = payload.get("campaigns", [])
925
+ offers = payload.get("offers", [])
926
+ return any(c.get("id") == offer_id for c in campaigns) or any(o.get("id") == offer_id for o in offers)
927
+
928
+ # fallback لو كانت ليستة مباشرة
929
+ if isinstance(payload, list):
930
+ return any(it.get("id") == offer_id for it in payload)
931
+
932
+ return False
933
+
934
  def validate_fk_or_400(doctor_id: int, branch_id: int, offer_id: Optional[int]):
935
+ # 1) doctor من Supabase (مافيش ملف doctors)
936
  d = supa_select("doctors", {**eq("id", doctor_id), "select": "id", "limit": "1"})
937
  if not d:
938
  raise HTTPException(400, "Invalid doctor_id")
939
 
940
+ # 2) branch من ملف JSON
941
+ if not _branch_exists_in_json(branch_id):
 
 
 
942
  raise HTTPException(400, "Invalid branch_id")
943
 
944
+ # 3) offer (اختياري) من ملف JSON
945
+ if offer_id is not None and not _offer_exists_in_json(offer_id):
946
+ raise HTTPException(400, "Invalid offer_id")
 
 
 
 
947
 
948
  # ====== Create Booking ======
949
  @app.post("/api/bookings", response_model=BookingOut)