* SPDX-License-Identifier: AGPL-3.0-or-later */ namespace KTXM\ChronoManager\Controllers; use KTXC\Http\Response\JsonResponse; use KTXC\SessionIdentity; use KTXC\SessionTenant; use KTXF\Controller\ControllerAbstract; use KTXF\Routing\Attributes\AuthenticatedRoute; use InvalidArgumentException; use KTXF\Resource\Selector\SourceSelector; use KTXM\ChronoManager\Manager; class ServiceController extends ControllerAbstract { public function __construct( private readonly SessionTenant $tenantIdentity, private readonly SessionIdentity $userIdentity, private Manager $chronoManager, ) {} /** * Retrieve services for a specific user * * @param SourceSelector|null $sources service sources * @param array|null $filter service filter options * @param array|null $sort service sorting options * @param string|null $uid user identifier * * @return JsonResponse */ #[AuthenticatedRoute('/service/list', name: 'chronomanager.service.list', methods: ['POST'])] public function list(?SourceSelector $sources = null, ?array $filter = null, ?array $sort = null, ?string $uid = null): JsonResponse { // authorize request $tenantId = $this->tenantIdentity->identifier(); $userId = $this->userIdentity->identifier(); // retrieve services $responseData = $this->chronoManager->serviceList($tenantId, $userId, $sources, $filter, $sort); return new JsonResponse($responseData, JsonResponse::HTTP_OK); } /** * Confirm if specific services are available for a specific user * * @param SourceSelector $sources service sources * @param string|null $uid user identifier * * @return JsonResponse */ #[AuthenticatedRoute('/service/extant', name: 'chronomanager.service.extant', methods: ['POST'])] public function extant(SourceSelector $sources, ?string $uid = null): JsonResponse { // authorize request $tenantId = $this->tenantIdentity->identifier(); $userId = $this->userIdentity->identifier(); // retrieve services status $responseData = $this->chronoManager->serviceExtant($tenantId, $userId, $sources); return new JsonResponse($responseData, JsonResponse::HTTP_OK); } /** * Fetch specific service for a specific user * * @param string $provider provider identifier * @param string $identifier service identifier * @param string|null $uid user identifier * * @return JsonResponse */ #[AuthenticatedRoute('/service/fetch', name: 'chronomanager.service.fetch', methods: ['POST'])] public function fetch(string $provider, string $identifier, ?string $uid = null): JsonResponse { try { // authorize request $tenantId = $this->tenantIdentity->identifier(); $userId = $this->userIdentity->identifier(); // retrieve service $responseData = $this->chronoManager->serviceFetch($tenantId, $userId, $provider, $identifier); return new JsonResponse($responseData, JsonResponse::HTTP_OK); } catch (InvalidArgumentException $e) { return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST); } } }