66 lines
2.3 KiB
PHP
66 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Me;
|
|
|
|
use App\Http\Controllers\Api\V1\ApiController;
|
|
use App\Models\CollectionLog;
|
|
use App\Models\CollectionTeam;
|
|
use App\Models\TeamMember;
|
|
use App\Models\Trip;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class MyStatsController extends ApiController
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
$role = $user->role ?? '';
|
|
|
|
$stats = [
|
|
'total_trips' => 0,
|
|
'total_volume_kg' => 0,
|
|
'total_scans' => 0,
|
|
];
|
|
|
|
if ($role === 'driver') {
|
|
// Find teams where user is driver
|
|
$teamIds = CollectionTeam::where('driver_id', $user->id)->pluck('id');
|
|
|
|
$stats['total_trips'] = Trip::whereIn('team_id', $teamIds)
|
|
->where('status', Trip::STATUS_COMPLETED)
|
|
->count();
|
|
|
|
$stats['total_volume_kg'] = (int) Trip::whereIn('team_id', $teamIds)
|
|
->where('status', Trip::STATUS_COMPLETED)
|
|
->sum('total_load_kg');
|
|
|
|
} elseif ($role === 'scanner') {
|
|
// Scanner only cares about QR scans and volume
|
|
$stats['total_scans'] = CollectionLog::where('scanned_by_user_id', $user->id)
|
|
->where('verification_status', CollectionLog::STATUS_VALID)
|
|
->count();
|
|
|
|
$stats['total_volume_kg'] = (int) CollectionLog::where('scanned_by_user_id', $user->id)
|
|
->where('verification_status', CollectionLog::STATUS_VALID)
|
|
->sum('weight_kg');
|
|
|
|
} elseif ($role === 'helper') {
|
|
// Helper cares about QR scans (if they scan) and total trips
|
|
$stats['total_scans'] = CollectionLog::where('scanned_by_user_id', $user->id)
|
|
->where('verification_status', CollectionLog::STATUS_VALID)
|
|
->count();
|
|
|
|
$teamIds = TeamMember::where('user_id', $user->id)
|
|
->where('role_in_team', TeamMember::ROLE_HELPER)
|
|
->pluck('team_id');
|
|
|
|
$stats['total_trips'] = Trip::whereIn('team_id', $teamIds)
|
|
->where('status', Trip::STATUS_COMPLETED)
|
|
->count();
|
|
}
|
|
|
|
return $this->ok($stats);
|
|
}
|
|
}
|