fix(employee): enforce start_date and align active status rules

This commit is contained in:
Jérémy Munsch 2026-11-15 16:40:00 +01:00
parent 2f76a874d9
commit ee6a6607dc
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
5 changed files with 214 additions and 7 deletions

View file

@ -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`).

View file

@ -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

View file

@ -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);
}
}

View file

@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Models;
use App\Models\Employee;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Support\Facades\Date;
describe('Employee model active status', function (): void {
it('evaluates is_active and active scope consistently across start_date and end_date', function (): void {
// GIVEN
$frozenNow = Date::parse('2026-11-15 10:00:00');
Date::setTestNow($frozenNow);
try {
$createEmployee = static function (
string $matricule,
string $code,
?string $startDate,
?string $endDate,
): Employee {
return Employee::query()->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();
}
});
});

View file

@ -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(