feat(scanner): update scan handling to return results for invalid QR data and other scan conditions

This commit is contained in:
2026-09-09 12:49:27 -03:00
parent 64e965aff7
commit cac4fcf2b2
2 changed files with 39 additions and 40 deletions

View File

@@ -291,19 +291,19 @@ class ScannerTicketControllerTest extends TestCase
$this->assertNotNull($scanAttempt->resolved_at);
}
public function test_scan_requires_a_uuid_in_the_data_field(): void
public function test_scan_returns_an_attempt_for_invalid_qr_data(): void
{
Sanctum::actingAs($this->scanner);
$this->postJson('/api/v1/scanner/tickets/scan')
->assertUnprocessable()
->assertJsonPath('message', 'El QR proporcionado es inválido.')
->assertJsonPath('errors.data.0', 'El QR proporcionado es inválido.');
->assertOk()
->assertJsonPath('data.scan_attempt.id', fn (mixed $id): bool => is_int($id))
->assertJsonPath('data.scan_attempt.result', ScanAttemptResult::InvalidQr->value);
$this->postJson('/api/v1/scanner/tickets/scan', ['data' => 'not-a-uuid'])
->assertUnprocessable()
->assertJsonPath('message', 'El QR proporcionado es inválido.')
->assertJsonPath('errors.data.0', 'El QR proporcionado es inválido.');
->assertOk()
->assertJsonPath('data.scan_attempt.id', fn (mixed $id): bool => is_int($id))
->assertJsonPath('data.scan_attempt.result', ScanAttemptResult::InvalidQr->value);
$scanAttempts = ScanAttempt::query()->orderBy('id')->get();
$this->assertCount(2, $scanAttempts);
@@ -330,8 +330,9 @@ class ScannerTicketControllerTest extends TestCase
Sanctum::actingAs($otherScanner);
$this->postJson('/api/v1/scanner/tickets/scan', ['data' => $ticket->ticket])
->assertUnprocessable()
->assertJsonValidationErrors('ticket');
->assertOk()
->assertJsonPath('data.scan_attempt.id', fn (mixed $id): bool => is_int($id))
->assertJsonPath('data.scan_attempt.result', ScanAttemptResult::AlreadyScanned->value);
$this->assertSame($this->scanner->id, $ticket->fresh()->scanner_user_id);
$this->assertDatabaseHas('scan_attempts', [
'scanner_user_id' => $otherScanner->id,
@@ -354,8 +355,9 @@ class ScannerTicketControllerTest extends TestCase
Sanctum::actingAs($this->scanner);
$this->postJson('/api/v1/scanner/tickets/scan', ['data' => $ticket->ticket])
->assertUnprocessable()
->assertJsonValidationErrors('ticket');
->assertOk()
->assertJsonPath('data.scan_attempt.id', fn (mixed $id): bool => is_int($id))
->assertJsonPath('data.scan_attempt.result', ScanAttemptResult::CategoryForbidden->value);
$this->assertNull($ticket->fresh()->used_at);
$this->assertDatabaseHas('scan_attempts', [
'scanner_user_id' => $this->scanner->id,
@@ -407,7 +409,9 @@ class ScannerTicketControllerTest extends TestCase
$this->getJson("/api/v1/scanner/tickets/{$foreignTicket->ticket}")
->assertNotFound();
$this->postJson('/api/v1/scanner/tickets/scan', ['data' => $foreignTicket->ticket])
->assertNotFound();
->assertOk()
->assertJsonPath('data.scan_attempt.id', fn (mixed $id): bool => is_int($id))
->assertJsonPath('data.scan_attempt.result', ScanAttemptResult::TicketNotFound->value);
$this->assertDatabaseHas('scan_attempts', [
'tenant_code' => $this->tenant->codigo,
'scanner_user_id' => $this->scanner->id,