refactor: update OTP system to support user-specific verification tables with legacy encryption and multi-role authentication.
This commit is contained in:
@@ -49,20 +49,31 @@ class RideController extends Controller
|
||||
$request->validate([
|
||||
'start_location' => 'required|string',
|
||||
'end_location' => 'required|string',
|
||||
'start_lat' => 'required|numeric',
|
||||
'start_lng' => 'required|numeric',
|
||||
'end_lat' => 'required|numeric',
|
||||
'end_lng' => 'required|numeric',
|
||||
'price' => 'required|numeric|min:0',
|
||||
'car_type' => 'required|string',
|
||||
'payment_method' => 'required|in:cash,visa',
|
||||
'distance' => 'required|numeric',
|
||||
'price_for_driver' => 'nullable|numeric|min:0',
|
||||
'price_for_passenger' => 'nullable|numeric|min:0',
|
||||
'status' => 'nullable|string',
|
||||
// Socket specific fields
|
||||
'start_lat' => 'nullable|numeric',
|
||||
'start_lng' => 'nullable|numeric',
|
||||
'end_lat' => 'nullable|numeric',
|
||||
'end_lng' => 'nullable|numeric',
|
||||
'start_name' => 'nullable|string',
|
||||
'end_name' => 'nullable|string',
|
||||
'duration_text' => 'nullable|string',
|
||||
'passenger_rating' => 'nullable|numeric',
|
||||
]);
|
||||
|
||||
$passengerId = $request->input('_jwt_user_id');
|
||||
|
||||
// Prevent duplicate active rides
|
||||
$activeRide = Ride::forPassenger($passengerId)->active()->first();
|
||||
$activeRide = DB::connection('ride')->table('ride')
|
||||
->where('passenger_id', $passengerId)
|
||||
->whereIn('status', ['waiting', 'going_to_passenger', 'arrived', 'started'])
|
||||
->first();
|
||||
|
||||
if ($activeRide) {
|
||||
return response()->json([
|
||||
'status' => 'failure',
|
||||
@@ -70,72 +81,71 @@ class RideController extends Controller
|
||||
], 409);
|
||||
}
|
||||
|
||||
// Begin transaction across both databases
|
||||
// Data array as expected by V1 Database
|
||||
$rideData = [
|
||||
'start_location' => $request->input('start_location'),
|
||||
'end_location' => $request->input('end_location'),
|
||||
'date' => now()->toDateString(),
|
||||
'time' => now()->toTimeString(),
|
||||
'endtime' => '00:00:00',
|
||||
'price' => $request->input('price'),
|
||||
'passenger_id' => $passengerId,
|
||||
'driver_id' => '0', // 0 in V1 instead of 'none'
|
||||
'status' => $request->input('status', 'waiting'),
|
||||
'carType' => $request->input('car_type', 'Speed'),
|
||||
'price_for_driver' => $request->input('price_for_driver', $request->input('price')),
|
||||
'price_for_passenger' => $request->input('price_for_passenger', $request->input('price')),
|
||||
'distance' => $request->input('distance'),
|
||||
];
|
||||
|
||||
DB::connection('primary')->beginTransaction();
|
||||
DB::connection('ride')->beginTransaction();
|
||||
|
||||
try {
|
||||
$ride = Ride::create([
|
||||
'start_location' => $request->input('start_location'),
|
||||
'end_location' => $request->input('end_location'),
|
||||
'date' => now()->toDateString(),
|
||||
'time' => now()->toTimeString(),
|
||||
'endtime' => '00:00:00',
|
||||
'price' => $request->input('price'),
|
||||
'passenger_id' => $passengerId,
|
||||
'driver_id' => 'none',
|
||||
'status' => 'waiting',
|
||||
'paymentMethod' => $request->input('payment_method', 'Cash'),
|
||||
'carType' => $request->input('car_type', 'Speed'),
|
||||
'price_for_passenger' => $request->input('price'),
|
||||
'distance' => $request->input('distance'),
|
||||
]);
|
||||
// 1. Insert into Primary DB (Main Server)
|
||||
$insertedId = DB::connection('primary')->table('ride')->insertGetId($rideData);
|
||||
|
||||
// Also insert into waiting rides (for driver search)
|
||||
DB::connection('primary')->table('waitingRides')->insert([
|
||||
'id' => (string) $ride->id,
|
||||
'start_location' => $request->input('start_location'),
|
||||
'start_lat' => $request->input('start_lat'),
|
||||
'start_lng' => $request->input('start_lng'),
|
||||
'end_location' => $request->input('end_location'),
|
||||
'end_lat' => $request->input('end_lat'),
|
||||
'end_lng' => $request->input('end_lng'),
|
||||
'date' => now()->toDateString(),
|
||||
'time' => now()->toTimeString(),
|
||||
'price' => $request->input('price'),
|
||||
'passenger_id' => $passengerId,
|
||||
'status' => 'waiting',
|
||||
'carType' => $request->input('car_type', 'Speed'),
|
||||
'passengerRate' => 5.0,
|
||||
'price_for_passenger' => $request->input('price'),
|
||||
'distance' => $request->input('distance'),
|
||||
'duration' => $request->input('duration', '0'),
|
||||
'payment_method' => $request->input('payment_method', 'cash'),
|
||||
'passenger_wallet' => $request->input('wallet_balance', '0'),
|
||||
]);
|
||||
// 2. Insert into Ride DB (Tracking Server)
|
||||
$rideData['id'] = $insertedId; // Keep IDs perfectly synced
|
||||
DB::connection('ride')->table('ride')->insert($rideData);
|
||||
|
||||
DB::connection('primary')->commit();
|
||||
DB::connection('ride')->commit();
|
||||
|
||||
// Notify nearby drivers via socket
|
||||
$this->socket->sendToLocationServer('new_ride', [
|
||||
'ride_id' => $ride->id,
|
||||
'lat' => $request->input('start_lat'),
|
||||
'lng' => $request->input('start_lng'),
|
||||
'car_type' => $request->input('car_type'),
|
||||
// 3. Broadcast to Marketplace (Location Socket)
|
||||
$this->socket->sendToLocationServer('market_new_ride', [
|
||||
'id' => (string) $insertedId,
|
||||
'start_lat' => $request->input('start_lat'),
|
||||
'start_lng' => $request->input('start_lng'),
|
||||
'price' => (string) $request->input('price'),
|
||||
'carType' => $request->input('car_type'),
|
||||
'startName' => $request->input('start_name', ''),
|
||||
'endName' => $request->input('end_name', ''),
|
||||
'distance' => (string) $request->input('distance'),
|
||||
'duration' => $request->input('duration_text', ''),
|
||||
'passengerRate' => (string) $request->input('passenger_rating', '5.0'),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => ['ride_id' => $ride->id],
|
||||
], 201);
|
||||
// Return exactly the inserted ID as success output (V1 App relies on this)
|
||||
'data' => $insertedId,
|
||||
], 200); // 200 instead of 201 to match V1 expectation
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::connection('primary')->rollBack();
|
||||
DB::connection('ride')->rollBack();
|
||||
|
||||
\Log::error('AddRide Critical Error: ' . $e->getMessage());
|
||||
|
||||
return response()->json([
|
||||
'status' => 'failure',
|
||||
'message' => 'Failed to create ride',
|
||||
'message' => 'Failed to add ride',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* POST /v2/rides/{id}/accept
|
||||
* Replaces: ride/rides/acceptRide.php
|
||||
@@ -190,10 +200,10 @@ class RideController extends Controller
|
||||
$passengerToken = PassengerToken::where('passengerID', $ride->passenger_id)->first();
|
||||
if ($passengerToken) {
|
||||
$decryptedToken = $this->encryption->decrypt($passengerToken->token);
|
||||
$this->fcm->sendToDevice(
|
||||
$this->fcm->sendLocalizedToDevice(
|
||||
$decryptedToken,
|
||||
'تم قبول طلبك',
|
||||
'السائق في الطريق إليك',
|
||||
'ride_accepted_title',
|
||||
'ride_accepted_body',
|
||||
['ride_id' => (string) $rideId, 'status' => 'Applied'],
|
||||
'ride_accepted'
|
||||
);
|
||||
@@ -247,10 +257,10 @@ class RideController extends Controller
|
||||
// Notify passenger
|
||||
$passengerToken = PassengerToken::where('passengerID', $ride->passenger_id)->first();
|
||||
if ($passengerToken) {
|
||||
$this->fcm->sendToDevice(
|
||||
$this->fcm->sendLocalizedToDevice(
|
||||
$this->encryption->decrypt($passengerToken->token),
|
||||
'الرحلة بدأت',
|
||||
'رحلة سعيدة!',
|
||||
'ride_started_title',
|
||||
'ride_started_body',
|
||||
['ride_id' => (string) $rideId, 'status' => 'Begin'],
|
||||
'ride_started'
|
||||
);
|
||||
@@ -288,10 +298,10 @@ class RideController extends Controller
|
||||
|
||||
$passengerToken = PassengerToken::where('passengerID', $ride->passenger_id)->first();
|
||||
if ($passengerToken) {
|
||||
$this->fcm->sendToDevice(
|
||||
$this->fcm->sendLocalizedToDevice(
|
||||
$this->encryption->decrypt($passengerToken->token),
|
||||
'السائق وصل',
|
||||
'السائق في انتظارك',
|
||||
'driver_arrived_title',
|
||||
'driver_arrived_body',
|
||||
['ride_id' => (string) $rideId, 'status' => 'Arrived'],
|
||||
'driver_arrived'
|
||||
);
|
||||
@@ -360,10 +370,10 @@ class RideController extends Controller
|
||||
// Notify passenger
|
||||
$passengerToken = PassengerToken::where('passengerID', $ride->passenger_id)->first();
|
||||
if ($passengerToken) {
|
||||
$this->fcm->sendToDevice(
|
||||
$this->fcm->sendLocalizedToDevice(
|
||||
$this->encryption->decrypt($passengerToken->token),
|
||||
'الرحلة انتهت',
|
||||
'شكراً لاستخدامك انطلق',
|
||||
'ride_finished_title',
|
||||
'ride_finished_body',
|
||||
[
|
||||
'ride_id' => (string) $rideId,
|
||||
'status' => 'finish',
|
||||
@@ -411,17 +421,17 @@ class RideController extends Controller
|
||||
'driverID' => $ride->driver_id ?? 'none',
|
||||
'passengerID' => $passengerId,
|
||||
'rideID' => (string) $rideId,
|
||||
'note' => $request->input('reason', 'nothing'),
|
||||
'note' => $request->input('reason', 'No reason specified'),
|
||||
]);
|
||||
|
||||
// Notify driver if assigned
|
||||
if ($ride->driver_id !== 'none') {
|
||||
$driverToken = DriverToken::where('captain_id', $ride->driver_id)->first();
|
||||
if ($driverToken) {
|
||||
$this->fcm->sendToDevice(
|
||||
$this->fcm->sendLocalizedToDevice(
|
||||
$this->encryption->decrypt($driverToken->token),
|
||||
'تم إلغاء الرحلة',
|
||||
'الراكب ألغى الطلب',
|
||||
'ride_cancelled_title',
|
||||
'ride_cancelled_body_passenger',
|
||||
['ride_id' => (string) $rideId, 'status' => 'CancelByPassenger'],
|
||||
'ride_cancelled'
|
||||
);
|
||||
@@ -467,21 +477,27 @@ class RideController extends Controller
|
||||
'driverID' => $driverId,
|
||||
'passengerID' => $ride->passenger_id,
|
||||
'rideID' => (string) $rideId,
|
||||
'note' => $request->input('reason', 'nothing'),
|
||||
'note' => $request->input('reason', 'No reason specified'),
|
||||
]);
|
||||
|
||||
// Notify passenger
|
||||
// Notify passenger via FCM
|
||||
$passengerToken = PassengerToken::where('passengerID', $ride->passenger_id)->first();
|
||||
if ($passengerToken) {
|
||||
$this->fcm->sendToDevice(
|
||||
$this->fcm->sendLocalizedToDevice(
|
||||
$this->encryption->decrypt($passengerToken->token),
|
||||
'تم إلغاء الرحلة',
|
||||
'السائق ألغى الطلب، جاري البحث...',
|
||||
'ride_cancelled_title',
|
||||
'ride_cancelled_body_driver',
|
||||
['ride_id' => (string) $rideId, 'status' => 'CancelByDriver'],
|
||||
'ride_cancelled'
|
||||
);
|
||||
}
|
||||
|
||||
// Notify passenger via Socket (Faster than FCM)
|
||||
$this->socket->notifyPassenger($ride->passenger_id, [
|
||||
'ride_id' => $rideId,
|
||||
'status' => 'CancelByDriver',
|
||||
]);
|
||||
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user