Initial V2 commit
This commit is contained in:
179
app/Http/Controllers/Admin/DriverManagementController.php
Normal file
179
app/Http/Controllers/Admin/DriverManagementController.php
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Helpers\LegacyEncryption;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Admin Driver Management Controller
|
||||
* Replaces: serviceapp/getDriverByPhone.php, getDriverByNational.php,
|
||||
* getDriversWaitingActive.php, getDriverDetailsForActivate.php,
|
||||
* updateDriverToActive.php, registerDriverAndCarService.php, etc.
|
||||
*/
|
||||
class DriverManagementController extends Controller
|
||||
{
|
||||
private LegacyEncryption $enc;
|
||||
|
||||
public function __construct(LegacyEncryption $enc) { $this->enc = $enc; }
|
||||
|
||||
/** GET /v2/admin/drivers?status=waiting&page=1 */
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$status = $request->input('status', 'notDeleted');
|
||||
$page = (int) $request->input('page', 1);
|
||||
$limit = min((int) $request->input('limit', 20), 100);
|
||||
|
||||
$drivers = DB::connection('ride')->table('driver')
|
||||
->where('status', $status)
|
||||
->orderBy('created_at', 'desc')
|
||||
->skip(($page - 1) * $limit)->take($limit)
|
||||
->get();
|
||||
|
||||
// Decrypt fields
|
||||
$drivers = $drivers->map(function ($d) {
|
||||
$arr = (array) $d;
|
||||
return $this->enc->decryptFields($arr, ['first_name', 'last_name', 'phone', 'email', 'national_number']);
|
||||
});
|
||||
|
||||
$total = DB::connection('ride')->table('driver')->where('status', $status)->count();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $drivers,
|
||||
'pagination' => ['page' => $page, 'limit' => $limit, 'total' => $total],
|
||||
]);
|
||||
}
|
||||
|
||||
/** GET /v2/admin/drivers/search?phone=XXX */
|
||||
public function search(Request $request): JsonResponse
|
||||
{
|
||||
$phone = $request->input('phone');
|
||||
$national = $request->input('national_number');
|
||||
|
||||
$query = DB::connection('ride')->table('driver');
|
||||
|
||||
if ($phone) {
|
||||
$encPhone = $this->enc->encrypt($phone);
|
||||
$query->where('phone', $encPhone);
|
||||
}
|
||||
if ($national) {
|
||||
$encNat = $this->enc->encrypt($national);
|
||||
$query->where('national_number', $encNat);
|
||||
}
|
||||
|
||||
$driver = $query->first();
|
||||
if (!$driver) {
|
||||
return response()->json(['status' => 'failure', 'message' => 'Driver not found'], 404);
|
||||
}
|
||||
|
||||
$data = $this->enc->decryptFields((array) $driver, ['first_name', 'last_name', 'phone', 'email', 'national_number', 'address']);
|
||||
unset($data['password'], $data['api_secret']);
|
||||
|
||||
// Attach car info
|
||||
$car = DB::connection('ride')->table('CarRegistration')
|
||||
->where('driverID', $driver->id)->where('isDefault', 1)->first();
|
||||
$data['car'] = $car ? $this->enc->decryptFields((array) $car, ['car_plate', 'owner']) : null;
|
||||
|
||||
// Attach documents
|
||||
$docs = DB::connection('ride')->table('driver_documents')
|
||||
->where('driverID', $driver->id)->get();
|
||||
$data['documents'] = $docs;
|
||||
|
||||
return response()->json(['status' => 'success', 'data' => $data]);
|
||||
}
|
||||
|
||||
/** POST /v2/admin/drivers/{id}/activate */
|
||||
public function activate(Request $request, string $driverId): JsonResponse
|
||||
{
|
||||
DB::connection('ride')->table('driver')
|
||||
->where('id', $driverId)->update(['status' => 'notDeleted']);
|
||||
DB::connection('tracking')->table('driver')
|
||||
->where('id', $driverId)->update(['status' => 'notDeleted']);
|
||||
DB::connection('primary')->table('driver')
|
||||
->where('id', $driverId)->update(['status' => 'notDeleted']);
|
||||
|
||||
return response()->json(['status' => 'success', 'message' => 'Driver activated']);
|
||||
}
|
||||
|
||||
/** POST /v2/admin/drivers/{id}/deactivate */
|
||||
public function deactivate(Request $request, string $driverId): JsonResponse
|
||||
{
|
||||
$reason = $request->input('reason', 'Admin deactivation');
|
||||
|
||||
DB::connection('ride')->table('driver')
|
||||
->where('id', $driverId)->update(['status' => 'Deleted']);
|
||||
DB::connection('tracking')->table('driver')
|
||||
->where('id', $driverId)->update(['status' => 'Deleted']);
|
||||
|
||||
// Add to blacklist
|
||||
DB::connection('ride')->table('blacklist_driver')->insert([
|
||||
'driver_id' => $driverId,
|
||||
'phone' => '',
|
||||
'reason' => $reason,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
return response()->json(['status' => 'success', 'message' => 'Driver deactivated']);
|
||||
}
|
||||
|
||||
/** POST /v2/admin/drivers/{id}/add-car */
|
||||
public function addCar(Request $request, string $driverId): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'car_plate' => 'required|string',
|
||||
'make' => 'required|string',
|
||||
'model' => 'required|string',
|
||||
'year' => 'required|string',
|
||||
'color' => 'required|string',
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'driverID' => $driverId,
|
||||
'vin' => $request->input('vin', ''),
|
||||
'car_plate' => $this->enc->encrypt($request->input('car_plate')),
|
||||
'make' => $request->input('make'),
|
||||
'model' => $request->input('model'),
|
||||
'year' => $request->input('year'),
|
||||
'expiration_date' => $request->input('expiration_date', ''),
|
||||
'color' => $request->input('color'),
|
||||
'owner' => $this->enc->encrypt($request->input('owner', '')),
|
||||
'color_hex' => $request->input('color_hex', ''),
|
||||
'fuel' => $request->input('fuel', ''),
|
||||
'isDefault' => 1,
|
||||
'created_at' => now(),
|
||||
'status' => 'yet',
|
||||
];
|
||||
|
||||
// Insert in all 3 databases
|
||||
DB::connection('ride')->table('CarRegistration')->insert($data);
|
||||
DB::connection('tracking')->table('CarRegistration')->insert($data);
|
||||
DB::connection('primary')->table('CarRegistration')->insert($data);
|
||||
|
||||
return response()->json(['status' => 'success'], 201);
|
||||
}
|
||||
|
||||
/** POST /v2/admin/drivers/{id}/notes */
|
||||
public function addNote(Request $request, string $driverId): JsonResponse
|
||||
{
|
||||
$request->validate(['note' => 'required|string|max:250']);
|
||||
|
||||
// Get driver phone
|
||||
$driver = DB::connection('ride')->table('driver')->where('id', $driverId)->first();
|
||||
$phone = $driver ? $this->enc->decrypt($driver->phone) : '';
|
||||
|
||||
DB::connection('primary')->table('notesForDriverService')->updateOrInsert(
|
||||
['phone' => $phone],
|
||||
[
|
||||
'note' => $request->input('note'),
|
||||
'editor' => $request->input('editor', 'admin'),
|
||||
'createdAt' => now(),
|
||||
]
|
||||
);
|
||||
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user