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: #14 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
75 lines
2.7 KiB
PHP
75 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProviderInterface;
|
|
use App\Repository\AuditLogRepository;
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
class AuditLogProvider implements ProviderInterface
|
|
{
|
|
private const PER_PAGE = 50;
|
|
|
|
public function __construct(
|
|
private readonly RequestStack $requestStack,
|
|
private readonly AuditLogRepository $auditLogRepository,
|
|
) {}
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
|
|
{
|
|
$request = $this->requestStack->getCurrentRequest();
|
|
if (!$request) {
|
|
return new JsonResponse(['items' => [], 'total' => 0]);
|
|
}
|
|
|
|
$employeeId = $request->query->get('employeeId');
|
|
$from = $request->query->get('from');
|
|
$to = $request->query->get('to');
|
|
$entityType = $request->query->get('entityType');
|
|
$page = max(1, (int) $request->query->get('page', '1'));
|
|
|
|
$empId = $employeeId ? (int) $employeeId : null;
|
|
$fromDt = $from ? new DateTimeImmutable($from) : null;
|
|
$toDt = $to ? new DateTimeImmutable($to) : null;
|
|
$type = $entityType ?: null;
|
|
$offset = ($page - 1) * self::PER_PAGE;
|
|
|
|
$total = $this->auditLogRepository->countByFilters($empId, $fromDt, $toDt, $type);
|
|
$logs = $this->auditLogRepository->findByFilters($empId, $fromDt, $toDt, $type, self::PER_PAGE, $offset);
|
|
|
|
$items = [];
|
|
foreach ($logs as $log) {
|
|
$employee = $log->getEmployee();
|
|
$employeeName = $employee
|
|
? trim(($employee->getLastName() ?? '').' '.($employee->getFirstName() ?? ''))
|
|
: null;
|
|
|
|
$items[] = [
|
|
'id' => $log->getId(),
|
|
'employeeName' => $employeeName,
|
|
'employeeId' => $employee?->getId(),
|
|
'username' => $log->getUsername(),
|
|
'action' => $log->getAction(),
|
|
'entityType' => $log->getEntityType(),
|
|
'description' => $log->getDescription(),
|
|
'changes' => $log->getChanges(),
|
|
'affectedDate' => $log->getAffectedDate()?->format('Y-m-d'),
|
|
'createdAt' => $log->getCreatedAt()->setTimezone(new DateTimeZone('Europe/Paris'))->format('Y-m-d H:i:s'),
|
|
];
|
|
}
|
|
|
|
return new JsonResponse([
|
|
'items' => $items,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'perPage' => self::PER_PAGE,
|
|
]);
|
|
}
|
|
}
|