| Numéro du ticket | Titre du ticket | |------------------|-----------------| | #322 | Page horaire | ## Description de la PR [#322] Page horaire ## Modification du .env ## Check list - [ ] Pas de régression - [ ] TU/TI/TF rédigée - [ ] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: #4 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #4.
This commit is contained in:
110
src/State/WorkHourDayContextProvider.php
Normal file
110
src/State/WorkHourDayContextProvider.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\WorkHourDayContext;
|
||||
use App\Dto\WorkHours\DayContextRow;
|
||||
use App\Entity\User;
|
||||
use App\Repository\Contract\AbsenceReadRepositoryInterface;
|
||||
use App\Repository\Contract\EmployeeScopedRepositoryInterface;
|
||||
use App\Service\WorkHours\AbsenceSegmentsResolver;
|
||||
use App\Service\WorkHours\WorkedHoursCreditPolicy;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
final readonly class WorkHourDayContextProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private Security $security,
|
||||
private RequestStack $requestStack,
|
||||
private EmployeeScopedRepositoryInterface $employeeRepository,
|
||||
private AbsenceReadRepositoryInterface $absenceRepository,
|
||||
private AbsenceSegmentsResolver $absenceSegmentsResolver,
|
||||
private WorkedHoursCreditPolicy $workedHoursCreditPolicy,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): WorkHourDayContext
|
||||
{
|
||||
$user = $this->security->getUser();
|
||||
// Endpoint protégé: on exige un utilisateur authentifié.
|
||||
if (!$user instanceof User) {
|
||||
throw new AccessDeniedHttpException('Authentication required.');
|
||||
}
|
||||
|
||||
$workDate = $this->resolveWorkDate();
|
||||
$employees = $this->employeeRepository->findScoped($user);
|
||||
$absences = $this->absenceRepository->findByDateAndEmployees($workDate, $employees);
|
||||
|
||||
$rowsByEmployeeId = [];
|
||||
foreach ($employees as $employee) {
|
||||
$employeeId = $employee->getId();
|
||||
if (!$employeeId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// On initialise toutes les lignes, même sans absence ce jour-là.
|
||||
$rowsByEmployeeId[$employeeId] = new DayContextRow(employeeId: $employeeId);
|
||||
}
|
||||
|
||||
$dateKey = $workDate->format('Y-m-d');
|
||||
foreach ($absences as $absence) {
|
||||
$employeeId = $absence->getEmployee()?->getId();
|
||||
// Ignore les absences orphelines ou hors scope utilisateur.
|
||||
if (!$employeeId || !isset($rowsByEmployeeId[$employeeId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
[$absentMorning, $absentAfternoon] = $this->absenceSegmentsResolver->resolveForDate($absence, $dateKey);
|
||||
// Pas de segment absent sur ce jour: rien à injecter dans la ligne.
|
||||
if (!$absentMorning && !$absentAfternoon) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calcule le crédit d'heures selon la politique métier (type d'absence + contrat).
|
||||
$creditedMinutes = $this->workedHoursCreditPolicy->computeCreditedMinutes($absence, $dateKey, $absentMorning, $absentAfternoon);
|
||||
$creditedPresenceUnits = $this->workedHoursCreditPolicy->computeCreditedPresenceUnits($absence, $absentMorning, $absentAfternoon);
|
||||
$rowsByEmployeeId[$employeeId]->addAbsence(
|
||||
label: $absence->getType()?->getLabel(),
|
||||
morning: $absentMorning,
|
||||
afternoon: $absentAfternoon,
|
||||
creditedMinutes: $creditedMinutes,
|
||||
creditedPresenceUnits: $creditedPresenceUnits
|
||||
);
|
||||
}
|
||||
|
||||
$response = new WorkHourDayContext();
|
||||
$response->workDate = $dateKey;
|
||||
$response->rows = array_map(
|
||||
static fn (DayContextRow $row): array => $row->toArray(),
|
||||
array_values($rowsByEmployeeId)
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function resolveWorkDate(): DateTimeImmutable
|
||||
{
|
||||
$query = $this->requestStack->getCurrentRequest()?->query;
|
||||
$raw = (string) ($query?->get('workDate') ?? '');
|
||||
|
||||
// Sans paramètre, on cible la date du jour.
|
||||
if ('' === $raw) {
|
||||
return new DateTimeImmutable('today');
|
||||
}
|
||||
|
||||
$date = DateTimeImmutable::createFromFormat('Y-m-d', $raw);
|
||||
// Validation stricte du format pour éviter les ambiguïtés de parsing.
|
||||
if (!$date || $date->format('Y-m-d') !== $raw) {
|
||||
throw new UnprocessableEntityHttpException('workDate must use Y-m-d format.');
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user