Skip to content

Commit

Permalink
Merge e7170b4 into fc6c563
Browse files Browse the repository at this point in the history
  • Loading branch information
defectiveAi authored Dec 19, 2024
2 parents fc6c563 + e7170b4 commit 6483246
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand All @@ -30,15 +31,18 @@ public sealed class ConsolidateActorsHandler : IRequestHandler<ConsolidateActors
private readonly IActorConsolidationRepository _actorConsolidationRepository;
private readonly IActorConsolidationService _actorConsolidationService;
private readonly IRevisionLogClient _revisionLogClient;
private readonly IUnitOfWorkProvider _unitOfWorkProvider;

public ConsolidateActorsHandler(
IActorConsolidationRepository actorConsolidationRepository,
IActorConsolidationService actorConsolidationService,
IRevisionLogClient revisionLogClient)
IRevisionLogClient revisionLogClient,
IUnitOfWorkProvider unitOfWorkProvider)
{
_actorConsolidationRepository = actorConsolidationRepository;
_actorConsolidationService = actorConsolidationService;
_revisionLogClient = revisionLogClient;
_unitOfWorkProvider = unitOfWorkProvider;
}

public async Task Handle(ConsolidateActorsCommand request, CancellationToken cancellationToken)
Expand All @@ -51,19 +55,34 @@ public async Task Handle(ConsolidateActorsCommand request, CancellationToken can

foreach (var actorConsolidation in actorsReadyToConsolidate)
{
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()))
var uow = await _unitOfWorkProvider
.NewUnitOfWorkAsync()
.ConfigureAwait(false);

await _actorConsolidationService.ConsolidateAsync(actorConsolidation).ConfigureAwait(false);
await using (uow.ConfigureAwait(false))
{
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);

await _actorConsolidationService
.ConsolidateAsync(actorConsolidation)
.ConfigureAwait(false);

await _actorConsolidationRepository
.AddOrUpdateAsync(actorConsolidation)
.ConfigureAwait(false);

await uow.CommitAsync().ConfigureAwait(false);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task Handle(ScheduleConsolidateActorsCommand request, CancellationT
request.Consolidation.ConsolidateAt.ToInstant());

await _actorConsolidationRepository
.AddAsync(actorConsolidation)
.AddOrUpdateAsync(actorConsolidation)
.ConfigureAwait(false);

foreach (var gridArea in fromActor.MarketRole.GridAreas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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 @@ -32,24 +31,21 @@ 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,
IUnitOfWorkProvider unitOfWorkProvider)
IGridAreaRepository gridAreaRepository)
{
_actorConsolidationAuditLogRepository = actorConsolidationAuditLogRepository;
_actorCredentialsRemovalService = actorCredentialsRemovalService;
_actorRepository = actorRepository;
_auditIdentityProvider = auditIdentityProvider;
_domainEventRepository = domainEventRepository;
_gridAreaRepository = gridAreaRepository;
_unitOfWorkProvider = unitOfWorkProvider;
}

public async Task ConsolidateAsync(ActorConsolidation actorConsolidation)
Expand All @@ -62,43 +58,36 @@ public async Task ConsolidateAsync(ActorConsolidation actorConsolidation)
var toActor = await _actorRepository.GetAsync(actorConsolidation.ActorToId).ConfigureAwait(false);
NotFoundValidationException.ThrowIfNull(toActor, actorConsolidation.ActorToId.Value);

var uow = await _unitOfWorkProvider
.NewUnitOfWorkAsync()
.ConfigureAwait(false);

await using (uow.ConfigureAwait(false))
if (fromActor.MarketRole.Function is EicFunction.GridAccessProvider
&& toActor.MarketRole.Function is EicFunction.GridAccessProvider)
{
if (fromActor.MarketRole.Function is EicFunction.GridAccessProvider
&& toActor.MarketRole.Function is EicFunction.GridAccessProvider)
{
var actorGridAreasToTransfer = fromActor.MarketRole.GridAreas.ToList();
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);
}
actorConsolidation.Complete();
}

private async Task AuditLogConsolidationCompletedAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ public ActorConsolidation(ActorConsolidationId id, ActorId fromId, ActorId toId,
Status = status;
}

public ActorConsolidationId Id { get; init; }
public ActorId ActorFromId { get; init; }
public ActorId ActorToId { get; init; }
public Instant ConsolidateAt { get; set; }
public ActorConsolidationStatus Status { get; set; }
public ActorConsolidationId Id { get; }
public ActorId ActorFromId { get; }
public ActorId ActorToId { get; }
public Instant ConsolidateAt { get; }
public ActorConsolidationStatus Status { get; private set; }

