|
8 | 8 | use App\Domain\Integrations\Exceptions\KeycloakClientNotFound; |
9 | 9 | use App\Domain\UdbUuid; |
10 | 10 | use App\Keycloak\Client; |
| 11 | +use App\Keycloak\Repositories\KeycloakClientRepository; |
11 | 12 | use App\Search\Sapi3\SearchService; |
12 | 13 | use App\UiTPAS\Dto\UiTPASPermission; |
13 | 14 | use App\UiTPAS\UiTPASApiInterface; |
14 | 15 | use App\UiTPAS\UiTPASConfig; |
15 | 16 | use CultuurNet\SearchV3\ValueObjects\Organizer as SapiOrganizer; |
| 17 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
16 | 18 | use Illuminate\Support\Collection; |
17 | 19 |
|
18 | 20 | final readonly class GetIntegrationOrganizersWithTestOrganizer |
19 | 21 | { |
20 | 22 | public function __construct( |
21 | | - private SearchService $searchClient, |
| 23 | + private SearchService $testSearchService, |
| 24 | + private SearchService $prodSearchService, |
22 | 25 | private UiTPASApiInterface $UiTPASApi, |
23 | 26 | private ClientCredentialsContext $testCredentialsContext, |
24 | 27 | private ClientCredentialsContext $prodCredentialsContext, |
| 28 | + private KeycloakClientRepository $keycloakClientRepository, |
25 | 29 | ) { |
26 | 30 | } |
27 | 31 |
|
28 | 32 | public function getAndEnrichOrganisations(Integration $integration): Collection |
29 | 33 | { |
30 | | - $organizerIds = collect($integration->udbOrganizers())->map(fn (UdbOrganizer $organizer) => $organizer->organizerId); |
31 | | - $UiTPASOrganizers = $this->searchClient->findOrganizers(...$organizerIds)->getMember()?->getItems(); |
32 | | - $keycloakClient = $this->getClientByEnv($integration, Environment::Production); |
| 34 | + $prodOrganizers = $testOrganizers = []; |
| 35 | + $keycloakClientCache = []; |
| 36 | + foreach ($integration->udbOrganizers() as $udbOrganizer) { |
| 37 | + //@todo this can be simplified once client id can no longer be null |
| 38 | + try { |
| 39 | + if ($udbOrganizer->clientId === null) { |
| 40 | + $prodOrganizers[] = $udbOrganizer; |
| 41 | + continue; |
| 42 | + } |
33 | 43 |
|
34 | | - $organizers = collect($UiTPASOrganizers)->map(function (SapiOrganizer $organizer) use ($keycloakClient) { |
35 | | - $id = explode('/', $organizer->getId() ?? ''); |
36 | | - $id = $id[count($id) - 1]; |
| 44 | + $keycloakClient = $this->keycloakClientRepository->getById($udbOrganizer->clientId); |
| 45 | + $keycloakClientCache[$udbOrganizer->clientId->toString()] = $keycloakClient; |
37 | 46 |
|
38 | | - return [ |
39 | | - 'id' => $id, |
40 | | - 'name' => $organizer->getName()?->getValues() ?? [], |
41 | | - 'status' => 'Live', |
42 | | - 'permissions' => $keycloakClient ? $this->getLabels($this->UiTPASApi->fetchPermissions( |
43 | | - $this->prodCredentialsContext, |
44 | | - new UdbUuid($id), |
45 | | - $keycloakClient->clientId |
46 | | - )) : [], |
47 | | - ]; |
48 | | - }); |
| 47 | + if ($keycloakClient->environment === Environment::Production) { |
| 48 | + $prodOrganizers[] = $udbOrganizer; |
| 49 | + } else { |
| 50 | + $testOrganizers[] = $udbOrganizer; |
| 51 | + } |
| 52 | + } catch (ModelNotFoundException) { |
| 53 | + $prodOrganizers[] = $udbOrganizer; |
| 54 | + } |
| 55 | + } |
49 | 56 |
|
50 | | - $keycloakClient = $this->getClientByEnv($integration, Environment::Testing); |
51 | | - $orgTestId = (string)config(UiTPASConfig::TEST_ORGANISATION->value); |
52 | | - $organizers->push([ |
53 | | - 'id' => $orgTestId, |
54 | | - 'name' => ['nl' => 'UiTPAS Organisatie (Regio Gent + Paspartoe)'], |
55 | | - 'status' => 'Test', |
56 | | - 'permissions' => $keycloakClient ? $this->getLabels($this->UiTPASApi->fetchPermissions( |
57 | | - $this->testCredentialsContext, |
58 | | - new UdbUuid($orgTestId), |
59 | | - $keycloakClient->clientId |
60 | | - )) : [], |
61 | | - ]); |
| 57 | + $organizers = collect() |
| 58 | + ->merge($this->mapOrganizers($this->testSearchService, $this->testCredentialsContext, $testOrganizers, 'Test', $integration, $keycloakClientCache)) |
| 59 | + ->merge($this->mapOrganizers($this->prodSearchService, $this->prodCredentialsContext, $prodOrganizers, 'Live', $integration, $keycloakClientCache)); |
| 60 | + |
| 61 | + // Only add demo user if not already added from the database. |
| 62 | + $testOrgId = (string)config(UiTPASConfig::TEST_ORGANISATION->value); |
| 63 | + if (!$organizers->contains(fn (array $organizer) => $organizer['id'] === $testOrgId)) { |
| 64 | + $organizers = $organizers->merge($this->addTestOrganizer($integration)); |
| 65 | + } |
62 | 66 |
|
63 | 67 | return $organizers; |
64 | 68 | } |
@@ -91,4 +95,74 @@ private function getLabels(?UiTPASPermission $permission): array |
91 | 95 |
|
92 | 96 | return $labels; |
93 | 97 | } |
| 98 | + |
| 99 | + public function addTestOrganizer(Integration $integration): Collection |
| 100 | + { |
| 101 | + $output = collect(); |
| 102 | + $keycloakClient = $this->getClientByEnv($integration, Environment::Testing); |
| 103 | + $orgTestId = (string)config(UiTPASConfig::TEST_ORGANISATION->value); |
| 104 | + $output->push([ |
| 105 | + 'id' => $orgTestId, |
| 106 | + 'name' => ['nl' => 'UiTPAS Organisatie (Regio Gent + Paspartoe)'], |
| 107 | + 'status' => 'Test', |
| 108 | + 'permissions' => $keycloakClient ? $this->getLabels($this->UiTPASApi->fetchPermissions( |
| 109 | + $this->testCredentialsContext, |
| 110 | + new UdbUuid($orgTestId), |
| 111 | + $keycloakClient->clientId |
| 112 | + )) : [], |
| 113 | + ]); |
| 114 | + |
| 115 | + return $output; |
| 116 | + } |
| 117 | + |
| 118 | + private function mapOrganizers( |
| 119 | + SearchService $searchService, |
| 120 | + ClientCredentialsContext $context, |
| 121 | + array $udbOrganizers, |
| 122 | + string $status, |
| 123 | + Integration $integration, |
| 124 | + array $keycloakClientCache |
| 125 | + ): Collection { |
| 126 | + $organizerIds = array_map(fn (UdbOrganizer $item) => $item->organizerId, $udbOrganizers); |
| 127 | + |
| 128 | + // Fetch organizers from UDB and index by organizer ID |
| 129 | + $UiTPASOrganizers = collect($searchService->findOrganizers(...$organizerIds)->getMember()?->getItems() ?? [])->keyBy(function (SapiOrganizer $organizer) { |
| 130 | + $id = explode('/', $organizer->getId() ?? ''); |
| 131 | + return $id[count($id) - 1]; |
| 132 | + }); |
| 133 | + |
| 134 | + $organizers = collect(); |
| 135 | + |
| 136 | + foreach ($udbOrganizers as $udbOrganizer) { |
| 137 | + if (!$udbOrganizer instanceof UdbOrganizer) { |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + $organizer = $UiTPASOrganizers->get($udbOrganizer->organizerId->toString()); |
| 142 | + |
| 143 | + if (!$organizer) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + if ($udbOrganizer->clientId === null) { |
| 148 | + //@todo This if can be removed later if clientId is no longer nullable |
| 149 | + $keycloakClient = $this->getClientByEnv($integration, Environment::Production); |
| 150 | + } else { |
| 151 | + $keycloakClient = $keycloakClientCache[$udbOrganizer->clientId->toString()] ?? null; |
| 152 | + } |
| 153 | + |
| 154 | + $organizers->push([ |
| 155 | + 'id' => $udbOrganizer->organizerId->toString(), |
| 156 | + 'name' => $organizer->getName()?->getValues() ?? [], |
| 157 | + 'status' => $status, |
| 158 | + 'permissions' => $keycloakClient ? $this->getLabels($this->UiTPASApi->fetchPermissions( |
| 159 | + $context, |
| 160 | + $udbOrganizer->organizerId, |
| 161 | + $keycloakClient->clientId |
| 162 | + )) : [], |
| 163 | + ]); |
| 164 | + } |
| 165 | + |
| 166 | + return $organizers; |
| 167 | + } |
94 | 168 | } |
0 commit comments