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']);
|
||||
}
|
||||
}
|
||||
74
app/Http/Controllers/Admin/PassengerManagementController.php
Normal file
74
app/Http/Controllers/Admin/PassengerManagementController.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Passenger;
|
||||
use App\Helpers\LegacyEncryption;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Admin Passenger Management Controller
|
||||
*/
|
||||
class PassengerManagementController extends Controller
|
||||
{
|
||||
private LegacyEncryption $enc;
|
||||
|
||||
public function __construct(LegacyEncryption $enc)
|
||||
{
|
||||
$this->enc = $enc;
|
||||
}
|
||||
|
||||
/** GET /v2/admin/passengers */
|
||||
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);
|
||||
|
||||
$passengers = DB::connection('primary')->table('passengers')
|
||||
->where('status', $status)
|
||||
->orderBy('created_at', 'desc')
|
||||
->skip(($page - 1) * $limit)
|
||||
->take($limit)
|
||||
->get();
|
||||
|
||||
$passengers = $passengers->map(function ($p) {
|
||||
$arr = (array) $p;
|
||||
return $this->enc->decryptFields($arr, Passenger::ENCRYPTED_FIELDS);
|
||||
});
|
||||
|
||||
$total = DB::connection('primary')->table('passengers')->where('status', $status)->count();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $passengers,
|
||||
'pagination' => ['page' => $page, 'limit' => $limit, 'total' => $total],
|
||||
]);
|
||||
}
|
||||
|
||||
/** GET /v2/admin/passengers/search?phone=XXX */
|
||||
public function search(Request $request): JsonResponse
|
||||
{
|
||||
$phone = $request->input('phone');
|
||||
if (!$phone) {
|
||||
return response()->json(['status' => 'failure', 'message' => 'Phone required'], 400);
|
||||
}
|
||||
|
||||
$encPhone = $this->enc->encrypt($phone);
|
||||
$passenger = DB::connection('primary')->table('passengers')
|
||||
->where('phone', $encPhone)
|
||||
->first();
|
||||
|
||||
if (!$passenger) {
|
||||
return response()->json(['status' => 'failure', 'message' => 'Passenger not found'], 404);
|
||||
}
|
||||
|
||||
$data = $this->enc->decryptFields((array) $passenger, Passenger::ENCRYPTED_FIELDS);
|
||||
unset($data['password'], $data['api_secret']);
|
||||
|
||||
return response()->json(['status' => 'success', 'data' => $data]);
|
||||
}
|
||||
}
|
||||
55
app/Http/Controllers/Admin/RideManagementController.php
Normal file
55
app/Http/Controllers/Admin/RideManagementController.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Admin Ride Management Controller
|
||||
*/
|
||||
class RideManagementController extends Controller
|
||||
{
|
||||
/** GET /v2/admin/rides */
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$status = $request->input('status');
|
||||
$page = (int) $request->input('page', 1);
|
||||
$limit = min((int) $request->input('limit', 20), 100);
|
||||
|
||||
$query = DB::connection('ride')->table('ride');
|
||||
|
||||
if ($status) {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
|
||||
$rides = $query->orderBy('created_at', 'desc')
|
||||
->skip(($page - 1) * $limit)
|
||||
->take($limit)
|
||||
->get();
|
||||
|
||||
$total = $status
|
||||
? DB::connection('ride')->table('ride')->where('status', $status)->count()
|
||||
: DB::connection('ride')->table('ride')->count();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => $rides,
|
||||
'pagination' => ['page' => $page, 'limit' => $limit, 'total' => $total],
|
||||
]);
|
||||
}
|
||||
|
||||
/** GET /v2/admin/rides/{id} */
|
||||
public function show(string $id): JsonResponse
|
||||
{
|
||||
$ride = DB::connection('ride')->table('ride')->where('id', $id)->first();
|
||||
|
||||
if (!$ride) {
|
||||
return response()->json(['status' => 'failure', 'message' => 'Ride not found'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['status' => 'success', 'data' => $ride]);
|
||||
}
|
||||
}
|
||||
85
app/Http/Controllers/Admin/StatsController.php
Normal file
85
app/Http/Controllers/Admin/StatsController.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Admin Stats Controller
|
||||
* Replaces: serviceapp/getRidesStatic.php, getPassengersStatic.php,
|
||||
* getEmployeeStatic.php, getdriverstotalMonthly.php, getEditorStatsCalls.php
|
||||
*/
|
||||
class StatsController extends Controller
|
||||
{
|
||||
/** GET /v2/admin/stats/overview */
|
||||
public function overview(): JsonResponse
|
||||
{
|
||||
$totalDrivers = DB::connection('ride')->table('driver')->count();
|
||||
$activeDrivers = DB::connection('ride')->table('driver')->where('status', 'notDeleted')->count();
|
||||
$totalPassengers = DB::connection('primary')->table('passengers')->count();
|
||||
$activePassengers = DB::connection('primary')->table('passengers')->where('status', 'notDeleted')->count();
|
||||
$totalRides = DB::connection('ride')->table('ride')->count();
|
||||
$finishedRides = DB::connection('ride')->table('ride')->where('status', 'finish')->count();
|
||||
$todayRides = DB::connection('ride')->table('ride')
|
||||
->where('status', 'finish')->whereDate('rideTimeFinish', today())->count();
|
||||
$todayRevenue = DB::connection('ride')->table('ride')
|
||||
->where('status', 'finish')->whereDate('rideTimeFinish', today())
|
||||
->sum('price_for_passenger');
|
||||
$onlineDrivers = DB::connection('tracking')->table('car_locations')
|
||||
->where('status', 'on')->where('updated_at', '>', now()->subMinutes(10))->count();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'data' => [
|
||||
'drivers' => ['total' => $totalDrivers, 'active' => $activeDrivers, 'online' => $onlineDrivers],
|
||||
'passengers' => ['total' => $totalPassengers, 'active' => $activePassengers],
|
||||
'rides' => ['total' => $totalRides, 'finished' => $finishedRides, 'today' => $todayRides],
|
||||
'revenue' => ['today' => round($todayRevenue, 2)],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/** GET /v2/admin/stats/rides?from=2026-01-01&to=2026-04-22 */
|
||||
public function rides(Request $request): JsonResponse
|
||||
{
|
||||
$from = $request->input('from', today()->subDays(30)->toDateString());
|
||||
$to = $request->input('to', today()->toDateString());
|
||||
|
||||
$daily = DB::connection('ride')->table('ride')
|
||||
->selectRaw("DATE(rideTimeFinish) as date, COUNT(*) as count, SUM(price_for_passenger) as revenue")
|
||||
->where('status', 'finish')
|
||||
->whereBetween('rideTimeFinish', [$from . ' 00:00:00', $to . ' 23:59:59'])
|
||||
->groupByRaw('DATE(rideTimeFinish)')
|
||||
->orderBy('date')
|
||||
->get();
|
||||
|
||||
return response()->json(['status' => 'success', 'data' => $daily]);
|
||||
}
|
||||
|
||||
/** GET /v2/admin/stats/drivers-monthly */
|
||||
public function driversMonthly(): JsonResponse
|
||||
{
|
||||
$monthly = DB::connection('ride')->table('driver')
|
||||
->selectRaw("DATE_FORMAT(created_at, '%Y-%m') as month, COUNT(*) as count")
|
||||
->groupByRaw("DATE_FORMAT(created_at, '%Y-%m')")
|
||||
->orderBy('month', 'desc')
|
||||
->limit(12)
|
||||
->get();
|
||||
|
||||
return response()->json(['status' => 'success', 'data' => $monthly]);
|
||||
}
|
||||
|
||||
/** GET /v2/admin/stats/employees */
|
||||
public function employees(): JsonResponse
|
||||
{
|
||||
$employees = DB::connection('ride')->table('employee')
|
||||
->select('id', 'name', 'phone', 'status', 'created_at')
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
return response()->json(['status' => 'success', 'data' => $employees]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user