feat(staff): expose scanner attempt history

This commit is contained in:
2026-09-08 10:56:27 -03:00
parent 531dbe52b9
commit 002f08a8fa
5 changed files with 245 additions and 2 deletions

View File

@@ -7,10 +7,13 @@ use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\ResetPasswordAttempt;
use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Notification\Events\PasswordResetRequested;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use App\Domains\Ticket\Enums\ScanAttemptResult;
use App\Domains\Ticket\Models\ScanAttempt;
use App\Domains\Ticket\Models\Ticket;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -264,6 +267,159 @@ class StaffControllerTest extends TestCase
$this->getJson('/api/v1/adminapp/tenant/staff')->assertForbidden();
}
public function test_adminapp_can_search_staff_scan_attempts_by_ticket_id_category_and_date(): void
{
$scanner = User::factory()->create([
'rol_codigo' => RoleCode::Scanner->value,
'tenant_codigo' => $this->tenant->codigo,
]);
$otherScanner = User::factory()->create([
'rol_codigo' => RoleCode::Scanner->value,
'tenant_codigo' => $this->tenant->codigo,
]);
$category = $this->createCategory('Alojamiento');
$catalogItem = CatalogItem::query()->create([
'tenant_code' => $this->tenant->codigo,
'category_id' => $category->id,
'slug' => 'alojamiento-test',
'nombre' => 'Hotel',
'precio' => 100,
]);
$ticket = Ticket::query()->create([
'tenant_code' => $this->tenant->codigo,
'ticket' => (string) Str::uuid(),
'source_catalog_item_id' => $catalogItem->id,
'user_id' => $this->admin->id,
]);
$matching = $this->createScanAttempt($scanner, 'matching-qr', [
'ticket_id' => $ticket->id,
'created_at' => '2026-08-11 14:30:00',
]);
$this->createScanAttempt($scanner, 'another-qr', [
'created_at' => '2026-08-22 22:22:22',
]);
$this->createScanAttempt($otherScanner, 'matching-qr', [
'ticket_id' => $ticket->id,
'created_at' => '2026-08-11 14:30:00',
]);
Sanctum::actingAs($this->admin);
$this->getJson(
"/api/v1/adminapp/tenant/staff/{$scanner->id}/scan-attempts?q={$ticket->id}"
)->assertOk()
->assertJsonFragment([
'id' => $matching->id,
'ticket_id' => $ticket->id,
]);
$assertSingleMatchingAttempt = function (string $search) use ($scanner, $matching, $ticket): void {
$this->getJson(
"/api/v1/adminapp/tenant/staff/{$scanner->id}/scan-attempts?q=".urlencode($search).'&per_page=1'
)
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $matching->id)
->assertJsonPath('data.0.ticket_id', $ticket->id)
->assertJsonPath('data.0.category', 'Alojamiento')
->assertJsonPath('data.0.result_label', 'Verificado')
->assertJsonPath('meta.current_page', 1)
->assertJsonPath('meta.per_page', 1)
->assertJsonPath('meta.total', 1);
};
$assertSingleMatchingAttempt('alojamiento');
$assertSingleMatchingAttempt('11/08/26');
}
public function test_adminapp_cannot_list_scan_attempts_for_non_scanner_staff(): void
{
$customer = User::factory()->create([
'rol_codigo' => RoleCode::User->value,
'tenant_codigo' => $this->tenant->codigo,
]);
Sanctum::actingAs($this->admin);
$this->getJson("/api/v1/adminapp/tenant/staff/{$customer->id}/scan-attempts")
->assertNotFound();
}
public function test_adminapp_can_search_staff_scan_attempts_by_day_and_month_across_years(): void
{
$scanner = User::factory()->create([
'rol_codigo' => RoleCode::Scanner->value,
'tenant_codigo' => $this->tenant->codigo,
]);
$firstMatch = $this->createScanAttempt($scanner, 'first-match', [
'created_at' => '2024-09-07 10:00:00',
]);
$secondMatch = $this->createScanAttempt($scanner, 'second-match', [
'created_at' => '2026-09-07 10:00:00',
]);
$this->createScanAttempt($scanner, 'different-day', [
'created_at' => '2026-09-08 10:00:00',
]);
Sanctum::actingAs($this->admin);
$this->getJson("/api/v1/adminapp/tenant/staff/{$scanner->id}/scan-attempts?q=07%2F09")
->assertOk()
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.id', $secondMatch->id)
->assertJsonPath('data.1.id', $firstMatch->id)
->assertJsonPath('meta.total', 2);
}
public function test_adminapp_datetime_search_matches_text_in_any_datetime_component(): void
{
$scanner = User::factory()->create([
'rol_codigo' => RoleCode::Scanner->value,
'tenant_codigo' => $this->tenant->codigo,
]);
$matches = [
$this->createScanAttempt($scanner, 'year-match', [
'created_at' => '2007-11-12 10:00:08',
]),
$this->createScanAttempt($scanner, 'month-match', [
'created_at' => '2026-07-12 10:00:08',
]),
$this->createScanAttempt($scanner, 'day-match', [
'created_at' => '2026-11-07 10:00:08',
]),
$this->createScanAttempt($scanner, 'seconds-match', [
'created_at' => '2026-11-12 10:00:07',
]),
];
$this->createScanAttempt($scanner, 'no-match', [
'created_at' => '2026-11-12 10:00:08',
]);
Sanctum::actingAs($this->admin);
$response = $this->getJson(
"/api/v1/adminapp/tenant/staff/{$scanner->id}/scan-attempts?q=07"
)->assertOk()
->assertJsonCount(4, 'data')
->assertJsonPath('meta.total', 4);
foreach ($matches as $match) {
$response->assertJsonFragment(['id' => $match->id]);
}
}
/** @param array<string, mixed> $attributes */
private function createScanAttempt(User $scanner, string $data, array $attributes = []): ScanAttempt
{
$attempt = new ScanAttempt;
$attempt->forceFill(array_merge([
'tenant_code' => $this->tenant->codigo,
'scanner_user_id' => $scanner->id,
'data' => $data,
'result' => ScanAttemptResult::Accepted,
'resolved_at' => now(),
], $attributes));
$attempt->save();
return $attempt;
}
private function createAttachment(string $filename): Attachment
{
return Attachment::query()->create([