Update main.py
Browse files
main.py
CHANGED
|
@@ -856,14 +856,17 @@ from pydantic import BaseModel, Field
|
|
| 856 |
from typing import Optional, Literal
|
| 857 |
|
| 858 |
class BookingIn(BaseModel):
|
| 859 |
-
#
|
| 860 |
doctor_id: Optional[int] = None
|
| 861 |
branch_id: Optional[int] = None
|
| 862 |
-
offer_id: Optional[int] = None
|
| 863 |
-
doctor_name: Optional[str] = None
|
| 864 |
-
branch_name: Optional[str] = None
|
| 865 |
-
offer_title: Optional[str] = None
|
| 866 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 867 |
appointment_iso: Optional[str] = Field(None, description="ISO8601 datetime")
|
| 868 |
notes: Optional[str] = None
|
| 869 |
referral_source: Literal[
|
|
@@ -871,19 +874,24 @@ class BookingIn(BaseModel):
|
|
| 871 |
"paid_ads", "clinic_walkin", "insurance_dir", "other"
|
| 872 |
] = "google_search"
|
| 873 |
|
|
|
|
| 874 |
class BookingOut(BaseModel):
|
| 875 |
id: int
|
| 876 |
booking_code: str
|
| 877 |
user_id: int
|
| 878 |
-
doctor_id: int
|
| 879 |
-
branch_id: int
|
| 880 |
-
offer_id: Optional[int]
|
|
|
|
|
|
|
|
|
|
| 881 |
appointment_iso: Optional[str]
|
| 882 |
notes: Optional[str]
|
| 883 |
referral_source: str
|
| 884 |
status: str
|
| 885 |
created_at: str
|
| 886 |
|
|
|
|
| 887 |
# ====== Bookings Helpers ======
|
| 888 |
import random, string
|
| 889 |
|
|
@@ -1052,40 +1060,47 @@ def validate_fk_or_400(doctor_id: int, branch_id: int, offer_id: Optional[int]):
|
|
| 1052 |
raise HTTPException(400, "Invalid offer_id")
|
| 1053 |
|
| 1054 |
# ====== Create Booking ======
|
| 1055 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1056 |
def create_booking(body: BookingIn, payload=Depends(require_scopes(["user"]))):
|
| 1057 |
"""
|
| 1058 |
-
ي
|
| 1059 |
-
وي
|
| 1060 |
"""
|
| 1061 |
me = get_user_by_email_or_404(payload["sub"])
|
| 1062 |
|
| 1063 |
-
# حلّي الأسماء لأي IDs ناقصة
|
| 1064 |
-
body = resolve_missing_ids_from_names(body)
|
| 1065 |
-
|
| 1066 |
-
# تأكد إن IDs الأساسية اتوفرت في الآخر
|
| 1067 |
-
if not body.doctor_id:
|
| 1068 |
-
raise HTTPException(400, "doctor_id required (or resolvable from doctor_name)")
|
| 1069 |
-
if not body.branch_id:
|
| 1070 |
-
raise HTTPException(400, "branch_id required (or resolvable from branch_name)")
|
| 1071 |
-
# offer_id اختياري
|
| 1072 |
-
|
| 1073 |
-
# (اختياري) تأكيد IDs بعد الـ resolve
|
| 1074 |
-
validate_fk_or_400(body.doctor_id, body.branch_id, body.offer_id)
|
| 1075 |
-
|
| 1076 |
rec = {
|
| 1077 |
"booking_code": gen_booking_code(),
|
| 1078 |
"user_id": me["id"],
|
| 1079 |
"doctor_id": body.doctor_id,
|
| 1080 |
"branch_id": body.branch_id,
|
| 1081 |
"offer_id": body.offer_id,
|
|
|
|
|
|
|
|
|
|
| 1082 |
"appointment_iso": body.appointment_iso,
|
| 1083 |
"notes": body.notes,
|
| 1084 |
"referral_source": body.referral_source,
|
| 1085 |
"status": "pending",
|
| 1086 |
"created_at": iso(now_utc()),
|
|
|
|
| 1087 |
}
|
| 1088 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1089 |
|
| 1090 |
# ====== My Bookings ======
|
| 1091 |
from typing import List
|
|
|
|
| 856 |
from typing import Optional, Literal
|
| 857 |
|
| 858 |
class BookingIn(BaseModel):
|
| 859 |
+
# ids لو اتبعت هنخزنها، ولو لأ هنسيبها Null
|
| 860 |
doctor_id: Optional[int] = None
|
| 861 |
branch_id: Optional[int] = None
|
| 862 |
+
offer_id: Optional[int] = None
|
|
|
|
|
|
|
|
|
|
| 863 |
|
| 864 |
+
# labels النصّية اللي جاية من الـ dropdown في الموبايل (هنخزنها كما هي)
|
| 865 |
+
doctor_label: Optional[str] = None # مثال: "محمد صالح المقبل"
|
| 866 |
+
branch_label: Optional[str] = None # مثال: "فرع جدة - التحلية"
|
| 867 |
+
offer_label: Optional[str] = None # مثال: "فحص الشبكية المجاني"
|
| 868 |
+
|
| 869 |
+
# باقي الحقول كما هي
|
| 870 |
appointment_iso: Optional[str] = Field(None, description="ISO8601 datetime")
|
| 871 |
notes: Optional[str] = None
|
| 872 |
referral_source: Literal[
|
|
|
|
| 874 |
"paid_ads", "clinic_walkin", "insurance_dir", "other"
|
| 875 |
] = "google_search"
|
| 876 |
|
| 877 |
+
|
| 878 |
class BookingOut(BaseModel):
|
| 879 |
id: int
|
| 880 |
booking_code: str
|
| 881 |
user_id: int
|
| 882 |
+
doctor_id: Optional[int] = None
|
| 883 |
+
branch_id: Optional[int] = None
|
| 884 |
+
offer_id: Optional[int] = None
|
| 885 |
+
doctor_label: Optional[str] = None
|
| 886 |
+
branch_label: Optional[str] = None
|
| 887 |
+
offer_label: Optional[str] = None
|
| 888 |
appointment_iso: Optional[str]
|
| 889 |
notes: Optional[str]
|
| 890 |
referral_source: str
|
| 891 |
status: str
|
| 892 |
created_at: str
|
| 893 |
|
| 894 |
+
|
| 895 |
# ====== Bookings Helpers ======
|
| 896 |
import random, string
|
| 897 |
|
|
|
|
| 1060 |
raise HTTPException(400, "Invalid offer_id")
|
| 1061 |
|
| 1062 |
# ====== Create Booking ======
|
| 1063 |
+
# ====== Schemas للرد ======
|
| 1064 |
+
class BookingCreateResponse(BaseModel):
|
| 1065 |
+
message: str
|
| 1066 |
+
data: Optional[BookingOut] = None
|
| 1067 |
+
|
| 1068 |
+
@app.post("/api/bookings", response_model=BookingCreateResponse)
|
| 1069 |
def create_booking(body: BookingIn, payload=Depends(require_scopes(["user"]))):
|
| 1070 |
"""
|
| 1071 |
+
يسجّل بيانات الحجز كما جاءت من تطبيق الموبايل مباشرة في جدول bookings،
|
| 1072 |
+
ويرجع رسالة نجاح/فشل.
|
| 1073 |
"""
|
| 1074 |
me = get_user_by_email_or_404(payload["sub"])
|
| 1075 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1076 |
rec = {
|
| 1077 |
"booking_code": gen_booking_code(),
|
| 1078 |
"user_id": me["id"],
|
| 1079 |
"doctor_id": body.doctor_id,
|
| 1080 |
"branch_id": body.branch_id,
|
| 1081 |
"offer_id": body.offer_id,
|
| 1082 |
+
"doctor_label": body.doctor_label,
|
| 1083 |
+
"branch_label": body.branch_label,
|
| 1084 |
+
"offer_label": body.offer_label,
|
| 1085 |
"appointment_iso": body.appointment_iso,
|
| 1086 |
"notes": body.notes,
|
| 1087 |
"referral_source": body.referral_source,
|
| 1088 |
"status": "pending",
|
| 1089 |
"created_at": iso(now_utc()),
|
| 1090 |
+
"raw_payload": body.dict(), # احذف السطر ده لو العمود مش موجود
|
| 1091 |
}
|
| 1092 |
+
|
| 1093 |
+
try:
|
| 1094 |
+
saved = supa_insert("bookings", rec)
|
| 1095 |
+
return {
|
| 1096 |
+
"message": "تم تسجيل طلبك بنجاح. سيقوم أحد ممثلي خدمة العملاء بالتواصل معك هاتفياً لتحديد موعد",
|
| 1097 |
+
"data": saved
|
| 1098 |
+
}
|
| 1099 |
+
except Exception as e:
|
| 1100 |
+
# لو حابب تسجّل الخطأ في اللوج
|
| 1101 |
+
print("[create_booking] insert failed:", e)
|
| 1102 |
+
# نرجّع رسالة فشل موحدة
|
| 1103 |
+
raise HTTPException(status_code=400, detail="تعذر التسجيل. يرجى المحاولة مره أخرى")
|
| 1104 |
|
| 1105 |
# ====== My Bookings ======
|
| 1106 |
from typing import List
|