From 8031041fd7977a12d8d764eb142253eff571ee04 Mon Sep 17 00:00:00 2001 From: Jesper Justesen <1972142+FirestarJes@users.noreply.github.com> Date: Wed, 18 Dec 2024 09:21:03 +0100 Subject: [PATCH] feat: Update ActorConsolidationAuditLogRepository to have an object in new and old value (#1050) --- ...eAt to ActorConsolidationAuditLogEntry.sql | 2 ++ ...to ActorConsolidationAuditLogEntry.sql.sql | 2 ++ .../Model/ActorConsolidationActorAndDate.cs | 23 +++++++++++++++++++ .../ActorConsolidationAuditLogEntryEntity.cs | 1 + .../ActorConsolidationAuditLogRepository.cs | 22 ++++++++++++++---- ...ridAreaAuditLogsHandlerIntegrationTests.cs | 20 ++++++++++++---- ...torConsolidationAuditLogRepositoryTests.cs | 17 ++++++++++---- 7 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 source/marketparticipant/Energinet.DataHub.MarketParticipant.ApplyDBMigrationsApp/Scripts/ALL/Model/202412171448 Add ConsolidateAt to ActorConsolidationAuditLogEntry.sql create mode 100644 source/marketparticipant/Energinet.DataHub.MarketParticipant.ApplyDBMigrationsApp/Scripts/LocalDB/202412171448 Add ConsolidateAt to ActorConsolidationAuditLogEntry.sql.sql create mode 100644 source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Model/ActorConsolidationActorAndDate.cs diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.ApplyDBMigrationsApp/Scripts/ALL/Model/202412171448 Add ConsolidateAt to ActorConsolidationAuditLogEntry.sql b/source/marketparticipant/Energinet.DataHub.MarketParticipant.ApplyDBMigrationsApp/Scripts/ALL/Model/202412171448 Add ConsolidateAt to ActorConsolidationAuditLogEntry.sql new file mode 100644 index 000000000..30de26e7b --- /dev/null +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.ApplyDBMigrationsApp/Scripts/ALL/Model/202412171448 Add ConsolidateAt to ActorConsolidationAuditLogEntry.sql @@ -0,0 +1,2 @@ +ALTER TABLE [dbo].[ActorConsolidationAuditLogEntry] + ADD [ConsolidateAt] [datetimeoffset] NOT NULL DEFAULT (GETUTCDATE()) diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.ApplyDBMigrationsApp/Scripts/LocalDB/202412171448 Add ConsolidateAt to ActorConsolidationAuditLogEntry.sql.sql b/source/marketparticipant/Energinet.DataHub.MarketParticipant.ApplyDBMigrationsApp/Scripts/LocalDB/202412171448 Add ConsolidateAt to ActorConsolidationAuditLogEntry.sql.sql new file mode 100644 index 000000000..30de26e7b --- /dev/null +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.ApplyDBMigrationsApp/Scripts/LocalDB/202412171448 Add ConsolidateAt to ActorConsolidationAuditLogEntry.sql.sql @@ -0,0 +1,2 @@ +ALTER TABLE [dbo].[ActorConsolidationAuditLogEntry] + ADD [ConsolidateAt] [datetimeoffset] NOT NULL DEFAULT (GETUTCDATE()) diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Model/ActorConsolidationActorAndDate.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Model/ActorConsolidationActorAndDate.cs new file mode 100644 index 000000000..01ff31ae2 --- /dev/null +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Model/ActorConsolidationActorAndDate.cs @@ -0,0 +1,23 @@ +// Copyright 2020 Energinet DataHub A/S +// +// Licensed under the Apache License, Version 2.0 (the "License2"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; + +namespace Energinet.DataHub.MarketParticipant.Infrastructure.Model; + +public sealed class ActorConsolidationActorAndDate +{ + public Guid ActorId { get; set; } + public DateTimeOffset ConsolidateAt { get; set; } +} diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Model/ActorConsolidationAuditLogEntryEntity.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Model/ActorConsolidationAuditLogEntryEntity.cs index aeeecdd2d..2a31e43e4 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Model/ActorConsolidationAuditLogEntryEntity.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Model/ActorConsolidationAuditLogEntryEntity.cs @@ -25,4 +25,5 @@ public sealed class ActorConsolidationAuditLogEntryEntity public string NewValue { get; set; } = null!; public string OldValue { get; set; } = null!; public DateTimeOffset Timestamp { get; set; } + public DateTimeOffset ConsolidateAt { get; set; } } diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Repositories/ActorConsolidationAuditLogRepository.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Repositories/ActorConsolidationAuditLogRepository.cs index a5e08160b..e3f58d873 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Repositories/ActorConsolidationAuditLogRepository.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Repositories/ActorConsolidationAuditLogRepository.cs @@ -15,10 +15,12 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using Energinet.DataHub.MarketParticipant.Domain.Model; using Energinet.DataHub.MarketParticipant.Domain.Model.Users; using Energinet.DataHub.MarketParticipant.Domain.Repositories; +using Energinet.DataHub.MarketParticipant.Infrastructure.Model; using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Model; using Microsoft.EntityFrameworkCore; using NodaTime.Extensions; @@ -51,8 +53,8 @@ from entity in entities entity.Timestamp.ToInstant(), new AuditIdentity(entity.ChangedByUserId), false, - entity.NewValue, - entity.OldValue); + GetSerializedActorConsolidationActorAndDate(entity.ConsolidateAt, entity.NewValue), + GetSerializedActorConsolidationActorAndDate(entity.ConsolidateAt, entity.OldValue)); static GridAreaAuditedChange Map(ActorConsolidationAuditLogField field) { @@ -82,8 +84,8 @@ from entity in entities entity.Timestamp.ToInstant(), new AuditIdentity(entity.ChangedByUserId), false, - entity.NewValue, - entity.OldValue); + GetSerializedActorConsolidationActorAndDate(entity.ConsolidateAt, entity.NewValue), + GetSerializedActorConsolidationActorAndDate(entity.ConsolidateAt, entity.OldValue)); static ActorAuditedChange Map(ActorConsolidationAuditLogField field) { @@ -113,7 +115,8 @@ public Task AuditAsync( ChangedByUserId = auditIdentity.Value, Field = (int)Map(change), NewValue = actorConsolidation.ActorToId.ToString(), - OldValue = actorConsolidation.ActorFromId.ToString() + OldValue = actorConsolidation.ActorFromId.ToString(), + ConsolidateAt = actorConsolidation.ConsolidateAt.ToDateTimeOffset() }; _context.ActorConsolidationAuditLogEntries.Add(entity); @@ -129,4 +132,13 @@ static ActorConsolidationAuditLogField Map(GridAreaAuditedChange change) }; } } + + private static string GetSerializedActorConsolidationActorAndDate(DateTimeOffset consolidateAt, string actorId) + { + return JsonSerializer.Serialize(new ActorConsolidationActorAndDate + { + ActorId = Guid.Parse(actorId), + ConsolidateAt = consolidateAt + }); + } } diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs index 32ebace4a..3cc8ca832 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Hosts/WebApi/GetGridAreaAuditLogsHandlerIntegrationTests.cs @@ -15,6 +15,7 @@ using System; using System.Globalization; using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using Energinet.DataHub.Core.App.Common.Abstractions.Users; using Energinet.DataHub.MarketParticipant.Application.Commands.GridAreas; @@ -23,6 +24,8 @@ using Energinet.DataHub.MarketParticipant.Domain.Model; using Energinet.DataHub.MarketParticipant.Domain.Model.Users; using Energinet.DataHub.MarketParticipant.Domain.Repositories; +using Energinet.DataHub.MarketParticipant.Infrastructure.Model; +using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Model; using Energinet.DataHub.MarketParticipant.IntegrationTests.Common; using Energinet.DataHub.MarketParticipant.IntegrationTests.Fixtures; using MediatR; @@ -74,6 +77,7 @@ public Task GetAuditLogs_ConsolidationRequested_IsAudited() { var expectedFrom = new ActorId(Guid.NewGuid()); var expectedTo = new ActorId(Guid.NewGuid()); + var consolidateAt = SystemClock.Instance.GetCurrentInstant(); return TestAuditOfGridAreaChangeAsync( response => @@ -83,8 +87,12 @@ public Task GetAuditLogs_ConsolidationRequested_IsAudited() .Where(log => log.AuditIdentityId != KnownAuditIdentityProvider.TestFramework.IdentityId.Value) .Single(log => log.Change == GridAreaAuditedChange.ConsolidationRequested && !string.IsNullOrEmpty(log.CurrentValue)); - Assert.Equal(expectedTo.ToString(), expectedLog.CurrentValue); - Assert.Equal(expectedFrom.ToString(), expectedLog.PreviousValue); + var expectedCurrentValue = JsonSerializer.Deserialize(expectedLog.CurrentValue); + var expectedPreviousValue = JsonSerializer.Deserialize(expectedLog.PreviousValue); + Assert.Equal(expectedTo.Value, expectedCurrentValue!.ActorId); + Assert.Equal(consolidateAt.ToDateTimeOffset(), expectedCurrentValue.ConsolidateAt); + Assert.Equal(expectedFrom.Value, expectedPreviousValue!.ActorId); + Assert.Equal(consolidateAt.ToDateTimeOffset(), expectedPreviousValue.ConsolidateAt); }, async (gridArea, sp) => { @@ -94,7 +102,7 @@ public Task GetAuditLogs_ConsolidationRequested_IsAudited() var actorConsolidation = new ActorConsolidation( expectedFrom, expectedTo, - SystemClock.Instance.GetCurrentInstant()); + consolidateAt); await actorConsolidationAuditLogRepository.AuditAsync( new AuditIdentity(frontendUser.CurrentUser.UserId), @@ -118,8 +126,10 @@ public Task GetAuditLogs_ConsolidationCompleted_IsAudited() .Where(log => log.AuditIdentityId != KnownAuditIdentityProvider.TestFramework.IdentityId.Value) .Single(log => log.Change == GridAreaAuditedChange.ConsolidationCompleted && !string.IsNullOrEmpty(log.CurrentValue)); - Assert.Equal(expectedTo.ToString(), expectedLog.CurrentValue); - Assert.Equal(expectedFrom.ToString(), expectedLog.PreviousValue); + var expectedCurrentValue = JsonSerializer.Deserialize(expectedLog.CurrentValue); + var expectedPreviousValue = JsonSerializer.Deserialize(expectedLog.PreviousValue); + Assert.Equal(expectedTo.ToString(), expectedCurrentValue.ActorId.ToString()); + Assert.Equal(expectedFrom.ToString(), expectedPreviousValue.ActorId.ToString()); }, async (gridArea, sp) => { diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationAuditLogRepositoryTests.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationAuditLogRepositoryTests.cs index 51967f66f..1e82dd05c 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationAuditLogRepositoryTests.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationAuditLogRepositoryTests.cs @@ -14,9 +14,12 @@ using System; using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using Energinet.DataHub.MarketParticipant.Domain.Model; using Energinet.DataHub.MarketParticipant.Domain.Model.Users; +using Energinet.DataHub.MarketParticipant.Infrastructure.Model; +using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Model; using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Repositories; using Energinet.DataHub.MarketParticipant.IntegrationTests.Common; using Energinet.DataHub.MarketParticipant.IntegrationTests.Fixtures; @@ -65,10 +68,13 @@ await target.AuditAsync( var actual = (await target.GetAsync(gridAreaId)).Single(); // Assert + var actualCurrentValue = JsonSerializer.Deserialize(actual.CurrentValue); + var actualPreviousValue = JsonSerializer.Deserialize(actual.PreviousValue); Assert.Equal(auditIdentity, actual.AuditIdentity); Assert.Equal(GridAreaAuditedChange.ConsolidationRequested, actual.Change); - Assert.Equal(actorConsolidation.ActorFromId.ToString(), actual.PreviousValue); - Assert.Equal(actorConsolidation.ActorToId.ToString(), actual.CurrentValue); + Assert.Equal(actorConsolidation.ActorFromId.ToString(), actualPreviousValue!.ActorId.ToString()); + Assert.Equal(actorConsolidation.ActorToId.ToString(), actualCurrentValue!.ActorId.ToString()); + Assert.Equal(actorConsolidation.ConsolidateAt.ToDateTimeOffset(), actualCurrentValue.ConsolidateAt); } [Fact] @@ -99,9 +105,12 @@ await target.AuditAsync( var actual = (await target.GetAsync(actorConsolidation.ActorFromId)).Single(); // Assert + var actualCurrentValue = JsonSerializer.Deserialize(actual.CurrentValue); + var actualPreviousValue = JsonSerializer.Deserialize(actual.PreviousValue); Assert.Equal(auditIdentity, actual.AuditIdentity); Assert.Equal(ActorAuditedChange.ConsolidationRequested, actual.Change); - Assert.Equal(actorConsolidation.ActorFromId.ToString(), actual.PreviousValue); - Assert.Equal(actorConsolidation.ActorToId.ToString(), actual.CurrentValue); + Assert.Equal(actorConsolidation.ActorFromId.ToString(), actualPreviousValue!.ActorId.ToString()); + Assert.Equal(actorConsolidation.ActorToId.ToString(), actualCurrentValue!.ActorId.ToString()); + Assert.Equal(actorConsolidation.ConsolidateAt.ToDateTimeOffset(), actualCurrentValue.ConsolidateAt); } }