152 lines
4.7 KiB
PHP
152 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Driver;
|
|
use App\Models\Passenger;
|
|
use App\Models\CarRegistration;
|
|
use App\Models\ImageProfileCaptain;
|
|
use App\Helpers\LegacyEncryption;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* متحكم الملف الشخصي (Profile Controller)
|
|
*
|
|
* الغرض من الملف:
|
|
* إدارة البيانات الشخصية للمستخدمين (سائقين وركاب).
|
|
*
|
|
* كيفية العمل:
|
|
* 1. يعرض بيانات المستخدم الحالية بعد فك تشفير الحقول الحساسة.
|
|
* 2. يسمح للمستخدم بتحديث بياناته مثل الاسم أو البريد الإلكتروني.
|
|
*/
|
|
class ProfileController extends Controller
|
|
{
|
|
private LegacyEncryption $enc;
|
|
|
|
public function __construct(LegacyEncryption $enc)
|
|
{
|
|
$this->enc = $enc;
|
|
}
|
|
|
|
/**
|
|
* GET /v2/profile/passenger
|
|
*/
|
|
public function passenger(Request $request): JsonResponse
|
|
{
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$passenger = Passenger::active()->find($id);
|
|
|
|
if (!$passenger) {
|
|
return response()->json(['status' => 'failure', 'message' => 'Not found'], 404);
|
|
}
|
|
|
|
$data = $passenger->toArray();
|
|
$data = $this->enc->decryptFields($data, Passenger::ENCRYPTED_FIELDS);
|
|
unset($data['password'], $data['api_secret']);
|
|
|
|
// Attach wallet balance
|
|
$wallet = DB::connection('primary')->table('passengerWallet')
|
|
->where('passenger_id', $id)->first();
|
|
$data['wallet_balance'] = $wallet->balance ?? '0.00';
|
|
|
|
// Attach rating
|
|
$rating = DB::connection('primary')->table('ratingPassenger')
|
|
->where('passenger_id', $id)->avg('rating');
|
|
$data['rating'] = round($rating ?? 5.0, 2);
|
|
|
|
return response()->json(['status' => 'success', 'data' => $data]);
|
|
}
|
|
|
|
/**
|
|
* GET /v2/profile/driver
|
|
*/
|
|
public function driver(Request $request): JsonResponse
|
|
{
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$driver = Driver::active()->byId($id)->first();
|
|
|
|
if (!$driver) {
|
|
return response()->json(['status' => 'failure', 'message' => 'Not found'], 404);
|
|
}
|
|
|
|
$data = $driver->toArray();
|
|
$data = $this->enc->decryptFields($data, Driver::ENCRYPTED_FIELDS);
|
|
unset($data['password'], $data['api_secret']);
|
|
|
|
// Car info
|
|
$car = CarRegistration::where('driverID', $id)->where('isDefault', 1)->first();
|
|
if ($car) {
|
|
$carData = $car->toArray();
|
|
$data['car'] = $this->enc->decryptFields($carData, CarRegistration::ENCRYPTED_FIELDS);
|
|
}
|
|
|
|
// Profile image
|
|
$image = ImageProfileCaptain::where('driverID', $id)->first();
|
|
$data['profile_image'] = $image->link ?? null;
|
|
|
|
// Rating
|
|
$data['rating'] = $driver->getAverageRating();
|
|
|
|
// Ride count
|
|
$data['ride_count'] = DB::connection('ride')->table('ride')
|
|
->where('driver_id', $id)->where('status', 'finish')->count();
|
|
|
|
return response()->json(['status' => 'success', 'data' => $data]);
|
|
}
|
|
|
|
/**
|
|
* PUT /v2/profile/passenger
|
|
*/
|
|
public function updatePassenger(Request $request): JsonResponse
|
|
{
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$passenger = Passenger::active()->find($id);
|
|
|
|
if (!$passenger) {
|
|
return response()->json(['status' => 'failure', 'message' => 'Not found'], 404);
|
|
}
|
|
|
|
$updates = [];
|
|
$encryptableFields = [
|
|
'first_name', 'last_name', 'gender', 'birthdate', 'sosPhone',
|
|
'site', 'education', 'employmentType', 'maritalStatus'
|
|
];
|
|
|
|
foreach ($encryptableFields as $field) {
|
|
if ($request->has($field)) {
|
|
$updates[$field] = $this->enc->encrypt($request->input($field));
|
|
}
|
|
}
|
|
|
|
|
|
if (!empty($updates)) {
|
|
$passenger->update($updates);
|
|
}
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Profile updated']);
|
|
}
|
|
|
|
/**
|
|
* PUT /v2/profile/driver/email
|
|
*/
|
|
public function updateDriverEmail(Request $request): JsonResponse
|
|
{
|
|
$request->validate(['email' => 'required|email']);
|
|
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$driver = Driver::active()->byId($id)->first();
|
|
|
|
if (!$driver) {
|
|
return response()->json(['status' => 'failure', 'message' => 'Not found'], 404);
|
|
}
|
|
|
|
$driver->update([
|
|
'email' => $this->enc->encrypt($request->input('email')),
|
|
]);
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Email updated']);
|
|
}
|
|
}
|