diff --git a/frontend/services/dto/audit-log.ts b/frontend/services/dto/audit-log.ts index 5db48ae..b9ab42c 100644 --- a/frontend/services/dto/audit-log.ts +++ b/frontend/services/dto/audit-log.ts @@ -8,5 +8,9 @@ export type AuditLog = { description: string changes: { old?: Record; new?: Record } | null affectedDate: string | null + ipAddress: string | null + userAgent: string | null + deviceLabel: string | null + deviceId: string | null createdAt: string } diff --git a/src/Repository/AuditLogRepository.php b/src/Repository/AuditLogRepository.php index 8d9ea09..50215a2 100644 --- a/src/Repository/AuditLogRepository.php +++ b/src/Repository/AuditLogRepository.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Repository; use App\Entity\AuditLog; +use App\Repository\Contract\AuditLogReadRepositoryInterface; use DateTimeImmutable; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; @@ -12,7 +13,7 @@ use Doctrine\Persistence\ManagerRegistry; /** * @extends ServiceEntityRepository */ -final class AuditLogRepository extends ServiceEntityRepository +final class AuditLogRepository extends ServiceEntityRepository implements AuditLogReadRepositoryInterface { public function __construct(ManagerRegistry $registry) { diff --git a/src/Repository/Contract/AuditLogReadRepositoryInterface.php b/src/Repository/Contract/AuditLogReadRepositoryInterface.php new file mode 100644 index 0000000..82ece79 --- /dev/null +++ b/src/Repository/Contract/AuditLogReadRepositoryInterface.php @@ -0,0 +1,30 @@ + + */ + public function findByFilters( + ?int $employeeId = null, + ?DateTimeImmutable $from = null, + ?DateTimeImmutable $to = null, + ?string $entityType = null, + int $limit = 50, + int $offset = 0, + ): array; + + public function countByFilters( + ?int $employeeId = null, + ?DateTimeImmutable $from = null, + ?DateTimeImmutable $to = null, + ?string $entityType = null, + ): int; +} diff --git a/src/State/AuditLogProvider.php b/src/State/AuditLogProvider.php index bd8be04..9736030 100644 --- a/src/State/AuditLogProvider.php +++ b/src/State/AuditLogProvider.php @@ -6,7 +6,7 @@ namespace App\State; use ApiPlatform\Metadata\Operation; use ApiPlatform\State\ProviderInterface; -use App\Repository\AuditLogRepository; +use App\Repository\Contract\AuditLogReadRepositoryInterface; use DateTimeImmutable; use DateTimeZone; use Symfony\Component\HttpFoundation\JsonResponse; @@ -18,7 +18,7 @@ class AuditLogProvider implements ProviderInterface public function __construct( private readonly RequestStack $requestStack, - private readonly AuditLogRepository $auditLogRepository, + private readonly AuditLogReadRepositoryInterface $auditLogRepository, ) {} public function provide(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse @@ -60,6 +60,10 @@ class AuditLogProvider implements ProviderInterface 'description' => $log->getDescription(), 'changes' => $log->getChanges(), 'affectedDate' => $log->getAffectedDate()?->format('Y-m-d'), + 'ipAddress' => $log->getIpAddress(), + 'userAgent' => $log->getUserAgent(), + 'deviceLabel' => $log->getDeviceLabel(), + 'deviceId' => $log->getDeviceId(), 'createdAt' => $log->getCreatedAt()->setTimezone(new DateTimeZone('Europe/Paris'))->format('Y-m-d H:i:s'), ]; } diff --git a/tests/State/AuditLogProviderTest.php b/tests/State/AuditLogProviderTest.php new file mode 100644 index 0000000..0b9b442 --- /dev/null +++ b/tests/State/AuditLogProviderTest.php @@ -0,0 +1,51 @@ +setUsername('usine') + ->setAction('create') + ->setEntityType('work_hour') + ->setDescription('desc') + ->setIpAddress('203.0.113.7') + ->setUserAgent('UA-string') + ->setDeviceLabel('Mobile · Android · Chrome') + ->setDeviceId('device-abc') + ; + + $repo = $this->createMock(AuditLogReadRepositoryInterface::class); + $repo->method('countByFilters')->willReturn(1); + $repo->method('findByFilters')->willReturn([$log]); + + $stack = new RequestStack(); + $stack->push(Request::create('/api/audit-logs', 'GET')); + + $provider = new AuditLogProvider($stack, $repo); + $response = $provider->provide($this->createMock(Operation::class)); + + $data = json_decode((string) $response->getContent(), true); + $item = $data['items'][0]; + + self::assertSame('203.0.113.7', $item['ipAddress']); + self::assertSame('UA-string', $item['userAgent']); + self::assertSame('Mobile · Android · Chrome', $item['deviceLabel']); + self::assertSame('device-abc', $item['deviceId']); + } +}