Skip to content

Commit

Permalink
Write consolidations to revlog. (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
defectiveAi authored Dec 18, 2024
1 parent 8031041 commit fc6c563
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsolidateActorsCommand>
{
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,21 +32,24 @@ 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,
IActorCredentialsRemovalService actorCredentialsRemovalService,
IActorRepository actorRepository,
IAuditIdentityProvider auditIdentityProvider,
IDomainEventRepository domainEventRepository,
IGridAreaRepository gridAreaRepository)
IGridAreaRepository gridAreaRepository,
IUnitOfWorkProvider unitOfWorkProvider)
{
_actorConsolidationAuditLogRepository = actorConsolidationAuditLogRepository;
_actorCredentialsRemovalService = actorCredentialsRemovalService;
_actorRepository = actorRepository;
_auditIdentityProvider = auditIdentityProvider;
_domainEventRepository = domainEventRepository;
_gridAreaRepository = gridAreaRepository;
_unitOfWorkProvider = unitOfWorkProvider;
}

public async Task ConsolidateAsync(ActorConsolidation actorConsolidation)
Expand All @@ -58,34 +62,43 @@ 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);

await uow.CommitAsync().ConfigureAwait(false);
}
}

private async Task AuditLogConsolidationCompletedAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,6 +53,7 @@ public async Task ConsolidateAsync_GridAreasAreTransferred_NoException()
var actorCredentialsRemovalService = scope.ServiceProvider.GetRequiredService<IActorCredentialsRemovalService>();
var actorRepository = scope.ServiceProvider.GetRequiredService<IActorRepository>();
var gridAreaRepository = scope.ServiceProvider.GetRequiredService<IGridAreaRepository>();
var unitOfWorkProvider = scope.ServiceProvider.GetRequiredService<IUnitOfWorkProvider>();

var auditIdentityProvider = new Mock<IAuditIdentityProvider>();
auditIdentityProvider.Setup(repo => repo.IdentityId).Returns(new Domain.Model.Users.AuditIdentity(Guid.NewGuid()));
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit fc6c563

Please sign in to comment.