public void Complete()
{
Status = ActorConsolidationStatus.Executed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ namespace Energinet.DataHub.MarketParticipant.Domain.Repositories;
public interface IActorConsolidationRepository
{
/// <summary>
/// Adds an <see cref="ActorConsolidation"/>.
/// Adds or updates an <see cref="ActorConsolidation"/>.
/// </summary>
/// <param name="actorConsolidation">The actor to consolidate.</param>
/// <returns>The <see cref="ActorConsolidationId">id</see> of the added <see cref="ActorConsolidation"/>.</returns>
/// <remarks>Throws an exception if the entity to add is not with a default GUID as id.</remarks>
Task<ActorConsolidationId> AddAsync(ActorConsolidation actorConsolidation);
Task<ActorConsolidationId> AddOrUpdateAsync(ActorConsolidation actorConsolidation);

/// <summary>
/// Gets a List of all <see cref="ActorConsolidation"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ActorConsolidationRepository(IMarketParticipantDbContext marketParticipan
_marketParticipantDbContext = marketParticipantDbContext;
}

public async Task<ActorConsolidationId> AddAsync(ActorConsolidation actorConsolidation)
public async Task<ActorConsolidationId> AddOrUpdateAsync(ActorConsolidation actorConsolidation)
{
ArgumentNullException.ThrowIfNull(actorConsolidation, nameof(actorConsolidation));

Expand All @@ -46,7 +46,10 @@ public async Task<ActorConsolidationId> AddAsync(ActorConsolidation actorConsoli
}
else
{
throw new InvalidOperationException($"ActorConsolidation you are adding already exists, or has a non default id");
destination = await _marketParticipantDbContext
.ActorConsolidations
.FindAsync(actorConsolidation.Id.Value)
.ConfigureAwait(false) ?? throw new InvalidOperationException($"ActorConsolidation with id {actorConsolidation.Id.Value} is missing, even though it cannot be deleted.");
}

MapToEntity(actorConsolidation, destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task AddAsync_OneActorConsolidation_CanReadBack()
scheduledAt);

// Act
var consolidationId = await consolidationRepository.AddAsync(testConsolidation);
var consolidationId = await consolidationRepository.AddOrUpdateAsync(testConsolidation);
var newConsolidation = await consolidationRepository2.GetAsync(consolidationId);

// Assert
Expand Down Expand Up @@ -129,8 +129,8 @@ public async Task AddAsync_MultipleActorConsolidations_CanReadBack()
scheduledAt);

// Act
await consolidationRepository.AddAsync(testConsolidation);
await consolidationRepository.AddAsync(testConsolidation2);
await consolidationRepository.AddOrUpdateAsync(testConsolidation);
await consolidationRepository.AddOrUpdateAsync(testConsolidation2);
var consolidations = (await consolidationRepository2.GetAsync()).ToList();

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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 @@ -53,7 +52,6 @@ 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 @@ -88,8 +86,7 @@ public async Task ConsolidateAsync_GridAreasAreTransferred_NoException()
actorRepository,
auditIdentityProvider.Object,
domainEventRepository.Object,
gridAreaRepository,
unitOfWorkProvider);
gridAreaRepository);

var scheduledAt = Instant.FromUtc(2024, 1, 1, 10, 59);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Energinet.DataHub.MarketParticipant.Domain.Model.Users;
using Energinet.DataHub.MarketParticipant.Domain.Repositories;
using Energinet.DataHub.MarketParticipant.Tests.Common;
using Energinet.DataHub.MarketParticipant.Tests.Handlers;
using Moq;
using NodaTime;
using Xunit;
Expand Down Expand Up @@ -63,8 +62,7 @@ public async Task ConsolidateAsync_TwoGridAccessProviders_EmptyDestination_Trans
actorRepository.Object,
auditIdentityProvider.Object,
domainEventRepository.Object,
gridAreaRepository.Object,
UnitOfWorkProviderMock.Create());
gridAreaRepository.Object);

actorRepository
.Setup(repo => repo.GetAsync(validFromActor.Id))
Expand Down Expand Up @@ -119,8 +117,7 @@ public async Task ConsolidateAsync_TwoGridAccessProviders_NotEmptyDestination_Tr
actorRepository.Object,
auditIdentityProvider.Object,
domainEventRepository.Object,
gridAreaRepository.Object,
UnitOfWorkProviderMock.Create());
gridAreaRepository.Object);

actorRepository
.Setup(repo => repo.GetAsync(validFromActor.Id))
Expand Down Expand Up @@ -172,8 +169,7 @@ public async Task ConsolidateAsync_FromNotGridAccessProvider_NoTransfer_Deactiva
actorRepository.Object,
auditIdentityProvider.Object,
domainEventRepository.Object,
gridAreaRepository.Object,
UnitOfWorkProviderMock.Create());
gridAreaRepository.Object);

actorRepository
.Setup(repo => repo.GetAsync(validFromActor.Id))
Expand Down

0 comments on commit 6483246

Please sign in to comment.