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 3548b1b6a..cc1ce2c2c 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/Actors/ConsolidateActorsHandler.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/Actors/ConsolidateActorsHandler.cs @@ -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; @@ -30,15 +31,18 @@ public sealed class ConsolidateActorsHandler : IRequestHandler - /// Adds an . + /// Adds or updates an . /// /// The actor to consolidate. /// The id of the added . - /// Throws an exception if the entity to add is not with a default GUID as id. - Task AddAsync(ActorConsolidation actorConsolidation); + Task AddOrUpdateAsync(ActorConsolidation actorConsolidation); /// /// Gets a List of all . diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Repositories/ActorConsolidationRepository.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Repositories/ActorConsolidationRepository.cs index 30c91cb7c..872f9d384 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Repositories/ActorConsolidationRepository.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Infrastructure/Persistence/Repositories/ActorConsolidationRepository.cs @@ -35,7 +35,7 @@ public ActorConsolidationRepository(IMarketParticipantDbContext marketParticipan _marketParticipantDbContext = marketParticipantDbContext; } - public async Task AddAsync(ActorConsolidation actorConsolidation) + public async Task AddOrUpdateAsync(ActorConsolidation actorConsolidation) { ArgumentNullException.ThrowIfNull(actorConsolidation, nameof(actorConsolidation)); @@ -46,7 +46,10 @@ public async Task 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); diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationRepositoryTests.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationRepositoryTests.cs index 071e63c6a..7a27bb6ba 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationRepositoryTests.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Repositories/ActorConsolidationRepositoryTests.cs @@ -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 @@ -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 diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Services/ActorConsolidationServiceIntegrationTests.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Services/ActorConsolidationServiceIntegrationTests.cs index c11ea12f4..e49b638ca 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Services/ActorConsolidationServiceIntegrationTests.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.IntegrationTests/Services/ActorConsolidationServiceIntegrationTests.cs @@ -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; @@ -53,7 +52,6 @@ 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())); @@ -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); diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Services/ActorConsolidationServiceTests.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Services/ActorConsolidationServiceTests.cs index d347d328a..fc33c9c65 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Services/ActorConsolidationServiceTests.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Services/ActorConsolidationServiceTests.cs @@ -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; @@ -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)) @@ -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)) @@ -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))