[#SIRH-17] Ajouter un système de log des actions utilisateurs (#9)
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled

| Numéro du ticket | Titre du ticket |
|------------------|-----------------|
|                  |                 |

## Description de la PR

## Modification du .env

## Check list

- [ ] Pas de régression
- [ ] TU/TI/TF rédigée
- [ ] TU/TI/TF OK
- [ ] CHANGELOG modifié

Reviewed-on: #9
Co-authored-by: tristan <tristan@yuno.malio.fr>
Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #9.
This commit is contained in:
2026-03-30 07:52:49 +00:00
committed by Autin
parent e74a264b37
commit 057d6bf06f
26 changed files with 1107 additions and 17 deletions

View File

@@ -14,6 +14,7 @@ use App\Enum\TrackingMode;
use App\Repository\Contract\AbsenceReadRepositoryInterface;
use App\Repository\EmployeeRepository;
use App\Repository\WorkHourRepository;
use App\Service\AuditLogger;
use App\Service\Contracts\EmployeeContractResolver;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
@@ -31,6 +32,7 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
private WorkHourRepository $workHourRepository,
private AbsenceReadRepositoryInterface $absenceRepository,
private EmployeeContractResolver $contractResolver,
private AuditLogger $auditLogger,
) {}
public function process(
@@ -137,9 +139,20 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
$is4hContract = 4 === $contract->getWeeklyHours();
$empName = trim(($employee->getLastName() ?? '').' '.($employee->getFirstName() ?? ''));
if ($this->isEntryEmpty($normalized)) {
// Convention choisie: une ligne vide supprime l'enregistrement existant.
if ($existing) {
$this->auditLogger->log(
$employee,
'delete',
'work_hour',
$existing->getId(),
sprintf('Heures supprimées pour %s le %s', $empName, $data->workDate),
['old' => $this->snapshotWorkHour($existing)],
$workDate,
);
$this->entityManager->remove($existing);
++$result->deleted;
} elseif (($absenceByEmployeeId[$employeeId] ?? false) === true || $is4hContract) {
@@ -163,9 +176,11 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
}
if ($existing) {
$workHour = $existing;
$oldSnapshot = $this->snapshotWorkHour($existing);
$workHour = $existing;
++$result->updated;
} else {
$oldSnapshot = null;
// Upsert: création si aucune ligne n'existe pour (employé, date).
$workHour = new WorkHour()
->setEmployee($employee)
@@ -179,6 +194,23 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
if (!$isAdmin) {
$workHour->setUpdatedAt(new DateTimeImmutable());
}
$newSnapshot = $this->snapshotWorkHour($workHour);
$action = null !== $oldSnapshot ? 'update' : 'create';
$changes = null !== $oldSnapshot
? ['old' => $oldSnapshot, 'new' => $newSnapshot]
: ['new' => $newSnapshot];
$this->auditLogger->log(
$employee,
$action,
'work_hour',
$workHour->getId(),
sprintf('Heures %s pour %s le %s', null !== $oldSnapshot ? 'modifiées' : 'créées', $empName, $data->workDate),
$changes,
$workDate,
);
++$result->processed;
}
@@ -446,6 +478,30 @@ final readonly class WorkHourBulkUpsertProcessor implements ProcessorInterface
;
}
/**
* @return array<string, mixed>
*/
private function snapshotWorkHour(WorkHour $wh): array
{
return [
'morningFrom' => $wh->getMorningFrom(),
'morningTo' => $wh->getMorningTo(),
'afternoonFrom' => $wh->getAfternoonFrom(),
'afternoonTo' => $wh->getAfternoonTo(),
'eveningFrom' => $wh->getEveningFrom(),
'eveningTo' => $wh->getEveningTo(),
'isPresentMorning' => $wh->getIsPresentMorning(),
'isPresentAfternoon' => $wh->getIsPresentAfternoon(),
'dayHoursMinutes' => $wh->getDayHoursMinutes(),
'nightHoursMinutes' => $wh->getNightHoursMinutes(),
'workshopHoursMinutes' => $wh->getWorkshopHoursMinutes(),
'hasBreakfast' => $wh->getHasBreakfast(),
'hasLunch' => $wh->getHasLunch(),
'hasDinner' => $wh->getHasDinner(),
'hasOvernight' => $wh->getHasOvernight(),
];
}
/**
* @param array{
* morningFrom:?string,