diff --git a/src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs b/src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs index 96adb19e9..0a5253b5b 100644 --- a/src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs +++ b/src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs @@ -81,9 +81,8 @@ public async Task GetAuthorizedParties(IPartyIdentifier CancellationToken cancellationToken = default) { var authorizedPartiesRequest = new AuthorizedPartiesRequest(authenticatedParty); - // var authorizedParties = await _partiesCache.GetOrSetAsync(authorizedPartiesRequest.GenerateCacheKey(), async token - // => await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, token), token: cancellationToken); - var authorizedParties = await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, cancellationToken); + var authorizedParties = await _partiesCache.GetOrSetAsync(authorizedPartiesRequest.GenerateCacheKey(), async token + => await PerformAuthorizedPartiesRequest(authorizedPartiesRequest, token), token: cancellationToken); return flatten ? GetFlattenedAuthorizedParties(authorizedParties) : authorizedParties; } diff --git a/src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/NameRegistry/PartyNameRegistryClient.cs b/src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/NameRegistry/PartyNameRegistryClient.cs index 7646f30f8..0f4f70454 100644 --- a/src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/NameRegistry/PartyNameRegistryClient.cs +++ b/src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/NameRegistry/PartyNameRegistryClient.cs @@ -4,6 +4,7 @@ using Digdir.Domain.Dialogporten.Application.Externals; using Digdir.Domain.Dialogporten.Domain.Parties; using Digdir.Domain.Dialogporten.Domain.Parties.Abstractions; +using Microsoft.Extensions.Logging; using ZiggyCreatures.Caching.Fusion; namespace Digdir.Domain.Dialogporten.Infrastructure.Altinn.NameRegistry; @@ -12,6 +13,7 @@ internal sealed class PartyNameRegistryClient : IPartyNameRegistry { private readonly IFusionCache _cache; private readonly HttpClient _client; + private readonly ILogger _logger; private static readonly JsonSerializerOptions SerializerOptions = new() { @@ -19,17 +21,28 @@ internal sealed class PartyNameRegistryClient : IPartyNameRegistry DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }; - public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProvider) + public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProvider, ILogger logger) { _client = client ?? throw new ArgumentNullException(nameof(client)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _cache = cacheProvider.GetCache(nameof(NameRegistry)) ?? throw new ArgumentNullException(nameof(cacheProvider)); } public async Task GetName(string externalIdWithPrefix, CancellationToken cancellationToken) { - return await _cache.GetOrSetAsync( + return await _cache.GetOrSetAsync( $"Name_{externalIdWithPrefix}", - ct => GetNameFromRegister(externalIdWithPrefix, ct), + async (ctx, ct) => + { + var name = await GetNameFromRegister(externalIdWithPrefix, ct); + if (name is null) + { + // Short negative cache + ctx.Options.Duration = TimeSpan.FromSeconds(10); + } + + return name; + }, token: cancellationToken); } @@ -48,7 +61,14 @@ public PartyNameRegistryClient(HttpClient client, IFusionCacheProvider cacheProv serializerOptions: SerializerOptions, cancellationToken: cancellationToken); - return nameLookupResult.PartyNames.FirstOrDefault()?.Name; + var name = nameLookupResult.PartyNames.FirstOrDefault()?.Name; + if (name is null) + { + // This is PII, but this is an error condition (probably due to missing Altinn profile) + _logger.LogError("Failed to get name from party name registry for external id {ExternalId}", externalIdWithPrefix); + } + + return name; } private static bool TryParse(string externalIdWithPrefix, [NotNullWhen(true)] out NameLookup? nameLookup)