use phone_number instead of remoteJid for driver notifications
Browse files- app/database/supabase.py +2 -0
- app/tools/handlers.py +6 -5
- tests/test_tools.py +26 -1
app/database/supabase.py
CHANGED
|
@@ -40,6 +40,8 @@ class SupabaseRepository:
|
|
| 40 |
preferred_language: str | None = None,
|
| 41 |
phone_number: str | None = None,
|
| 42 |
) -> dict[str, Any]:
|
|
|
|
|
|
|
| 43 |
payload = {
|
| 44 |
"remoteJid": remote_jid,
|
| 45 |
"name": name,
|
|
|
|
| 40 |
preferred_language: str | None = None,
|
| 41 |
phone_number: str | None = None,
|
| 42 |
) -> dict[str, Any]:
|
| 43 |
+
if phone_number:
|
| 44 |
+
phone_number = phone_number.split("@")[0]
|
| 45 |
payload = {
|
| 46 |
"remoteJid": remote_jid,
|
| 47 |
"name": name,
|
app/tools/handlers.py
CHANGED
|
@@ -235,12 +235,12 @@ class FalsaToolHandlers:
|
|
| 235 |
try:
|
| 236 |
driver_record = _first_or_dict(trip.get("drivers")) or {}
|
| 237 |
driver_customer = driver_record.get("customers") or {}
|
| 238 |
-
|
| 239 |
-
if not
|
| 240 |
-
raise WhatsAppClientError("Driver
|
| 241 |
-
driver_phone =
|
| 242 |
await self.whatsapp.send_text(
|
| 243 |
-
|
| 244 |
_driver_notification_text(
|
| 245 |
customer=self.customer,
|
| 246 |
trip=trip,
|
|
@@ -1081,6 +1081,7 @@ def _driver_notification_text(
|
|
| 1081 |
return (
|
| 1082 |
"🔔 حجز جديد في فلسا\n"
|
| 1083 |
f"العميل: {customer.get('name') or 'عميل جديد'}\n"
|
|
|
|
| 1084 |
f"الرحلة: {trip.get('departure')} ← {trip.get('destination')}\n"
|
| 1085 |
f"التاريخ: {trip_departure_date(trip)} {trip_departure_bucket(trip)}\n"
|
| 1086 |
f"المقاعد المطلوبة: {requested_seats}\n"
|
|
|
|
| 235 |
try:
|
| 236 |
driver_record = _first_or_dict(trip.get("drivers")) or {}
|
| 237 |
driver_customer = driver_record.get("customers") or {}
|
| 238 |
+
driver_recipient = driver_customer.get("phone_number") or driver_customer.get("remoteJid") or driver_record.get("remoteJid")
|
| 239 |
+
if not driver_recipient:
|
| 240 |
+
raise WhatsAppClientError("Driver recipient is missing")
|
| 241 |
+
driver_phone = driver_recipient.split("@")[0]
|
| 242 |
await self.whatsapp.send_text(
|
| 243 |
+
driver_recipient,
|
| 244 |
_driver_notification_text(
|
| 245 |
customer=self.customer,
|
| 246 |
trip=trip,
|
|
|
|
| 1081 |
return (
|
| 1082 |
"🔔 حجز جديد في فلسا\n"
|
| 1083 |
f"العميل: {customer.get('name') or 'عميل جديد'}\n"
|
| 1084 |
+
f"رقم العميل: {customer.get('phone_number') or 'غير متوفر'}\n"
|
| 1085 |
f"الرحلة: {trip.get('departure')} ← {trip.get('destination')}\n"
|
| 1086 |
f"التاريخ: {trip_departure_date(trip)} {trip_departure_bucket(trip)}\n"
|
| 1087 |
f"المقاعد المطلوبة: {requested_seats}\n"
|
tests/test_tools.py
CHANGED
|
@@ -32,7 +32,11 @@ def trip(
|
|
| 32 |
available_seats=3,
|
| 33 |
status="active",
|
| 34 |
car_type="SUV",
|
|
|
|
| 35 |
):
|
|
|
|
|
|
|
|
|
|
| 36 |
return {
|
| 37 |
"id": trip_id,
|
| 38 |
"departure": departure,
|
|
@@ -43,7 +47,7 @@ def trip(
|
|
| 43 |
"total_seats": 4,
|
| 44 |
"price": "50.00",
|
| 45 |
"status": status,
|
| 46 |
-
"drivers":
|
| 47 |
"driver_cars": {"car_type": car_type},
|
| 48 |
}
|
| 49 |
|
|
@@ -238,6 +242,27 @@ async def test_create_booking_lead_notifies_driver():
|
|
| 238 |
assert whatsapp.sent[0][0] == "967700000009"
|
| 239 |
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
@pytest.mark.asyncio
|
| 242 |
async def test_create_booking_lead_keeps_pending_when_driver_notification_fails():
|
| 243 |
repository = FakeRepository()
|
|
|
|
| 32 |
available_seats=3,
|
| 33 |
status="active",
|
| 34 |
car_type="SUV",
|
| 35 |
+
driver_phone_number=None,
|
| 36 |
):
|
| 37 |
+
drivers = {"name": "Ali", "remoteJid": "967700000009"}
|
| 38 |
+
if driver_phone_number is not None:
|
| 39 |
+
drivers["customers"] = {"phone_number": driver_phone_number}
|
| 40 |
return {
|
| 41 |
"id": trip_id,
|
| 42 |
"departure": departure,
|
|
|
|
| 47 |
"total_seats": 4,
|
| 48 |
"price": "50.00",
|
| 49 |
"status": status,
|
| 50 |
+
"drivers": drivers,
|
| 51 |
"driver_cars": {"car_type": car_type},
|
| 52 |
}
|
| 53 |
|
|
|
|
| 242 |
assert whatsapp.sent[0][0] == "967700000009"
|
| 243 |
|
| 244 |
|
| 245 |
+
@pytest.mark.asyncio
|
| 246 |
+
async def test_create_booking_lead_uses_driver_phone_number():
|
| 247 |
+
repository = FakeRepository()
|
| 248 |
+
repository.trips_by_id["trip-1"] = trip(driver_phone_number="967700000099")
|
| 249 |
+
whatsapp = FakeWhatsApp()
|
| 250 |
+
handlers = make_handlers(
|
| 251 |
+
repository=repository,
|
| 252 |
+
whatsapp=whatsapp,
|
| 253 |
+
customer={"id": "cust-1", "remoteJid": "967700000001", "name": "Mona"},
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
result = await handlers.create_booking_lead(
|
| 257 |
+
{"trip_id": "trip-1", "requested_seats": 1}
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
assert result.ok is True
|
| 261 |
+
assert result.data["driver_notification_status"] == "sent"
|
| 262 |
+
assert whatsapp.sent[0][0] == "967700000099"
|
| 263 |
+
assert result.data["driver_phone"] == "967700000099"
|
| 264 |
+
|
| 265 |
+
|
| 266 |
@pytest.mark.asyncio
|
| 267 |
async def test_create_booking_lead_keeps_pending_when_driver_notification_fails():
|
| 268 |
repository = FakeRepository()
|