From ee6a6607dcf10d8959993c0ff97359c355e9cd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Sun, 15 Nov 2026 16:40:00 +0100 Subject: [PATCH] fix(employee): enforce start_date and align active status rules --- REGLES METIERS.md | 15 +++ app/Models/Employee.php | 27 +++-- domain/Models/Entities/Shopper.php | 8 +- .../WithApp/App/Models/EmployeeTest.php | 72 ++++++++++++++ .../Domain/Models/Entities/ShopperTest.php | 99 +++++++++++++++++++ 5 files changed, 214 insertions(+), 7 deletions(-) create mode 100644 tests/Integration/WithApp/App/Models/EmployeeTest.php diff --git a/REGLES METIERS.md b/REGLES METIERS.md index 2fed1147..1218ee86 100644 --- a/REGLES METIERS.md +++ b/REGLES METIERS.md @@ -31,3 +31,18 @@ Elle est géré par le service `ProductAvailabilityService`. - Il est éligible INCO (`hasInco = true`). - La date actuelle est supérieure ou égale à `orderable_start` (si défini). - La date actuelle est inférieure à `orderable_end` (si défini). + +## 4. Statut Actif du Salarié + +Elle est vérifiée côté Domaine par `Shopper::isActive()` et côté Eloquent par `Employee::isActive` / `Employee::scopeActive()`. + +- **Date de début (`start_date`)** : + + - Le salarié est actif lorsque `start_date` est inférieure ou égale à la date/heure actuelle (ou si `start_date` est `null` en base de données). + - Un salarié dont la `start_date` est dans le futur est considéré comme **inactif** jusqu'à la date d'effet (`00:00:00` du jour de début). + +- **Date de fin (`end_date`)** : + + - En base de données, `end_date` est stockée au format `DATE` (interprétée à `00:00:00`). + - Le salarié est actif uniquement si `end_date` est `null` ou strictement supérieure à la date/heure actuelle (`end_date > now`). + - **Comportement assumé** : le salarié devient donc inactif dès le début de son dernier jour renseigné (`end_date` à `00:00:00`). diff --git a/app/Models/Employee.php b/app/Models/Employee.php index f7dcd6bc..01efa8a0 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -88,7 +88,14 @@ class Employee extends Model protected function isActive(): Attribute { return Attribute::make( - get: fn (): bool => $this->end_date?->greaterThanOrEqualTo(Date::now()) ?? true + get: function (): bool { + $now = Date::now(); + + $hasStarted = $this->start_date === null || $this->start_date->lessThanOrEqualTo($now); + $hasNotEnded = $this->end_date === null || $this->end_date->greaterThan($now); + + return $hasStarted && $hasNotEnded; + } ); } @@ -98,11 +105,19 @@ class Employee extends Model */ protected function scopeActive(Builder $query): Builder { - return $query->where( - fn ($q) => $q - ->where('end_date', '>=', Date::now()) - ->orWhereNull('end_date') - ); + $now = Date::now(); + + return $query + ->where( + fn (Builder $q): Builder => $q + ->where('start_date', '<=', $now) + ->orWhereNull('start_date') + ) + ->where( + fn (Builder $q): Builder => $q + ->where('end_date', '>', $now) + ->orWhereNull('end_date') + ); } public function routeNotificationForMail(): string diff --git a/domain/Models/Entities/Shopper.php b/domain/Models/Entities/Shopper.php index 0fb42b0a..6b985881 100644 --- a/domain/Models/Entities/Shopper.php +++ b/domain/Models/Entities/Shopper.php @@ -45,6 +45,12 @@ final readonly class Shopper implements Entity public function isActive(): bool { - return $this->endDate === null || $this->endDate->greaterThan(CarbonImmutable::now()); + $now = CarbonImmutable::now(); + + if ($this->startDate->greaterThan($now)) { + return false; + } + + return $this->endDate === null || $this->endDate->greaterThan($now); } } diff --git a/tests/Integration/WithApp/App/Models/EmployeeTest.php b/tests/Integration/WithApp/App/Models/EmployeeTest.php new file mode 100644 index 00000000..cc54756a --- /dev/null +++ b/tests/Integration/WithApp/App/Models/EmployeeTest.php @@ -0,0 +1,72 @@ +create([ + 'matricule' => new Matricule($matricule), + 'code' => new Code($code), + 'nom' => 'Dupont', + 'prenom' => 'Jean', + 'start_date' => $startDate, + 'end_date' => $endDate, + 'etablissement' => new ShopperEstablishmentCode('ETAB01'), + 'site' => new ShopperSiteCode('SITE'), + 'service' => new ShopperServiceCode('101'), + ]); + }; + + $activeNoDates = $createEmployee('1000001', 'ABCDEF1', null, null); + $activeStartedToday = $createEmployee('1000002', 'ABCDEF2', '2026-11-15', null); + $activeInWindow = $createEmployee('1000003', 'ABCDEF3', '2026-11-01', '2026-11-16'); + $inactiveFutureStart = $createEmployee('1000004', 'ABCDEF4', '2026-11-16', null); + $inactiveFutureBoth = $createEmployee('1000005', 'ABCDEF5', '2026-11-16', '2026-11-30'); + $inactiveEndedToday = $createEmployee('1000006', 'ABCDEF6', '2026-11-01', '2026-11-15'); + $inactiveEndedPast = $createEmployee('1000007', 'ABCDEF7', '2026-11-01', '2026-11-14'); + + // WHEN + $activeIds = Employee::query() + ->active() + ->pluck('id') + ->all(); + + // THEN + expect($activeNoDates->is_active)->toBeTrue() + ->and($activeStartedToday->is_active)->toBeTrue() + ->and($activeInWindow->is_active)->toBeTrue() + ->and($inactiveFutureStart->is_active)->toBeFalse() + ->and($inactiveFutureBoth->is_active)->toBeFalse() + ->and($inactiveEndedToday->is_active)->toBeFalse() + ->and($inactiveEndedPast->is_active)->toBeFalse() + ->and($activeIds)->toEqualCanonicalizing([ + $activeNoDates->id, + $activeStartedToday->id, + $activeInWindow->id, + ]); + } finally { + Date::setTestNow(); + } + }); +}); diff --git a/tests/Unit/Domain/Models/Entities/ShopperTest.php b/tests/Unit/Domain/Models/Entities/ShopperTest.php index 50dc9f29..5c37b418 100644 --- a/tests/Unit/Domain/Models/Entities/ShopperTest.php +++ b/tests/Unit/Domain/Models/Entities/ShopperTest.php @@ -98,6 +98,105 @@ describe('Shopper', function (): void { expect($entity->isActive())->toBeTrue(); }); + it('is inactive when startDate is in the future', function (): void { + // GIVEN + $startDate = CarbonImmutable::now()->addDay(); + + // WHEN + $entity = new Shopper( + new ShopperId(Uuid::uuid7()), + new Email('test@example.com'), + new Matricule('1234567'), + 'Nom', + 'Prenom', + $startDate, + null, + new ShopperEstablishmentCode('ETAB01'), + new ShopperSiteCode('SITE'), + new ShopperServiceCode('101') + ); + + // THEN + expect($entity->isActive())->toBeFalse(); + }); + + it('is inactive when startDate is in the future even with future endDate', function (): void { + // GIVEN + $startDate = CarbonImmutable::now()->addDay(); + $endDate = CarbonImmutable::now()->addDays(10); + + // WHEN + $entity = new Shopper( + new ShopperId(Uuid::uuid7()), + new Email('test@example.com'), + new Matricule('1234567'), + 'Nom', + 'Prenom', + $startDate, + $endDate, + new ShopperEstablishmentCode('ETAB01'), + new ShopperSiteCode('SITE'), + new ShopperServiceCode('101') + ); + + // THEN + expect($entity->isActive())->toBeFalse(); + }); + + it('is active when startDate equals now and endDate is null', function (): void { + // GIVEN + $now = CarbonImmutable::parse('2026-11-15 00:00:00'); + CarbonImmutable::setTestNow($now); + + try { + // WHEN + $entity = new Shopper( + new ShopperId(Uuid::uuid7()), + new Email('test@example.com'), + new Matricule('1234567'), + 'Nom', + 'Prenom', + $now, + null, + new ShopperEstablishmentCode('ETAB01'), + new ShopperSiteCode('SITE'), + new ShopperServiceCode('101') + ); + + // THEN + expect($entity->isActive())->toBeTrue(); + } finally { + CarbonImmutable::setTestNow(); + } + }); + + it('is inactive at the start of endDate', function (): void { + // GIVEN + $now = CarbonImmutable::parse('2026-11-15 00:00:01'); + CarbonImmutable::setTestNow($now); + + try { + // WHEN + $entity = new Shopper( + new ShopperId(Uuid::uuid7()), + new Email('test@example.com'), + new Matricule('1234567'), + 'Nom', + 'Prenom', + CarbonImmutable::parse('2026-11-01 00:00:00'), + CarbonImmutable::parse('2026-11-15 00:00:00'), + new ShopperEstablishmentCode('ETAB01'), + new ShopperSiteCode('SITE'), + new ShopperServiceCode('101') + ); + + // THEN + expect($entity->isActive())->toBeFalse(); + } finally { + CarbonImmutable::setTestNow(); + } + }); + it('throws exception if nom is empty', function (): void { // WHEN $action = fn () => new Shopper(