Initial V2 commit
This commit is contained in:
148
app/Http/Controllers/ProfileController.php
Normal file
148
app/Http/Controllers/ProfileController.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?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
|
||||
* Replaces: ride/profile/get.php, getCaptainProfile.php, update.php, updateDriverEmail.php
|
||||
*/
|
||||
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->input('_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->input('_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->input('_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'];
|
||||
|
||||
foreach ($encryptableFields as $field) {
|
||||
if ($request->has($field)) {
|
||||
$updates[$field] = $this->enc->encrypt($request->input($field));
|
||||
}
|
||||
}
|
||||
|
||||
$plainFields = ['education', 'employmentType', 'maritalStatus', 'site'];
|
||||
foreach ($plainFields as $field) {
|
||||
if ($request->has($field)) {
|
||||
$updates[$field] = $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->input('_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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user