Skip to content

Commit

Permalink
feat: Update ActorConsolidationAuditLogRepository to have an object i…
Browse files Browse the repository at this point in the history
…n new and old value (#1050)
  • Loading branch information
FirestarJes authored Dec 18, 2024
1 parent a03300a commit 8031041
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE [dbo].[ActorConsolidationAuditLogEntry]
ADD [ConsolidateAt] [datetimeoffset] NOT NULL DEFAULT (GETUTCDATE())
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE [dbo].[ActorConsolidationAuditLogEntry]
ADD [ConsolidateAt] [datetimeoffset] NOT NULL DEFAULT (GETUTCDATE())
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 =>
Expand All @@ -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<ActorConsolidationActorAndDate>(expectedLog.CurrentValue);
var expectedPreviousValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(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) =>
{
Expand All @@ -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),
Expand All @@ -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<ActorConsolidationActorAndDate>(expectedLog.CurrentValue);
var expectedPreviousValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(expectedLog.PreviousValue);
Assert.Equal(expectedTo.ToString(), expectedCurrentValue.ActorId.ToString());
Assert.Equal(expectedFrom.ToString(), expectedPreviousValue.ActorId.ToString());
},
async (gridArea, sp) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,10 +68,13 @@ await target.AuditAsync(
var actual = (await target.GetAsync(gridAreaId)).Single();

// Assert
var actualCurrentValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(actual.CurrentValue);
var actualPreviousValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(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]
Expand Down Expand Up @@ -99,9 +105,12 @@ await target.AuditAsync(
var actual = (await target.GetAsync(actorConsolidation.ActorFromId)).Single();

// Assert
var actualCurrentValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(actual.CurrentValue);
var actualPreviousValue = JsonSerializer.Deserialize<ActorConsolidationActorAndDate>(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);
}
}

0 comments on commit 8031041

Please sign in to comment.