diff --git a/src/Presentation/Api/V1/Http/Requests/Employees/EmployeesIndexRequest.php b/src/Presentation/Api/V1/Http/Requests/Employees/EmployeesIndexRequest.php index 37a6a6b..5ca3169 100644 --- a/src/Presentation/Api/V1/Http/Requests/Employees/EmployeesIndexRequest.php +++ b/src/Presentation/Api/V1/Http/Requests/Employees/EmployeesIndexRequest.php @@ -42,6 +42,22 @@ final class EmployeesIndexRequest extends FormRequest ]; } + #[\Override] + protected function prepareForValidation(): void + { + if ($this->has('siteIds')) { + $siteIds = $this->input('siteIds'); + if (is_string($siteIds)) { + $trimmed = trim($siteIds); + $this->merge([ + 'siteIds' => $trimmed === '' + ? [] + : (str_contains($trimmed, ',') ? array_map('trim', explode(',', $trimmed)) : [$trimmed]), + ]); + } + } + } + public function sorter(): EmployeeSorter { return new EmployeeSorter($this->sortBy(), $this->direction()); diff --git a/tests/Feature/Api/V1/Employees/EmployeesIndexTest.php b/tests/Feature/Api/V1/Employees/EmployeesIndexTest.php index 8fd0863..1f78551 100644 --- a/tests/Feature/Api/V1/Employees/EmployeesIndexTest.php +++ b/tests/Feature/Api/V1/Employees/EmployeesIndexTest.php @@ -148,6 +148,32 @@ describe('Employees Index Feature API', function (): void { $response->assertJsonCount(2, 'employees'); }); + it('returns 200 OK and filters results for siteIds scalar query filter without brackets', function (): void { + // GIVEN + RuPersonneSirhFactory::truncate(); + + RuPersonneSirhFactory::new()->create([ + 'MATRICULE_FIC' => '01155902', + 'SITE_FIC' => 'LDCSER', + ]); + + RuPersonneSirhFactory::new()->create([ + 'MATRICULE_FIC' => '01155903', + 'SITE_FIC' => 'SLELA', + ]); + + /** @var \Tests\TestCase $test */ + $test = $this; + + // WHEN + $response = $test->getJson('/api/v1/employees?siteIds=LDCSER'); + + // THEN + $response->assertStatus(200); + $response->assertJsonCount(1, 'employees'); + $response->assertJsonPath('employees.0.matricule', '01155902'); + }); + it('ignores database rows with invalid matricules like comments or alphanumeric strings', function (): void { // GIVEN RuPersonneSirhFactory::truncate();