From 74bec64c61ff10dbb5b24462cf700537a423bab1 Mon Sep 17 00:00:00 2001 From: Ivar Nesje Date: Mon, 6 Feb 2023 10:16:40 +0100 Subject: [PATCH] Localtest: Add service owner orgs as possible lookups in register api. (#9684) * Localtest: Use CDN orgs in register api. * Get cdn orgs url from config. --- src/Clients/CdnAltinnOrgs/AltinnOrgsClient.cs | 8 +++++-- .../Implementation/OrganizationsWrapper.cs | 24 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Clients/CdnAltinnOrgs/AltinnOrgsClient.cs b/src/Clients/CdnAltinnOrgs/AltinnOrgsClient.cs index 14076320..cfcfb90e 100644 --- a/src/Clients/CdnAltinnOrgs/AltinnOrgsClient.cs +++ b/src/Clients/CdnAltinnOrgs/AltinnOrgsClient.cs @@ -2,6 +2,8 @@ using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; +using Altinn.Platform.Authentication.Configuration; +using Microsoft.Extensions.Options; namespace LocalTest.Clients.CdnAltinnOrgs; @@ -12,15 +14,17 @@ public class AltinnOrgsClient { private static JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web); private readonly HttpClient _client; + private readonly GeneralSettings _authOptions; - public AltinnOrgsClient(HttpClient client) + public AltinnOrgsClient(HttpClient client, IOptions authOptions) { _client = client; + _authOptions = authOptions.Value; } public async Task GetCdnOrgs() { - var orgsJson = await _client.GetByteArrayAsync("https://altinncdn.no/orgs/altinn-orgs.json"); + var orgsJson = await _client.GetByteArrayAsync(_authOptions.GetOrganisationRepositoryLocation); return JsonSerializer.Deserialize(orgsJson, JSON_OPTIONS) ?? throw new JsonException("altinn-orgs respones was \"null\""); } } diff --git a/src/Services/Register/Implementation/OrganizationsWrapper.cs b/src/Services/Register/Implementation/OrganizationsWrapper.cs index 8ea4ff38..4841cafd 100644 --- a/src/Services/Register/Implementation/OrganizationsWrapper.cs +++ b/src/Services/Register/Implementation/OrganizationsWrapper.cs @@ -1,5 +1,6 @@ #nullable enable using Altinn.Platform.Register.Models; +using LocalTest.Clients.CdnAltinnOrgs; using LocalTest.Services.Register.Interface; using LocalTest.Services.TestData; @@ -11,17 +12,36 @@ namespace LocalTest.Services.Register.Implementation public class OrganizationsWrapper : IOrganizations { private readonly TestDataService _testDataService; + private readonly AltinnOrgsClient _orgsClient; - public OrganizationsWrapper(TestDataService testDataService) + public OrganizationsWrapper(TestDataService testDataService, AltinnOrgsClient orgsClient) { _testDataService = testDataService; + _orgsClient = orgsClient; } /// public async Task GetOrganization(string orgNr) { var data = await _testDataService.GetTestData(); - return data.Register.Org.TryGetValue(orgNr, out var value) ? value : null; + + if (data.Register.Org.TryGetValue(orgNr, out var value)) + { + return value; + } + + // Make lookup work for all the orgs that has apps in altinn. + var cdnOrgs = await _orgsClient.GetCdnOrgs(); + var cdnOrg = cdnOrgs?.Orgs?.Values.FirstOrDefault(org=>org.Orgnr == orgNr); + if(cdnOrg is not null) + { + return new Organization + { + Name = cdnOrg.Name?.Nb, + OrgNumber = cdnOrg.Orgnr, + }; + } + return null; } } }