Skip to content

Commit

Permalink
Legacy api performance tuning (#598)
Browse files Browse the repository at this point in the history
* Changed how to accesss multiple recipients and separate logic for 1 and multiple recipients

* Mapped search keys

---------

Co-authored-by: Henning Normann <[email protected]>
  • Loading branch information
HenningNormann and HenningNormann authored Jan 16, 2025
1 parent 117b302 commit 4e58804
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using Microsoft.Extensions.Logging;
using System.Security.Claims;
using Altinn.Correspondence.Common.Constants;

namespace Altinn.Correspondence.Application.GetCorrespondences;

Expand Down Expand Up @@ -54,15 +55,15 @@ public async Task<OneOf<LegacyGetCorrespondencesResponse, Error>> Process(Legacy
return AuthorizationErrors.LegacyNotAccessToOwner(instanceOwnerPartyId);
}
if (mappedInstanceOwner.OrgNumber != null)
recipients.Add(mappedInstanceOwner.OrgNumber);
recipients.Add(GetPrefixedForOrg(mappedInstanceOwner.OrgNumber));
else if (mappedInstanceOwner.SSN != null)
recipients.Add(mappedInstanceOwner.SSN);
recipients.Add(GetPrefixedForPerson(mappedInstanceOwner.SSN));
}
}
else
{
if (!string.IsNullOrEmpty(userParty.SSN)) recipients.Add(userParty.SSN);
if (!string.IsNullOrEmpty(userParty.OrgNumber)) recipients.Add(userParty.OrgNumber);
if (!string.IsNullOrEmpty(userParty.SSN)) recipients.Add(GetPrefixedForPerson(userParty.SSN));
if (!string.IsNullOrEmpty(userParty.OrgNumber)) recipients.Add(GetPrefixedForOrg(userParty.OrgNumber));
}
List<string> resourcesToSearch = new List<string>();

Expand Down Expand Up @@ -161,4 +162,14 @@ public async Task<OneOf<LegacyGetCorrespondencesResponse, Error>> Process(Legacy
};
return response;
}

private static string GetPrefixedForPerson(string ssn)
{
return $"{UrnConstants.PersonIdAttribute}:{ssn}";
}

private static string GetPrefixedForOrg(string orgnr)
{
return $"{UrnConstants.OrganizationNumberAttribute}:{orgnr}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,19 @@ public async Task UpdatePublished(Guid correspondenceId, DateTimeOffset publishe

public async Task<(List<CorrespondenceEntity>, int)> GetCorrespondencesForParties(int offset, int limit, DateTimeOffset? from, DateTimeOffset? to, CorrespondenceStatus? status, List<string> recipientIds, List<string> resourceIds, bool includeActive, bool includeArchived, bool includePurged, string searchString, CancellationToken cancellationToken)
{
var correspondences = _context.Correspondences
.Where(c => from == null || c.RequestedPublishTime > from) // From date filter
.Where(c => to == null || c.RequestedPublishTime < to) // To date filter
.Where(c => recipientIds.Any(recipient => c.Recipient.Contains(recipient))) // Filter by recipients
var correspondences = recipientIds.Count == 1
? _context.Correspondences.Where(c => c.Recipient == recipientIds[0]) // Filter by single recipient
: _context.Correspondences.Where(c => recipientIds.Contains(c.Recipient)); // Filter multiple recipients

correspondences = correspondences
.Where(c => from == null || c.RequestedPublishTime > from) // From date filter
.Where(c => to == null || c.RequestedPublishTime < to) // To date filter
.Where(c => resourceIds.Count == 0 || resourceIds.Contains(c.ResourceId)) // Filter by resources
.IncludeByStatuses(includeActive, includeArchived, includePurged, status) // Filter by statuses
.Where(c => string.IsNullOrEmpty(searchString) || (c.Content != null && c.Content.MessageTitle.Contains(searchString))) // Filter by messageTitle containing searchstring
.Include(c => c.Statuses)
.Include(c => c.Statuses)
.Include(c => c.Content)
.OrderByDescending(c => c.RequestedPublishTime); // Sort by RequestedPublishTime

.OrderByDescending(c => c.RequestedPublishTime); // Sort by RequestedPublishTime

var totalItems = await correspondences.CountAsync(cancellationToken);
var result = await correspondences.Skip(offset).Take(limit).ToListAsync(cancellationToken);
Expand Down

0 comments on commit 4e58804

Please sign in to comment.