Skip to content

Commit

Permalink
add markasread, following same logic as A3, but without markasunread (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CelineTrammi authored Nov 11, 2024
1 parent cd653ae commit 0a76bbc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
8 changes: 3 additions & 5 deletions Test/Altinn.Correspondence.Tests/LegacyControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ public async Task UpdateCorrespondenceStatus_CorrespondenceNotExists_Return404()
var archiveResponse = await _legacyClient.PostAsync($"correspondence/api/v1/legacy/correspondence/{randomCorrespondenceId}/archive", null);
Assert.Equal(HttpStatusCode.NotFound, archiveResponse.StatusCode);

// TODO: Missing markasread implementation
// var readResponse = await _client.PostAsync($"correspondence/api/v1/legacy/correspondence/{randomCorrespondenceId}/markasread", null);
// Assert.Equal(HttpStatusCode.NotFound, readResponse.StatusCode);
// Act and Assert
var readResponse = await _legacyClient.PostAsync($"correspondence/api/v1/legacy/correspondence/{randomCorrespondenceId}/markasread", null);
Assert.Equal(HttpStatusCode.NotFound, readResponse.StatusCode);
}

[Fact]
Expand Down Expand Up @@ -254,7 +254,6 @@ public async Task UpdateCorrespondenceStatus_CorrespondencePublished_ReturnOk()
Assert.Equal(HttpStatusCode.OK, archiveResponse.StatusCode);
}

/* TODO: Implement markasread
[Fact]
public async Task UpdateCorrespondenceStatus_MarkAsRead_WithoutFetched_ReturnsBadRequest()
{
Expand All @@ -271,7 +270,6 @@ public async Task UpdateCorrespondenceStatus_MarkAsRead_WithoutFetched_ReturnsBa
// Assert
Assert.Equal(HttpStatusCode.BadRequest, readResponse.StatusCode);
}
*/

[Fact]
public async Task UpdateCorrespondenceStatus_ToConfirmed_WithoutFetched_ReturnsBadRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ public async Task<ActionResult> DownloadCorrespondenceAttachment(
);
}

/// <summary>
/// Mark Correspondence found by ID as read
/// </summary>
/// <remarks>
/// Meant for Receivers
/// </remarks>
/// <returns>StatusId</returns>
[HttpPost]
[Route("{correspondenceId}/markasread")]
public async Task<ActionResult> MarkAsRead(
Guid correspondenceId,
[FromServices] LegacyUpdateCorrespondenceStatusHandler handler,
CancellationToken cancellationToken)
{
_logger.LogInformation("Marking Correspondence as read for {correspondenceId}", correspondenceId.ToString());

var commandResult = await handler.Process(new UpdateCorrespondenceStatusRequest
{
CorrespondenceId = correspondenceId,
Status = CorrespondenceStatus.Read
}, cancellationToken);

return commandResult.Match(
data => Ok(data),
Problem
);
}

/// Mark Correspondence found by ID as confirmed
/// </summary>
/// <remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,11 @@ public async Task<OneOf<Guid, Error>> Process(UpdateCorrespondenceStatusRequest
{
return Errors.LegacyNoAccessToCorrespondence;
}
var currentStatus = correspondence.GetLatestStatus();
if (currentStatus is null)
var currentStatusError = _updateCorrespondenceStatusHelper.ValidateCurrentStatus(correspondence);
if (currentStatusError is not null)
{
return Errors.LatestStatusIsNull;
return currentStatusError;
}
if (!currentStatus.Status.IsAvailableForRecipient())
{
return Errors.CorrespondenceNotFound;
}
// TODO: Implement logic for markasread and markasunread
var updateError = _updateCorrespondenceStatusHelper.ValidateUpdateRequest(request, correspondence);
if (updateError is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,20 @@ public async Task<OneOf<Guid, Error>> Process(UpdateCorrespondenceStatusRequest
{
return Errors.CorrespondenceNotFound;
}
var currentStatus = correspondence.GetLatestStatus();
if (currentStatus is null)
var currentStatusError = _updateCorrespondenceStatusHelper.ValidateCurrentStatus(correspondence);
if (currentStatusError is not null)
{
return Errors.LatestStatusIsNull;
return currentStatusError;
}
if (!currentStatus.Status.IsAvailableForRecipient())
{
return Errors.CorrespondenceNotFound;
}
if (currentStatus!.Status.IsPurged())
var updateError = _updateCorrespondenceStatusHelper.ValidateUpdateRequest(request, correspondence);
if (updateError is not null)
{
return Errors.CorrespondencePurged;
return updateError;
}
if (request.Status == CorrespondenceStatus.Read && correspondence.MarkedUnread == true)
{
await _correspondenceRepository.UpdateMarkedUnread(request.CorrespondenceId, false, cancellationToken);
}
var updateError = _updateCorrespondenceStatusHelper.ValidateUpdateRequest(request, correspondence);
if (updateError is not null)
{
return updateError;
}

await _correspondenceStatusRepository.AddCorrespondenceStatus(new CorrespondenceStatusEntity
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@
namespace Altinn.Correspondence.Application.UpdateCorrespondenceStatus;
public class UpdateCorrespondenceStatusHelper
{

/// <summary>
/// Validates if the current status of the correspondence allows for status updates.
/// </summary>
/// <param name="correspondence">The correspondence entity to validate</param>
/// <returns></returns>
public Error? ValidateCurrentStatus(CorrespondenceEntity correspondence)
{
var currentStatus = correspondence.GetLatestStatus();
if (currentStatus is null)
{
return Errors.LatestStatusIsNull;
}
if (!currentStatus.Status.IsAvailableForRecipient())
{
return Errors.CorrespondenceNotFound;
}
if (currentStatus!.Status.IsPurged())
{
return Errors.CorrespondencePurged;
}
return null;
}
/// <summary>
/// Validates if the requested status update is allowed based on the current correspondence state.
/// </summary>
Expand Down

0 comments on commit 0a76bbc

Please sign in to comment.