diff --git a/source/settlement-report/SettlementReports.Application/Handlers/RequestSettlementReportHandler.cs b/source/settlement-report/SettlementReports.Application/Handlers/RequestSettlementReportHandler.cs index eb89afd..36e6ef4 100644 --- a/source/settlement-report/SettlementReports.Application/Handlers/RequestSettlementReportHandler.cs +++ b/source/settlement-report/SettlementReports.Application/Handlers/RequestSettlementReportHandler.cs @@ -13,9 +13,12 @@ // limitations under the License. using Energinet.DataHub.SettlementReport.Application.Commands; +using Energinet.DataHub.SettlementReport.Application.Model; +using Energinet.DataHub.SettlementReport.Application.Services; using Energinet.DataHub.SettlementReport.Interfaces.Helpers; using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2; using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models; +using NodaTime.Extensions; namespace Energinet.DataHub.SettlementReport.Application.Handlers; @@ -23,19 +26,51 @@ public sealed class RequestSettlementReportHandler : IRequestSettlementReportJob { private readonly IDatabricksJobsHelper _jobHelper; private readonly ISettlementReportInitializeHandler _settlementReportInitializeHandler; + private readonly IGridAreaOwnerRepository _gridAreaOwnerRepository; public RequestSettlementReportHandler( IDatabricksJobsHelper jobHelper, - ISettlementReportInitializeHandler settlementReportInitializeHandler) + ISettlementReportInitializeHandler settlementReportInitializeHandler, + IGridAreaOwnerRepository gridAreaOwnerRepository) { _jobHelper = jobHelper; _settlementReportInitializeHandler = settlementReportInitializeHandler; + _gridAreaOwnerRepository = gridAreaOwnerRepository; } public async Task HandleAsync(RequestSettlementReportCommand request) { var reportId = new SettlementReportRequestId(Guid.NewGuid().ToString()); + + if (request.MarketRole == MarketRole.GridAccessProvider) + { + JobRunId? firstRunId = null; + + var gridAreas = request.RequestDto.Filter.GridAreas.Select(x => x.Key); + + var distinctOwners = await GetGridAreaOwnersAsync(gridAreas, request.RequestDto.Filter).ConfigureAwait(false); + + foreach (var owner in distinctOwners) + { + var id = await _jobHelper.RunSettlementReportsJobAsync(request.RequestDto, request.MarketRole, reportId, owner.Value).ConfigureAwait(false); + firstRunId ??= id; + + await _settlementReportInitializeHandler + .InitializeFromJobAsync( + request.UserId, + request.ActorId, + request.IsFas, + id, + reportId, + request.RequestDto) + .ConfigureAwait(false); + } + + return firstRunId!; + } + var runId = await _jobHelper.RunSettlementReportsJobAsync(request.RequestDto, request.MarketRole, reportId, request.ActorGln).ConfigureAwait(false); + await _settlementReportInitializeHandler .InitializeFromJobAsync( request.UserId, @@ -48,4 +83,24 @@ await _settlementReportInitializeHandler return runId; } + + private async Task> GetGridAreaOwnersAsync(IEnumerable gridAreaCodes, SettlementReportRequestFilterDto filter) + { + var gridAreaOwners = new HashSet(); + + foreach (var grid in gridAreaCodes) + { + var owners = await _gridAreaOwnerRepository.GetGridAreaOwnersAsync( + new GridAreaCode(grid), + filter.PeriodStart.ToInstant(), + filter.PeriodEnd.ToInstant()).ConfigureAwait(false); + + foreach (var owner in owners) + { + gridAreaOwners.Add(owner.ActorNumber); + } + } + + return gridAreaOwners; + } } diff --git a/source/settlement-report/SettlementReports.Infrastructure/Model/ActorNumber.cs b/source/settlement-report/SettlementReports.Application/Model/ActorNumber.cs similarity index 90% rename from source/settlement-report/SettlementReports.Infrastructure/Model/ActorNumber.cs rename to source/settlement-report/SettlementReports.Application/Model/ActorNumber.cs index a373678..fb13c53 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Model/ActorNumber.cs +++ b/source/settlement-report/SettlementReports.Application/Model/ActorNumber.cs @@ -12,6 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -namespace Energinet.DataHub.SettlementReport.Infrastructure.Model; +namespace Energinet.DataHub.SettlementReport.Application.Model; public sealed record ActorNumber(string Value); diff --git a/source/settlement-report/SettlementReports.Infrastructure/Model/GridAreaCode.cs b/source/settlement-report/SettlementReports.Application/Model/GridAreaCode.cs similarity index 90% rename from source/settlement-report/SettlementReports.Infrastructure/Model/GridAreaCode.cs rename to source/settlement-report/SettlementReports.Application/Model/GridAreaCode.cs index 45aa255..d2a2ccb 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Model/GridAreaCode.cs +++ b/source/settlement-report/SettlementReports.Application/Model/GridAreaCode.cs @@ -12,6 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -namespace Energinet.DataHub.SettlementReport.Infrastructure.Model; +namespace Energinet.DataHub.SettlementReport.Application.Model; public sealed record GridAreaCode(string Value); diff --git a/source/settlement-report/SettlementReports.Infrastructure/Model/GridAreaOwner.cs b/source/settlement-report/SettlementReports.Application/Model/GridAreaOwner.cs similarity index 91% rename from source/settlement-report/SettlementReports.Infrastructure/Model/GridAreaOwner.cs rename to source/settlement-report/SettlementReports.Application/Model/GridAreaOwner.cs index 012c74d..d1e8dfe 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Model/GridAreaOwner.cs +++ b/source/settlement-report/SettlementReports.Application/Model/GridAreaOwner.cs @@ -14,6 +14,6 @@ using NodaTime; -namespace Energinet.DataHub.SettlementReport.Infrastructure.Model; +namespace Energinet.DataHub.SettlementReport.Application.Model; public sealed record GridAreaOwner(GridAreaCode Code, ActorNumber ActorNumber, Instant ValidFrom); diff --git a/source/settlement-report/SettlementReports.Infrastructure/Services/IGridAreaOwnerRepository.cs b/source/settlement-report/SettlementReports.Application/Services/IGridAreaOwnerRepository.cs similarity index 85% rename from source/settlement-report/SettlementReports.Infrastructure/Services/IGridAreaOwnerRepository.cs rename to source/settlement-report/SettlementReports.Application/Services/IGridAreaOwnerRepository.cs index 834b84b..9214b1d 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Services/IGridAreaOwnerRepository.cs +++ b/source/settlement-report/SettlementReports.Application/Services/IGridAreaOwnerRepository.cs @@ -12,10 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -using Energinet.DataHub.SettlementReport.Infrastructure.Model; +using Energinet.DataHub.SettlementReport.Application.Model; using NodaTime; -namespace Energinet.DataHub.SettlementReport.Infrastructure.Services; +namespace Energinet.DataHub.SettlementReport.Application.Services; public interface IGridAreaOwnerRepository { diff --git a/source/settlement-report/SettlementReports.Infrastructure/Services/GridAreaOwnerRepository.cs b/source/settlement-report/SettlementReports.Infrastructure/Services/GridAreaOwnerRepository.cs index 822ea12..75e0494 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Services/GridAreaOwnerRepository.cs +++ b/source/settlement-report/SettlementReports.Infrastructure/Services/GridAreaOwnerRepository.cs @@ -12,8 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +using Energinet.DataHub.SettlementReport.Application.Model; +using Energinet.DataHub.SettlementReport.Application.Services; using Energinet.DataHub.SettlementReport.Infrastructure.Contracts; -using Energinet.DataHub.SettlementReport.Infrastructure.Model; using Energinet.DataHub.SettlementReport.Infrastructure.Persistence; using Microsoft.EntityFrameworkCore; using NodaTime; diff --git a/source/settlement-report/SettlementReports.IntegrationTests/Infrastructure/SettlementReports_v2/Persistence/GridAreaOwnerRepositoryTests.cs b/source/settlement-report/SettlementReports.IntegrationTests/Infrastructure/SettlementReports_v2/Persistence/GridAreaOwnerRepositoryTests.cs index 5dace58..d8f97c3 100644 --- a/source/settlement-report/SettlementReports.IntegrationTests/Infrastructure/SettlementReports_v2/Persistence/GridAreaOwnerRepositoryTests.cs +++ b/source/settlement-report/SettlementReports.IntegrationTests/Infrastructure/SettlementReports_v2/Persistence/GridAreaOwnerRepositoryTests.cs @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +using Energinet.DataHub.SettlementReport.Application.Model; using Energinet.DataHub.SettlementReport.Infrastructure.Contracts; -using Energinet.DataHub.SettlementReport.Infrastructure.Model; using Energinet.DataHub.SettlementReport.Infrastructure.Persistence; using Energinet.DataHub.SettlementReport.Infrastructure.Services; using Energinet.DataHub.SettlementReport.Test.Core.Fixture.Database; diff --git a/source/settlement-report/SettlementReports.UnitTests/Handlers/RequestSettlementReportHandlerTests.cs b/source/settlement-report/SettlementReports.UnitTests/Handlers/RequestSettlementReportHandlerTests.cs index 3835996..b41bed3 100644 --- a/source/settlement-report/SettlementReports.UnitTests/Handlers/RequestSettlementReportHandlerTests.cs +++ b/source/settlement-report/SettlementReports.UnitTests/Handlers/RequestSettlementReportHandlerTests.cs @@ -15,6 +15,7 @@ using System.Collections.ObjectModel; using Energinet.DataHub.SettlementReport.Application.Commands; using Energinet.DataHub.SettlementReport.Application.Handlers; +using Energinet.DataHub.SettlementReport.Application.Services; using Energinet.DataHub.SettlementReport.Interfaces.Helpers; using Energinet.DataHub.SettlementReport.Interfaces.Models; using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2; @@ -55,7 +56,7 @@ public async Task Handle_ValidRequest_ReturnsJobId() .ReturnsAsync(jobRunId); var command = new RequestSettlementReportCommand(request, Guid.NewGuid(), Guid.NewGuid(), true, "1233", MarketRole.EnergySupplier); - var handler = new RequestSettlementReportHandler(jobHelperMock.Object, initializerMock.Object); + var handler = new RequestSettlementReportHandler(jobHelperMock.Object, initializerMock.Object, new Mock().Object); // Act var result = await handler.HandleAsync(command); diff --git a/source/settlement-report/SettlementReports.WebAPI/Extensions/DependencyInjection/SettlementReportModuleExtensions.cs b/source/settlement-report/SettlementReports.WebAPI/Extensions/DependencyInjection/SettlementReportModuleExtensions.cs index 9b11c88..ff2dd8c 100644 --- a/source/settlement-report/SettlementReports.WebAPI/Extensions/DependencyInjection/SettlementReportModuleExtensions.cs +++ b/source/settlement-report/SettlementReports.WebAPI/Extensions/DependencyInjection/SettlementReportModuleExtensions.cs @@ -14,6 +14,7 @@ using Energinet.DataHub.Core.App.Common.Extensions.DependencyInjection; using Energinet.DataHub.SettlementReport.Application.Handlers; +using Energinet.DataHub.SettlementReport.Application.Services; using Energinet.DataHub.SettlementReport.Application.SettlementReports_v2; using Energinet.DataHub.SettlementReport.Common.Infrastructure.Extensions.Options; using Energinet.DataHub.SettlementReport.Common.Infrastructure.HealthChecks;