Skip to content

Commit

Permalink
feat: settlement report - include previous grid owners
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmajgaard committed Dec 5, 2024
1 parent 655e5e6 commit fef1252
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,64 @@
// 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;

public sealed class RequestSettlementReportHandler : IRequestSettlementReportJobHandler
{
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<JobRunId> 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,
Expand All @@ -48,4 +83,24 @@ await _settlementReportInitializeHandler

return runId;
}

private async Task<IEnumerable<ActorNumber>> GetGridAreaOwnersAsync(IEnumerable<string> gridAreaCodes, SettlementReportRequestFilterDto filter)
{
var gridAreaOwners = new HashSet<ActorNumber>();

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IGridAreaOwnerRepository>().Object);

// Act
var result = await handler.HandleAsync(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fef1252

Please sign in to comment.