From fac8c652cf991caffa5f1789f03f84d75ce4ece4 Mon Sep 17 00:00:00 2001 From: Alex <77341673+defectiveAi@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:06:36 +0100 Subject: [PATCH 1/3] Write consolidations to revlog. --- .../Actors/ConsolidateActorsHandler.cs | 40 +++++++------- .../Services/ActorConsolidationService.cs | 53 +++++++++++-------- .../SubsystemInformation.cs | 4 +- 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/Actors/ConsolidateActorsHandler.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/Actors/ConsolidateActorsHandler.cs index 39ec93590..3548b1b6a 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/Actors/ConsolidateActorsHandler.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/Actors/ConsolidateActorsHandler.cs @@ -17,51 +17,53 @@ using System.Threading.Tasks; using Energinet.DataHub.MarketParticipant.Application.Commands.Actors; using Energinet.DataHub.MarketParticipant.Application.Services; -using Energinet.DataHub.MarketParticipant.Domain; +using Energinet.DataHub.MarketParticipant.Domain.Model; using Energinet.DataHub.MarketParticipant.Domain.Repositories; +using Energinet.DataHub.RevisionLog.Integration; using MediatR; +using NodaTime; namespace Energinet.DataHub.MarketParticipant.Application.Handlers.Actors; public sealed class ConsolidateActorsHandler : IRequestHandler { private readonly IActorConsolidationRepository _actorConsolidationRepository; - private readonly IDomainEventRepository _domainEventRepository; - private readonly IUnitOfWorkProvider _unitOfWorkProvider; private readonly IActorConsolidationService _actorConsolidationService; + private readonly IRevisionLogClient _revisionLogClient; public ConsolidateActorsHandler( IActorConsolidationRepository actorConsolidationRepository, - IDomainEventRepository domainEventRepository, - IUnitOfWorkProvider unitOfWorkProvider, - IActorConsolidationService actorConsolidationService) + IActorConsolidationService actorConsolidationService, + IRevisionLogClient revisionLogClient) { _actorConsolidationRepository = actorConsolidationRepository; - _domainEventRepository = domainEventRepository; - _unitOfWorkProvider = unitOfWorkProvider; _actorConsolidationService = actorConsolidationService; + _revisionLogClient = revisionLogClient; } public async Task Handle(ConsolidateActorsCommand request, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(request, nameof(request)); - var uow = await _unitOfWorkProvider - .NewUnitOfWorkAsync() + var actorsReadyToConsolidate = await _actorConsolidationRepository + .GetReadyToConsolidateAsync() .ConfigureAwait(false); - await using (uow.ConfigureAwait(false)) + foreach (var actorConsolidation in actorsReadyToConsolidate) { - var actorsReadyToConsolidate = await _actorConsolidationRepository - .GetReadyToConsolidateAsync() + await _revisionLogClient + .LogAsync(new RevisionLogEntry( + logId: Guid.NewGuid(), + systemId: SubsystemInformation.Id, + activity: "ConsolidateActor", + occurredOn: SystemClock.Instance.GetCurrentInstant(), + origin: nameof(ConsolidateActorsHandler), + affectedEntityType: nameof(Actor), + affectedEntityKey: actorConsolidation.ActorFromId.ToString(), + payload: actorConsolidation.Id.ToString())) .ConfigureAwait(false); - foreach (var actorConsolidation in actorsReadyToConsolidate) - { - await _actorConsolidationService.ConsolidateAsync(actorConsolidation).ConfigureAwait(false); - } - - await uow.CommitAsync().ConfigureAwait(false); + await _actorConsolidationService.ConsolidateAsync(actorConsolidation).ConfigureAwait(false); } } } diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs index 4c747895e..c3d35eb79 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs @@ -16,6 +16,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Energinet.DataHub.MarketParticipant.Domain; using Energinet.DataHub.MarketParticipant.Domain.Exception; using Energinet.DataHub.MarketParticipant.Domain.Model; using Energinet.DataHub.MarketParticipant.Domain.Repositories; @@ -31,6 +32,7 @@ public sealed class ActorConsolidationService : IActorConsolidationService private readonly IAuditIdentityProvider _auditIdentityProvider; private readonly IDomainEventRepository _domainEventRepository; private readonly IGridAreaRepository _gridAreaRepository; + private readonly IUnitOfWorkProvider _unitOfWorkProvider; public ActorConsolidationService( IActorConsolidationAuditLogRepository actorConsolidationAuditLogRepository, @@ -38,7 +40,8 @@ public ActorConsolidationService( IActorRepository actorRepository, IAuditIdentityProvider auditIdentityProvider, IDomainEventRepository domainEventRepository, - IGridAreaRepository gridAreaRepository) + IGridAreaRepository gridAreaRepository, + IUnitOfWorkProvider unitOfWorkProvider) { _actorConsolidationAuditLogRepository = actorConsolidationAuditLogRepository; _actorCredentialsRemovalService = actorCredentialsRemovalService; @@ -46,6 +49,7 @@ public ActorConsolidationService( _auditIdentityProvider = auditIdentityProvider; _domainEventRepository = domainEventRepository; _gridAreaRepository = gridAreaRepository; + _unitOfWorkProvider = unitOfWorkProvider; } public async Task ConsolidateAsync(ActorConsolidation actorConsolidation) @@ -58,34 +62,41 @@ public async Task ConsolidateAsync(ActorConsolidation actorConsolidation) var toActor = await _actorRepository.GetAsync(actorConsolidation.ActorToId).ConfigureAwait(false); NotFoundValidationException.ThrowIfNull(toActor, actorConsolidation.ActorToId.Value); - if (fromActor.MarketRole.Function is EicFunction.GridAccessProvider - && toActor.MarketRole.Function is EicFunction.GridAccessProvider) + var uow = await _unitOfWorkProvider + .NewUnitOfWorkAsync() + .ConfigureAwait(false); + + await using (uow.ConfigureAwait(false)) { - var actorGridAreasToTransfer = fromActor.MarketRole.GridAreas.ToList(); + if (fromActor.MarketRole.Function is EicFunction.GridAccessProvider + && toActor.MarketRole.Function is EicFunction.GridAccessProvider) + { + var actorGridAreasToTransfer = fromActor.MarketRole.GridAreas.ToList(); - toActor.TransferGridAreasFrom(fromActor); + toActor.TransferGridAreasFrom(fromActor); - await UpdateGridAreasValidToDateAsync(actorGridAreasToTransfer, actorConsolidation.ConsolidateAt).ConfigureAwait(false); - await AuditLogConsolidationCompletedAsync(actorGridAreasToTransfer, actorConsolidation).ConfigureAwait(false); - } + await UpdateGridAreasValidToDateAsync(actorGridAreasToTransfer, actorConsolidation.ConsolidateAt).ConfigureAwait(false); + await AuditLogConsolidationCompletedAsync(actorGridAreasToTransfer, actorConsolidation).ConfigureAwait(false); + } - await DeactivateActorAsync(fromActor).ConfigureAwait(false); + await DeactivateActorAsync(fromActor).ConfigureAwait(false); - await _actorRepository - .AddOrUpdateAsync(fromActor) - .ConfigureAwait(false); + await _actorRepository + .AddOrUpdateAsync(fromActor) + .ConfigureAwait(false); - await _actorRepository - .AddOrUpdateAsync(toActor) - .ConfigureAwait(false); + await _actorRepository + .AddOrUpdateAsync(toActor) + .ConfigureAwait(false); - await _domainEventRepository - .EnqueueAsync(fromActor) - .ConfigureAwait(false); + await _domainEventRepository + .EnqueueAsync(fromActor) + .ConfigureAwait(false); - await _domainEventRepository - .EnqueueAsync(toActor) - .ConfigureAwait(false); + await _domainEventRepository + .EnqueueAsync(toActor) + .ConfigureAwait(false); + } } private async Task AuditLogConsolidationCompletedAsync( diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/SubsystemInformation.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/SubsystemInformation.cs index ffb3de3e0..67c3a3478 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/SubsystemInformation.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/SubsystemInformation.cs @@ -18,6 +18,6 @@ namespace Energinet.DataHub.MarketParticipant.Application; public static class SubsystemInformation { - public static readonly Guid Id = Guid.Parse("DA19142E-D419-4ED2-9798-CE5546260F84"); - public static readonly string Name = "mark-part"; + public static Guid Id { get; } = Guid.Parse("DA19142E-D419-4ED2-9798-CE5546260F84"); + public static string Name => "mark-part"; } From afdaf10c48295f28edcc5ea7f8ceec644c5135b7 Mon Sep 17 00:00:00 2001 From: Alex <77341673+defectiveAi@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:11:30 +0100 Subject: [PATCH 2/3] Fix more tests. --- .../ActorConsolidationServiceIntegrationTests.cs | 6 +++++- .../Services/ActorConsolidationServiceTests.cs | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Services/ActorConsolidationServiceIntegrationTests.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Services/ActorConsolidationServiceIntegrationTests.cs index c1043bc33..c11ea12f4 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Services/ActorConsolidationServiceIntegrationTests.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Services/ActorConsolidationServiceIntegrationTests.cs @@ -15,6 +15,7 @@ using System; using System.Threading.Tasks; using Energinet.DataHub.MarketParticipant.Application.Services; +using Energinet.DataHub.MarketParticipant.Domain; using Energinet.DataHub.MarketParticipant.Domain.Model; using Energinet.DataHub.MarketParticipant.Domain.Repositories; using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Model; @@ -52,6 +53,7 @@ public async Task ConsolidateAsync_GridAreasAreTransferred_NoException() var actorCredentialsRemovalService = scope.ServiceProvider.GetRequiredService(); var actorRepository = scope.ServiceProvider.GetRequiredService(); var gridAreaRepository = scope.ServiceProvider.GetRequiredService(); + var unitOfWorkProvider = scope.ServiceProvider.GetRequiredService(); var auditIdentityProvider = new Mock(); auditIdentityProvider.Setup(repo => repo.IdentityId).Returns(new Domain.Model.Users.AuditIdentity(Guid.NewGuid())); @@ -86,7 +88,9 @@ public async Task ConsolidateAsync_GridAreasAreTransferred_NoException() actorRepository, auditIdentityProvider.Object, domainEventRepository.Object, - gridAreaRepository); + gridAreaRepository, + unitOfWorkProvider); + var scheduledAt = Instant.FromUtc(2024, 1, 1, 10, 59); // Act diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Services/ActorConsolidationServiceTests.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Services/ActorConsolidationServiceTests.cs index 0b1d531f8..d347d328a 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Services/ActorConsolidationServiceTests.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Services/ActorConsolidationServiceTests.cs @@ -16,12 +16,9 @@ using System.Collections.Generic; using System.Threading.Tasks; using Energinet.DataHub.MarketParticipant.Application.Services; -using Energinet.DataHub.MarketParticipant.Domain; using Energinet.DataHub.MarketParticipant.Domain.Model; using Energinet.DataHub.MarketParticipant.Domain.Model.Users; using Energinet.DataHub.MarketParticipant.Domain.Repositories; -using Energinet.DataHub.MarketParticipant.Domain.Services; -using Energinet.DataHub.MarketParticipant.Infrastructure.Persistence.Repositories; using Energinet.DataHub.MarketParticipant.Tests.Common; using Energinet.DataHub.MarketParticipant.Tests.Handlers; using Moq; @@ -66,7 +63,8 @@ public async Task ConsolidateAsync_TwoGridAccessProviders_EmptyDestination_Trans actorRepository.Object, auditIdentityProvider.Object, domainEventRepository.Object, - gridAreaRepository.Object); + gridAreaRepository.Object, + UnitOfWorkProviderMock.Create()); actorRepository .Setup(repo => repo.GetAsync(validFromActor.Id)) @@ -121,7 +119,8 @@ public async Task ConsolidateAsync_TwoGridAccessProviders_NotEmptyDestination_Tr actorRepository.Object, auditIdentityProvider.Object, domainEventRepository.Object, - gridAreaRepository.Object); + gridAreaRepository.Object, + UnitOfWorkProviderMock.Create()); actorRepository .Setup(repo => repo.GetAsync(validFromActor.Id)) @@ -173,7 +172,8 @@ public async Task ConsolidateAsync_FromNotGridAccessProvider_NoTransfer_Deactiva actorRepository.Object, auditIdentityProvider.Object, domainEventRepository.Object, - gridAreaRepository.Object); + gridAreaRepository.Object, + UnitOfWorkProviderMock.Create()); actorRepository .Setup(repo => repo.GetAsync(validFromActor.Id)) From f7f5182b500a17e093ce46f879723ddda3b6143c Mon Sep 17 00:00:00 2001 From: Alex <77341673+defectiveAi@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:29:07 +0100 Subject: [PATCH 3/3] Whoops --- .../Services/ActorConsolidationService.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs index c3d35eb79..7ef727a69 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs @@ -96,6 +96,8 @@ await _domainEventRepository await _domainEventRepository .EnqueueAsync(toActor) .ConfigureAwait(false); + + await uow.CommitAsync().ConfigureAwait(false); } }