Files
Verde-Web/tests/Feature/Api/V1/Scan/ScanTeamIdTest.php
Developer aa8fdaa5aa 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.
2026-07-06 16:00:01 +08:00

65 lines
1.9 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\Scan;
use App\Models\CollectionTeam;
use App\Models\DropOffPoint;
use App\Models\QrCode;
use App\Models\QrCodeBatch;
use App\Models\User;
use App\Services\Qr\BatchGenerator;
use Database\Seeders\RoleSeeder;
use Database\Seeders\SampleDropOffPointsSeeder;
use Database\Seeders\SamplePsgcSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ScanTeamIdTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed([RoleSeeder::class, SamplePsgcSeeder::class, SampleDropOffPointsSeeder::class]);
}
public function test_scan_without_trip_assigns_team_id()
{
$scanner = User::factory()->create(['role' => User::ROLE_SCANNER, 'status' => 'active']);
$team = CollectionTeam::create([
'tenant_id' => 1,
'name' => 'Test Team',
'scanner_id' => $scanner->id,
'status' => CollectionTeam::STATUS_ACTIVE,
]);
$dop = DropOffPoint::first();
$batch = app(BatchGenerator::class)->generate(1, QrCodeBatch::PURPOSE_FREE);
$code = $batch->codes->first();
$code->forceFill([
'assigned_to_household_id' => \App\Models\Household::factory()->create(['barangay_id' => $dop->barangay_id])->id,
'status' => 'active',
])->save();
Sanctum::actingAs($scanner);
$response = $this->postJson('/api/v1/scanner/scan', [
'serial' => $code->serial,
'drop_off_point_id' => $dop->id,
'lat' => $dop->coordinates->latitude,
'lng' => $dop->coordinates->longitude,
'weight_kg' => 5,
]);
$response->assertOk();
$this->assertDatabaseHas('collection_logs', [
'qr_code_id' => $code->id,
'team_id' => $team->id,
'trip_id' => null,
]);
}
}