fix(employees): normalize scalar siteIds query parameter to array in prepareForValidation
This commit is contained in:
parent
6497b109a2
commit
3c4eeb3014
2 changed files with 42 additions and 0 deletions
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue