From 941f5f6013f5c501659fd6e8c65018ecc5a051b6 Mon Sep 17 00:00:00 2001 From: CelineTrammi <61122289+CelineTrammi@users.noreply.github.com> Date: Fri, 30 Aug 2024 08:20:25 +0200 Subject: [PATCH] fix: Published status was overwritten by Fetch status using get details and overview (#239) * Add status to first DB and then return updated correspondence * set latestStatus as new Correspondence entity instead of directly changing members * use DateTime instead of DateTimeOffset * show status history in order of occurance * add assert for statushistory in GetCorrespondenceDetails --- .../CorrespondenceControllerTests.cs | 4 ++++ .../GetCorrespondenceDetailsHandler.cs | 15 +++++++++------ .../GetCorrespondenceOverviewHandler.cs | 9 ++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Test/Altinn.Correspondence.Tests/CorrespondenceControllerTests.cs b/Test/Altinn.Correspondence.Tests/CorrespondenceControllerTests.cs index 3ab72759..c73de4d8 100644 --- a/Test/Altinn.Correspondence.Tests/CorrespondenceControllerTests.cs +++ b/Test/Altinn.Correspondence.Tests/CorrespondenceControllerTests.cs @@ -336,6 +336,8 @@ public async Task GetCorrespondenceDetails_AsReceiver_UpdatesStatusToFetched() var getCorrespondenceDetailsResponse = await _client.GetAsync($"correspondence/api/v1/correspondence/{correspondence?.CorrespondenceIds.FirstOrDefault()}/details"); Assert.True(getCorrespondenceDetailsResponse.IsSuccessStatusCode, await getCorrespondenceDetailsResponse.Content.ReadAsStringAsync()); var response = await getCorrespondenceDetailsResponse.Content.ReadFromJsonAsync(_responseSerializerOptions); + Assert.Contains(response.StatusHistory, item => item.Status == CorrespondenceStatusExt.Published); + Assert.Contains(response.StatusHistory, item => item.Status == CorrespondenceStatusExt.Fetched); Assert.Equal(response.Status, CorrespondenceStatusExt.Fetched); } @@ -348,6 +350,8 @@ public async Task GetCorrespondenceDetails_AsSender_KeepsStatusAsPublished() var getCorrespondenceDetailsResponse = await _client.GetAsync($"correspondence/api/v1/correspondence/{correspondence?.CorrespondenceIds.FirstOrDefault()}/details"); Assert.True(getCorrespondenceDetailsResponse.IsSuccessStatusCode, await getCorrespondenceDetailsResponse.Content.ReadAsStringAsync()); var response = await getCorrespondenceDetailsResponse.Content.ReadFromJsonAsync(_responseSerializerOptions); + Assert.Contains(response.StatusHistory, item => item.Status == CorrespondenceStatusExt.Published); + Assert.DoesNotContain(response.StatusHistory, item => item.Status == CorrespondenceStatusExt.Fetched); Assert.Equal(response.Status, CorrespondenceStatusExt.Published); } [Fact] diff --git a/src/Altinn.Correspondence.Application/GetCorrespondenceDetails/GetCorrespondenceDetailsHandler.cs b/src/Altinn.Correspondence.Application/GetCorrespondenceDetails/GetCorrespondenceDetailsHandler.cs index 280017d1..71a9a7d0 100644 --- a/src/Altinn.Correspondence.Application/GetCorrespondenceDetails/GetCorrespondenceDetailsHandler.cs +++ b/src/Altinn.Correspondence.Application/GetCorrespondenceDetails/GetCorrespondenceDetailsHandler.cs @@ -33,7 +33,7 @@ public async Task> Process(Guid c { return Errors.NoAccessToResource; } - var latestStatus = correspondence.Statuses.OrderByDescending(s => s.StatusChanged).FirstOrDefault(); + var latestStatus = correspondence.Statuses?.OrderByDescending(s => s.StatusChanged).FirstOrDefault(); if (latestStatus == null) { return Errors.CorrespondenceNotFound; @@ -44,10 +44,13 @@ public async Task> Process(Guid c if (isRecipient && latestStatus.Status == CorrespondenceStatus.Published) { - latestStatus.Status = CorrespondenceStatus.Fetched; - latestStatus.StatusText = CorrespondenceStatus.Fetched.ToString(); - latestStatus.StatusChanged = DateTimeOffset.UtcNow; - + latestStatus = new CorrespondenceStatusEntity{ + CorrespondenceId = correspondence.Id, + Status = CorrespondenceStatus.Fetched, + StatusText = CorrespondenceStatus.Fetched.ToString(), + StatusChanged = DateTime.Now + }; + await _correspondenceStatusRepository.AddCorrespondenceStatus(new CorrespondenceStatusEntity { CorrespondenceId = correspondence.Id, @@ -71,7 +74,7 @@ await _correspondenceStatusRepository.AddCorrespondenceStatus(new Correspondence Notifications = correspondence.Notifications == null ? new List() : correspondence.Notifications, VisibleFrom = correspondence.VisibleFrom, IsReservable = correspondence.IsReservable == null || correspondence.IsReservable.Value, - StatusHistory = correspondence.Statuses, + StatusHistory = correspondence.Statuses?.OrderBy(s => s.StatusChanged).ToList() ?? [], ResourceId = correspondence.ResourceId }; return response; diff --git a/src/Altinn.Correspondence.Application/GetCorrespondenceOverview/GetCorrespondenceOverviewHandler.cs b/src/Altinn.Correspondence.Application/GetCorrespondenceOverview/GetCorrespondenceOverviewHandler.cs index 7213fca3..5ae6e956 100644 --- a/src/Altinn.Correspondence.Application/GetCorrespondenceOverview/GetCorrespondenceOverviewHandler.cs +++ b/src/Altinn.Correspondence.Application/GetCorrespondenceOverview/GetCorrespondenceOverviewHandler.cs @@ -44,9 +44,12 @@ public async Task> Process(Guid if (isRecipient && latestStatus.Status == CorrespondenceStatus.Published) { - latestStatus.Status = CorrespondenceStatus.Fetched; - latestStatus.StatusText = CorrespondenceStatus.Fetched.ToString(); - latestStatus.StatusChanged = DateTimeOffset.UtcNow; + latestStatus = new CorrespondenceStatusEntity{ + CorrespondenceId = correspondence.Id, + Status = CorrespondenceStatus.Fetched, + StatusText = CorrespondenceStatus.Fetched.ToString(), + StatusChanged = DateTime.Now + }; await _correspondenceStatusRepository.AddCorrespondenceStatus(new CorrespondenceStatusEntity {