diff --git a/Test/Altinn.Correspondence.Tests/TestingController/Legacy/LegacyRetrievalTests.cs b/Test/Altinn.Correspondence.Tests/TestingController/Legacy/LegacyRetrievalTests.cs index 347528c9..3330ddea 100644 --- a/Test/Altinn.Correspondence.Tests/TestingController/Legacy/LegacyRetrievalTests.cs +++ b/Test/Altinn.Correspondence.Tests/TestingController/Legacy/LegacyRetrievalTests.cs @@ -81,9 +81,9 @@ public async Task LegacyGetCorrespondenceOverview_ShouldAddFetchedToStatusHistor // Assert var historyResponse = await _legacyClient.GetAsync($"correspondence/api/v1/legacy/correspondence/{correspondence.CorrespondenceId}/history"); Assert.Equal(HttpStatusCode.OK, historyResponse.StatusCode); - var historyData = await historyResponse.Content.ReadFromJsonAsync(); + var historyData = await historyResponse.Content.ReadFromJsonAsync>(); Assert.NotNull(historyData); - Assert.Contains(historyData.History, status => status.Status.Contains(CorrespondenceStatus.Fetched.ToString())); + Assert.Contains(historyData, status => status.Status.Contains(CorrespondenceStatus.Fetched.ToString())); } [Fact] @@ -130,15 +130,13 @@ public async Task LegacyGetCorrespondenceHistory_WithCorrespondenceActions_Inclu Assert.Equal(HttpStatusCode.OK, response.StatusCode); // Assert - var content = await response.Content.ReadFromJsonAsync(_serializerOptions); + var content = await response.Content.ReadFromJsonAsync>(_serializerOptions); Assert.NotNull(content); - Assert.Equal(content.NeedsConfirm, payload.Correspondence.IsConfirmationNeeded); - Assert.All(content.History, status => Assert.True(status.User.AuthenticationLevel > 0)); - Assert.Contains(content.History, status => status.User.PartyId == _digdirPartyId); - Assert.Contains(content.History, status => status.Status.Contains(CorrespondenceStatus.Published.ToString())); - Assert.Contains(content.History, status => status.Status.Contains(CorrespondenceStatus.Fetched.ToString())); - Assert.Contains(content.History, status => status.Status.Contains(CorrespondenceStatus.Confirmed.ToString())); - Assert.Contains(content.History, status => status.Status.Contains(CorrespondenceStatus.Archived.ToString())); + Assert.Contains(content, status => status.User.PartyId == _digdirPartyId); + Assert.Contains(content, status => status.Status.Contains(CorrespondenceStatus.Published.ToString())); + Assert.Contains(content, status => status.Status.Contains(CorrespondenceStatus.Fetched.ToString())); + Assert.Contains(content, status => status.Status.Contains(CorrespondenceStatus.Confirmed.ToString())); + Assert.Contains(content, status => status.Status.Contains(CorrespondenceStatus.Archived.ToString())); } [Fact] diff --git a/src/Altinn.Correspondence.Application/GetCorrespondenceHistory/LegacyGetCorrespondenceHistoryHandler.cs b/src/Altinn.Correspondence.Application/GetCorrespondenceHistory/LegacyGetCorrespondenceHistoryHandler.cs index f382c2e9..ef9a3129 100644 --- a/src/Altinn.Correspondence.Application/GetCorrespondenceHistory/LegacyGetCorrespondenceHistoryHandler.cs +++ b/src/Altinn.Correspondence.Application/GetCorrespondenceHistory/LegacyGetCorrespondenceHistoryHandler.cs @@ -1,4 +1,5 @@ using Altinn.Correspondence.Application.Helpers; +using Altinn.Correspondence.Core.Models.Entities; using Altinn.Correspondence.Core.Models.Enums; using Altinn.Correspondence.Core.Models.Notifications; using Altinn.Correspondence.Core.Repositories; @@ -7,23 +8,20 @@ using System.Security.Claims; namespace Altinn.Correspondence.Application.GetCorrespondenceHistory; -public class LegacyGetCorrespondenceHistoryHandler : IHandler +public class LegacyGetCorrespondenceHistoryHandler( + ICorrespondenceRepository correspondenceRepository, + IAltinnNotificationService altinnNotificationService, + IAltinnRegisterService altinnRegisterService, + IAltinnAuthorizationService altinnAuthorizationService, + UserClaimsHelper userClaimsHelper) : IHandler> { - private readonly ICorrespondenceRepository _correspondenceRepository; - private readonly IAltinnNotificationService _altinnNotificationService; - private readonly IAltinnRegisterService _altinnRegisterService; - private readonly IAltinnAuthorizationService _altinnAuthorizationService; - private readonly UserClaimsHelper _userClaimsHelper; + private readonly ICorrespondenceRepository _correspondenceRepository = correspondenceRepository; + private readonly IAltinnNotificationService _altinnNotificationService = altinnNotificationService; + private readonly IAltinnRegisterService _altinnRegisterService = altinnRegisterService; + private readonly IAltinnAuthorizationService _altinnAuthorizationService = altinnAuthorizationService; + private readonly UserClaimsHelper _userClaimsHelper = userClaimsHelper; - public LegacyGetCorrespondenceHistoryHandler(ICorrespondenceRepository correspondenceRepository, IAltinnNotificationService altinnNotificationService, IAltinnRegisterService altinnRegisterService, IAltinnAuthorizationService altinnAuthorizationService, UserClaimsHelper userClaimsHelper) - { - _correspondenceRepository = correspondenceRepository; - _altinnNotificationService = altinnNotificationService; - _altinnRegisterService = altinnRegisterService; - _altinnAuthorizationService = altinnAuthorizationService; - _userClaimsHelper = userClaimsHelper; - } - public async Task> Process(Guid correspondenceId, ClaimsPrincipal? user, CancellationToken cancellationToken) + public async Task, Error>> Process(Guid correspondenceId, ClaimsPrincipal? user, CancellationToken cancellationToken) { if (_userClaimsHelper.GetPartyId() is not int partyId) { @@ -52,19 +50,10 @@ public async Task> Process( var correspondenceHistory = correspondence.Statuses .Where(s => s.Status.IsAvailableForRecipient()) - .Select(s => new LegacyCorrespondenceStatus - { - Status = s.Status.ToString(), - StatusChanged = s.StatusChanged, - StatusText = $"[Correspondence] {s.StatusText}", - User = new LegacyUser - { - PartyId = recipientParty.PartyId, - AuthenticationLevel = (int)minimumAuthLevel - }, - }).ToList(); + .Select(s => GetCorrespondenceStatus(s, recipientParty, senderParty)) + .ToList(); - var notificationHistory = new List(); + var notificationHistory = new List(); foreach (var notification in correspondence.Notifications) { if (string.IsNullOrEmpty(notification.NotificationOrderId.ToString())) continue; @@ -75,35 +64,48 @@ public async Task> Process( if (notificationDetails.NotificationsStatusDetails.Sms is not null) { - notificationHistory.Add(GetNotificationStatus( + notificationHistory.Add(await GetNotificationStatus( notificationDetails.NotificationsStatusDetails.Sms.SendStatus, notificationDetails.NotificationsStatusDetails.Sms.Recipient, notification.IsReminder, - senderParty.PartyId, - (int)minimumAuthLevel)); + cancellationToken)); } if (notificationDetails.NotificationsStatusDetails.Email is not null) { - notificationHistory.Add(GetNotificationStatus( + notificationHistory.Add(await GetNotificationStatus( notificationDetails.NotificationsStatusDetails.Email.SendStatus, notificationDetails.NotificationsStatusDetails.Email.Recipient, notification.IsReminder, - senderParty.PartyId, - (int)minimumAuthLevel)); + cancellationToken)); } } + List joinedList = [.. correspondenceHistory.Concat(notificationHistory).OrderByDescending(s => s.StatusChanged)]; + + return joinedList; + } - var legacyHistory = new LegacyGetCorrespondenceHistoryResponse + private static LegacyGetCorrespondenceHistoryResponse GetCorrespondenceStatus(CorrespondenceStatusEntity s, Party recipientParty, Party senderParty) + { + List statusBySender = + [ + CorrespondenceStatus.Published, + ]; + return new LegacyGetCorrespondenceHistoryResponse { - History = [.. correspondenceHistory.Concat(notificationHistory).OrderByDescending(s => s.StatusChanged)], - NeedsConfirm = correspondence.IsConfirmationNeeded, + Status = s.Status.ToString(), + StatusChanged = s.StatusChanged, + StatusText = $"[Correspondence] {s.StatusText}", + User = new LegacyUser + { + PartyId = statusBySender.Contains(s.Status) ? senderParty.PartyId : recipientParty.PartyId + } }; - return legacyHistory; } - private static LegacyCorrespondenceStatus GetNotificationStatus(StatusExt sendStatus, Recipient recipient, bool isReminder, int partyId, int authenticationLevel) + private async Task GetNotificationStatus(StatusExt sendStatus, Recipient recipient, bool isReminder, CancellationToken cancellationToken) { - return new LegacyCorrespondenceStatus + int? partyId = await GetPartyIdForNotfication(recipient, cancellationToken); + return new LegacyGetCorrespondenceHistoryResponse { Status = sendStatus.Status, StatusChanged = sendStatus.LastUpdate, @@ -111,9 +113,23 @@ private static LegacyCorrespondenceStatus GetNotificationStatus(StatusExt sendSt User = new LegacyUser { PartyId = partyId, - AuthenticationLevel = authenticationLevel, Recipient = recipient }, }; } + + private async Task GetPartyIdForNotfication(Recipient recipient, CancellationToken cancellationToken) + { + if (recipient.NationalIdentityNumber is not null) + { + var p = await _altinnRegisterService.LookUpPartyById(recipient.NationalIdentityNumber, cancellationToken); + return p?.PartyId; + } + else if (recipient.OrganizationNumber is not null) + { + var party = await _altinnRegisterService.LookUpPartyById(recipient.OrganizationNumber, cancellationToken); + return party?.PartyId; + } + return null; + } } diff --git a/src/Altinn.Correspondence.Application/GetCorrespondenceHistory/LegacyGetCorrespondenceHistoryResponse.cs b/src/Altinn.Correspondence.Application/GetCorrespondenceHistory/LegacyGetCorrespondenceHistoryResponse.cs index 846c7cd0..4ff6ef4c 100644 --- a/src/Altinn.Correspondence.Application/GetCorrespondenceHistory/LegacyGetCorrespondenceHistoryResponse.cs +++ b/src/Altinn.Correspondence.Application/GetCorrespondenceHistory/LegacyGetCorrespondenceHistoryResponse.cs @@ -3,12 +3,6 @@ namespace Altinn.Correspondence.Application.GetCorrespondenceHistory; public class LegacyGetCorrespondenceHistoryResponse -{ - public List History { get; set; } = new List(); - - public bool? NeedsConfirm { get; set; } -} -public class LegacyCorrespondenceStatus { public string Status { get; set; } public DateTimeOffset? StatusChanged { get; set; } @@ -17,7 +11,6 @@ public class LegacyCorrespondenceStatus } public class LegacyUser { - public int PartyId { get; set; } - public int AuthenticationLevel { get; set; } + public int? PartyId { get; set; } public Recipient Recipient { get; set; } } \ No newline at end of file