Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ct/legacy/history changes #515

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<LegacyGetCorrespondenceHistoryResponse>();
var historyData = await historyResponse.Content.ReadFromJsonAsync<List<LegacyGetCorrespondenceHistoryResponse>>();
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]
Expand Down Expand Up @@ -130,15 +130,13 @@ public async Task LegacyGetCorrespondenceHistory_WithCorrespondenceActions_Inclu
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

// Assert
var content = await response.Content.ReadFromJsonAsync<LegacyGetCorrespondenceHistoryResponse>(_serializerOptions);
var content = await response.Content.ReadFromJsonAsync<List<LegacyGetCorrespondenceHistoryResponse>>(_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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -7,23 +8,20 @@
using System.Security.Claims;

namespace Altinn.Correspondence.Application.GetCorrespondenceHistory;
public class LegacyGetCorrespondenceHistoryHandler : IHandler<Guid, LegacyGetCorrespondenceHistoryResponse>
public class LegacyGetCorrespondenceHistoryHandler(
ICorrespondenceRepository correspondenceRepository,
IAltinnNotificationService altinnNotificationService,
IAltinnRegisterService altinnRegisterService,
IAltinnAuthorizationService altinnAuthorizationService,
UserClaimsHelper userClaimsHelper) : IHandler<Guid, List<LegacyGetCorrespondenceHistoryResponse>>
{
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<OneOf<LegacyGetCorrespondenceHistoryResponse, Error>> Process(Guid correspondenceId, ClaimsPrincipal? user, CancellationToken cancellationToken)
public async Task<OneOf<List<LegacyGetCorrespondenceHistoryResponse>, Error>> Process(Guid correspondenceId, ClaimsPrincipal? user, CancellationToken cancellationToken)
{
if (_userClaimsHelper.GetPartyId() is not int partyId)
{
Expand Down Expand Up @@ -52,19 +50,10 @@ public async Task<OneOf<LegacyGetCorrespondenceHistoryResponse, Error>> 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<LegacyCorrespondenceStatus>();
var notificationHistory = new List<LegacyGetCorrespondenceHistoryResponse>();
foreach (var notification in correspondence.Notifications)
{
if (string.IsNullOrEmpty(notification.NotificationOrderId.ToString())) continue;
Expand All @@ -75,45 +64,72 @@ public async Task<OneOf<LegacyGetCorrespondenceHistoryResponse, Error>> 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<LegacyGetCorrespondenceHistoryResponse> 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<CorrespondenceStatus> statusBySender =
[
CorrespondenceStatus.Published,
];
return new LegacyGetCorrespondenceHistoryResponse
CelineTrammi marked this conversation as resolved.
Show resolved Hide resolved
{
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<LegacyGetCorrespondenceHistoryResponse> 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,
StatusText = $"[{(isReminder ? "Reminder" : "Notification")}] {sendStatus.StatusDescription}",
User = new LegacyUser
{
PartyId = partyId,
AuthenticationLevel = authenticationLevel,
Recipient = recipient
},
};
}

private async Task<int?> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
namespace Altinn.Correspondence.Application.GetCorrespondenceHistory;

public class LegacyGetCorrespondenceHistoryResponse
{
public List<LegacyCorrespondenceStatus> History { get; set; } = new List<LegacyCorrespondenceStatus>();

public bool? NeedsConfirm { get; set; }
}
public class LegacyCorrespondenceStatus
{
public string Status { get; set; }
public DateTimeOffset? StatusChanged { get; set; }
Expand All @@ -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; }
}
Loading