Binrushd-Medical-Center commited on
Commit
ea88441
·
verified ·
1 Parent(s): 73e1788

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -10
main.py CHANGED
@@ -1107,31 +1107,41 @@ from typing import Optional
1107
  def bookings_by_contact(
1108
  payload=Depends(require_scopes(["user"])),
1109
  client_mobile: Optional[str] = None,
 
1110
  limit: int = 100,
1111
  offset: int = 0,
1112
  order: str = "created_at.desc",
1113
  ):
1114
  """
1115
- يرجّع حجوزات المستخدم الحالي (من الإيميل الموجود داخل التوكن) مع إمكانية
1116
- تضييق النتائج برقم الجوال (اختياري).
1117
- - الإيميل يؤخذ حصراً من التوكن لمنع التسريب.
1118
- - client_mobile (اختياري) فلتر إضافي داخل نطاق نفس المستخدم.
 
 
1119
  """
1120
- # جبنا المستخدم من الإيميل الموجود في التوكن
1121
- me = get_user_by_email_or_404(payload["sub"])
1122
 
1123
- # فلتر أساسي بالإيميل من التوكن
1124
  params = {
1125
  "select": "*",
1126
- **eq("client_email", me["email"]),
1127
  "order": order,
1128
  "limit": str(limit),
1129
  "offset": str(offset),
1130
  }
1131
 
1132
- # فلتر اختياري بالموبايل (لو اتبعت)
1133
- if client_mobile:
 
1134
  params.update(eq("client_mobile", client_mobile))
 
 
 
 
 
 
 
 
1135
 
1136
  rows = supa_select("bookings", params)
1137
  return {
@@ -1139,6 +1149,7 @@ def bookings_by_contact(
1139
  "count": len(rows),
1140
  "data": rows
1141
  }
 
1142
 
1143
  # ====== Referral Sources (static list) ======
1144
  @app.get("/api/referral-sources")
 
1107
  def bookings_by_contact(
1108
  payload=Depends(require_scopes(["user"])),
1109
  client_mobile: Optional[str] = None,
1110
+ client_email: Optional[str] = None,
1111
  limit: int = 100,
1112
  offset: int = 0,
1113
  order: str = "created_at.desc",
1114
  ):
1115
  """
1116
+ يرجّع حجوزات العميل كـ JSON.
1117
+ - لو بعت client_mobile فقط → تصفية بالموبايل.
1118
+ - لو بعت client_email فقط → تصفية بالإيميل.
1119
+ - لو بعت الاتنين → AND (لازم يطابقوا معًا).
1120
+ - لو مبعتش حاجة → نستخدم إيميل التوكن كـ default.
1121
+ لازال يتطلّب Bearer token بصلاحية user.
1122
  """
1123
+ # default email = من التوكن لو مش مبعوت في الكويري
1124
+ effective_email = client_email or payload.get("sub")
1125
 
 
1126
  params = {
1127
  "select": "*",
 
1128
  "order": order,
1129
  "limit": str(limit),
1130
  "offset": str(offset),
1131
  }
1132
 
1133
+ # طبّق الفلاتر حسب اللي مبعوت
1134
+ if client_mobile and effective_email:
1135
+ # AND: الموبايل + الإيميل
1136
  params.update(eq("client_mobile", client_mobile))
1137
+ params.update(eq("client_email", effective_email))
1138
+ elif client_mobile:
1139
+ params.update(eq("client_mobile", client_mobile))
1140
+ elif effective_email:
1141
+ params.update(eq("client_email", effective_email))
1142
+ else:
1143
+ # نظريًا مش هنوصل هنا لأن عندنا توكن، بس للاحتياط
1144
+ raise HTTPException(400, "أرسل client_mobile أو client_email أو استخدم توكن صالح")
1145
 
1146
  rows = supa_select("bookings", params)
1147
  return {
 
1149
  "count": len(rows),
1150
  "data": rows
1151
  }
1152
+
1153
 
1154
  # ====== Referral Sources (static list) ======
1155
  @app.get("/api/referral-sources")