fix(scanner): add team_id to collection_logs for robust stats tracking
- Created a database migration to add `team_id` to `collection_logs`. - Created a database migration to backfill `team_id` for past scans using `trip_id` and scanner's active team. - Updated `ScanService` to lookup the scanner's active team and store the `team_id` directly on the `CollectionLog`. - Modified the `CollectionTeam` model's `collectionLogs()` relation to be a direct `HasMany` using the new `team_id` instead of routing through `Trip`. - Updated `AdminTeamReportController` to query metrics directly from `collection_logs.team_id`, ensuring all past and future scans (including those done without an active trip) correctly increment team stats and daily charts.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Services\Scan;
|
||||
|
||||
use App\Models\CollectionLog;
|
||||
use App\Models\CollectionTeam;
|
||||
use App\Models\DropOffPoint;
|
||||
use App\Models\QrCode;
|
||||
use App\Models\Trip;
|
||||
@@ -86,8 +87,13 @@ class ScanService
|
||||
'scanned_by_user_id' => $scanner->id,
|
||||
])->save();
|
||||
|
||||
$team = CollectionTeam::where('scanner_id', $scanner->id)
|
||||
->where('status', CollectionTeam::STATUS_ACTIVE)
|
||||
->first();
|
||||
|
||||
$log = CollectionLog::create([
|
||||
'qr_code_id' => $code->id,
|
||||
'team_id' => $team?->id,
|
||||
'household_id' => $code->assigned_to_household_id,
|
||||
'drop_off_point_id' => $dropOff->id,
|
||||
'scanned_by_user_id' => $scanner->id,
|
||||
|
||||
Reference in New Issue
Block a user