From d135c74f8ad5e8631da524c2ed6cddee1f809bfd Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Tue, 1 Oct 2024 15:05:25 +0200 Subject: [PATCH] refs #726. Integration UDS. Success Put info --- config/api_platform/RemoteCalendar.yaml | 6 + .../UDS/RemoteCalendarSyncUdsAction.php | 34 ++++ src/Entity/RemoteCalendar.php | 17 ++ src/Service/UDS/UDSClient.php | 165 +++++++++++++----- 4 files changed, 179 insertions(+), 43 deletions(-) create mode 100644 src/Controller/UDS/RemoteCalendarSyncUdsAction.php diff --git a/config/api_platform/RemoteCalendar.yaml b/config/api_platform/RemoteCalendar.yaml index 9585ffe..4bb8e36 100644 --- a/config/api_platform/RemoteCalendar.yaml +++ b/config/api_platform/RemoteCalendar.yaml @@ -23,6 +23,12 @@ resources: ApiPlatform\Metadata\Post: ~ ApiPlatform\Metadata\Delete: ~ + sync_uds: + class: ApiPlatform\Metadata\Post + method: POST + uriTemplate: /remote-calendars/{uuid}/sync-uds + controller: App\Controller\UDS\RemoteCalendarSyncUdsAction + properties: App\Entity\RemoteCalendar: id: diff --git a/src/Controller/UDS/RemoteCalendarSyncUdsAction.php b/src/Controller/UDS/RemoteCalendarSyncUdsAction.php new file mode 100644 index 0000000..019534b --- /dev/null +++ b/src/Controller/UDS/RemoteCalendarSyncUdsAction.php @@ -0,0 +1,34 @@ +udsClient->__invoke(); + + return new JsonResponse($data, Response::HTTP_OK); + + } +} \ No newline at end of file diff --git a/src/Entity/RemoteCalendar.php b/src/Entity/RemoteCalendar.php index a0773e5..c06e599 100644 --- a/src/Entity/RemoteCalendar.php +++ b/src/Entity/RemoteCalendar.php @@ -83,4 +83,21 @@ class RemoteCalendar extends AbstractEntity return $this; } + + public function isRemoteAvailable(): bool + { + $now = new \DateTime(); + + foreach ($this->getRules() as $rule) { + $dayOfWeek = (int) $now->format('w'); + $adjustedDayOfWeek = ($dayOfWeek + 6) % 7; + if (!in_array($adjustedDayOfWeek, $rule->getBusyWeekdays())) { + return false; + } + + $currentTime = $now->format('H:i'); + + return ($currentTime >= $rule->getBusyFromHour() && $currentTime <= $rule->getBusyToHour()); + } + } } diff --git a/src/Service/UDS/UDSClient.php b/src/Service/UDS/UDSClient.php index fa7fde2..364c62f 100644 --- a/src/Service/UDS/UDSClient.php +++ b/src/Service/UDS/UDSClient.php @@ -3,22 +3,31 @@ namespace App\Service\UDS; use AllowDynamicProperties; +use App\Entity\OrganizationalUnit; use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\HttpClient\Exception\TransportException; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; #[AllowDynamicProperties] class UDSClient { + private string $token; + + private string $scrambler; + public function __construct( - private HttpClientInterface $httpClient, - EntityManagerInterface $entityManager, - private string $udsAPIurl, - private string $udsAuthLogin, - private string $udsAuthUsername, - private string $udsAuthPassword, - string $token, - string $scrambler + private HttpClientInterface $httpClient, + private readonly EntityManagerInterface $entityManager, + private string $udsAPIurl, + private string $udsAuthLogin, + private string $udsAuthUsername, + private string $udsAuthPassword, ) { } @@ -26,9 +35,35 @@ class UDSClient /** * @throws TransportExceptionInterface */ - public function __invoke(): void + public function __invoke(): array { $this->login(); + $servicePools = $this->getServicePools(); + + foreach ($servicePools as $servicePool) { + $servicePoolInfo = $this->getServicePool($servicePool['provider_id'], $servicePool['service_id']); + + if (!$servicePoolInfo) { + continue; + } + + $classroom = $servicePoolInfo['ou']; + $maxAvailableSeats = $this->getMaxAvailableSeats($classroom); + $servicePool['max_srvs'] = 10; + $servicePool['osmanager_id'] = null; + + $response = $this->httpClient->request('PUT', $this->udsAPIurl . 'servicespools/' . $servicePool['id'], [ + 'verify_host' => false, + 'headers' => [ + 'X-Auth-Token' => $this->token, + 'Content-Type' => 'application/json', + 'Scrambler' => $this->scrambler + ], + 'body' => json_encode($servicePool) + ]); + + return json_decode($response->getContent(false), true); + } } /** @@ -36,17 +71,28 @@ class UDSClient */ public function login(): void { - $response = $this->httpClient->request('POST', $this->udsAPIurl . '/auth/login', [ - 'json' => [ - 'login' => $this->udsAuthLogin, - 'username' => $this->udsAuthUsername, - 'password' => $this->udsAuthPassword - ] - ]); + try { + $response = $this->httpClient->request('POST', $this->udsAPIurl . 'auth/login', [ + 'verify_host' => false, + 'headers' => [ + 'Content-Type' => 'application/json' + ], + 'json' => [ + 'auth' => $this->udsAuthLogin, + 'username' => $this->udsAuthUsername, + 'password' => $this->udsAuthPassword + ] + ]); - $data = json_decode($response->getContent(), true); - $this->token = $data['token']; - $this->scrambler = $data['scrambler']; + $data = json_decode($response->getContent(false), true); + + $this->token = $data['token']; + $this->scrambler = $data['scrambler']; + + } catch (TransportExceptionInterface $e) { + throw new TransportException('Error while logging in'); + } catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) { + } } /** @@ -54,35 +100,68 @@ class UDSClient */ public function getServicePools(): array { - $response = $this->httpClient->request('GET', $this->udsAPIurl . '/servicespools/overview', [ - 'headers' => [ - 'X-Auth-Token' => $this->token, - 'Content-Type' => 'application/json', - 'Scrambler' => $this->scrambler - ] - ]); + try { + $response = $this->httpClient->request('GET', $this->udsAPIurl . '/servicespools/overview', [ + 'verify_host' => false, + 'headers' => [ + 'X-Auth-Token' => $this->token, + 'Content-Type' => 'application/json', + 'Scrambler' => $this->scrambler + ] + ]); + return json_decode($response->getContent(), true); + } catch (TransportExceptionInterface $e) { + throw new TransportException('Error while fetching service pools'); + } catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) { + } } - public function getServicePool(string $providerId, string $serviceId): array + /** + * @throws TransportExceptionInterface + */ + public function getServicePool(string $providerId, string $serviceId): ?array { - $response = $this->httpClient->request('GET', $this->udsAPIurl . '/providers/' . $providerId .'/services/'. $serviceId, [ - 'headers' => [ - 'X-Auth-Token' => $this->token, - 'Content-Type' => 'application/json', - 'Scrambler' => $this->scrambler - ] - ]); + try { + $response = $this->httpClient->request('GET', $this->udsAPIurl . 'providers/' . $providerId .'/services/'. $serviceId, [ + 'verify_host' => false, + 'headers' => [ + 'X-Auth-Token' => $this->token, + 'Content-Type' => 'application/json', + 'Scrambler' => $this->scrambler + ] + ]); + + return json_decode($response->getContent(), true); + } catch (TransportExceptionInterface $e) { + throw new TransportException('Error while fetching service pool'); + } catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) { + } + + return null; } - public function setServicePool(string $serviceId, array $payload): void + public function getMaxAvailableSeats(int $organizationalUnitId): int { - $response = $this->httpClient->request('PUT', $this->udsAPIurl . '/servicespools/' . $serviceId, [ - 'headers' => [ - 'X-Auth-Token' => $this->token, - 'Content-Type' => 'application/json', - 'Scrambler' => $this->scrambler - ], - 'json' => $payload - ]); + $organizationalUnit = $this->entityManager->getRepository(OrganizationalUnit::class)->find($organizationalUnitId); + + if (!$organizationalUnit) { + return 0; + } + + $remoteCalendar = $organizationalUnit->getRemoteCalendar(); + if (!$remoteCalendar || !$remoteCalendar->isRemoteAvailable()) { + return 0; + } + + $totalAvailableClients = 0; + + foreach ($organizationalUnit->getClients() as $client) { + $status = $client->getStatus(); + + $clientAvailable = in_array($status, ['active', 'windows', 'linux']); + $totalAvailableClients += $clientAvailable ? 1 : 0; + } + + return $totalAvailableClients; } } \ No newline at end of file