diff --git a/source/settlement-report/Orchestration.SettlementReports/Functions/SettlementReports/SettlementReportRequestTrigger.cs b/source/settlement-report/Orchestration.SettlementReports/Functions/SettlementReports/SettlementReportRequestTrigger.cs index 68e0608..d1b8ec5 100644 --- a/source/settlement-report/Orchestration.SettlementReports/Functions/SettlementReports/SettlementReportRequestTrigger.cs +++ b/source/settlement-report/Orchestration.SettlementReports/Functions/SettlementReports/SettlementReportRequestTrigger.cs @@ -64,7 +64,23 @@ await _revisionLogClient.LogAsync( payload: System.Text.Json.JsonSerializer.Serialize(settlementReportRequest))) .ConfigureAwait(false); - if (_userContext.CurrentUser.Actor.MarketRole == FrontendActorMarketRole.EnergySupplier && string.IsNullOrWhiteSpace(settlementReportRequest.Filter.EnergySupplier)) + var marketRole = _userContext.CurrentUser.Actor.MarketRole switch + { + FrontendActorMarketRole.Other => MarketRole.Other, + FrontendActorMarketRole.GridAccessProvider => MarketRole.GridAccessProvider, + FrontendActorMarketRole.EnergySupplier => MarketRole.EnergySupplier, + FrontendActorMarketRole.SystemOperator => MarketRole.SystemOperator, + FrontendActorMarketRole.DataHubAdministrator => MarketRole.DataHubAdministrator, + _ => throw new ArgumentOutOfRangeException(nameof(_userContext.CurrentUser.Actor.MarketRole)), + }; + + if (_userContext.CurrentUser is { MultiTenancy: true, Actor.MarketRole: FrontendActorMarketRole.DataHubAdministrator } && + settlementReportRequest.MarketRoleOverride.HasValue) + { + marketRole = settlementReportRequest.MarketRoleOverride.Value; + } + + if (marketRole == MarketRole.EnergySupplier && string.IsNullOrWhiteSpace(settlementReportRequest.Filter.EnergySupplier)) { settlementReportRequest = settlementReportRequest with { @@ -75,7 +91,7 @@ await _revisionLogClient.LogAsync( }; } - if (!IsValid(settlementReportRequest)) + if (!IsValid(settlementReportRequest, marketRole)) { return req.CreateResponse(HttpStatusCode.Forbidden); } @@ -86,16 +102,6 @@ await _revisionLogClient.LogAsync( return req.CreateResponse(HttpStatusCode.BadRequest); } - var marketRole = _userContext.CurrentUser.Actor.MarketRole switch - { - FrontendActorMarketRole.Other => MarketRole.Other, - FrontendActorMarketRole.GridAccessProvider => MarketRole.GridAccessProvider, - FrontendActorMarketRole.EnergySupplier => MarketRole.EnergySupplier, - FrontendActorMarketRole.SystemOperator => MarketRole.SystemOperator, - FrontendActorMarketRole.DataHubAdministrator => MarketRole.DataHubAdministrator, - _ => throw new ArgumentOutOfRangeException(nameof(_userContext.CurrentUser.Actor.MarketRole)), - }; - var chargeOwnerId = marketRole is MarketRole.GridAccessProvider or MarketRole.SystemOperator ? _userContext.CurrentUser.Actor.ActorNumber : null; @@ -125,16 +131,14 @@ await response return response; } - private bool IsValid(SettlementReportRequestDto req) + private bool IsValid(SettlementReportRequestDto req, MarketRole marketRole) { if (_userContext.CurrentUser.MultiTenancy) { return true; } - var marketRole = _userContext.CurrentUser.Actor.MarketRole; - - if (marketRole == FrontendActorMarketRole.GridAccessProvider) + if (marketRole == MarketRole.GridAccessProvider) { if (!string.IsNullOrWhiteSpace(req.Filter.EnergySupplier)) { @@ -144,12 +148,12 @@ private bool IsValid(SettlementReportRequestDto req) return req.Filter.GridAreas.All(x => _userContext.CurrentUser.Actor.GridAreas.Contains(x.Key)); } - if (marketRole == FrontendActorMarketRole.EnergySupplier) + if (marketRole == MarketRole.EnergySupplier) { return req.Filter.EnergySupplier == _userContext.CurrentUser.Actor.ActorNumber; } - if (marketRole == FrontendActorMarketRole.SystemOperator && + if (marketRole == MarketRole.SystemOperator && req.Filter.CalculationType != CalculationType.BalanceFixing && req.Filter.CalculationType != CalculationType.Aggregation) { diff --git a/source/settlement-report/SettlementReports.Interfaces/SettlementReports_v2/Models/SettlementReportRequestDto.cs b/source/settlement-report/SettlementReports.Interfaces/SettlementReports_v2/Models/SettlementReportRequestDto.cs index 235d1c2..9d3836b 100644 --- a/source/settlement-report/SettlementReports.Interfaces/SettlementReports_v2/Models/SettlementReportRequestDto.cs +++ b/source/settlement-report/SettlementReports.Interfaces/SettlementReports_v2/Models/SettlementReportRequestDto.cs @@ -19,4 +19,5 @@ public sealed record SettlementReportRequestDto( bool PreventLargeTextFiles, bool IncludeBasisData, bool IncludeMonthlyAmount, - SettlementReportRequestFilterDto Filter); + SettlementReportRequestFilterDto Filter, + MarketRole? MarketRoleOverride = null); diff --git a/source/settlement-report/SettlementReports.WebAPI/Controllers/SettlementReportsController.cs b/source/settlement-report/SettlementReports.WebAPI/Controllers/SettlementReportsController.cs index a8a1d4d..8105f48 100644 --- a/source/settlement-report/SettlementReports.WebAPI/Controllers/SettlementReportsController.cs +++ b/source/settlement-report/SettlementReports.WebAPI/Controllers/SettlementReportsController.cs @@ -54,7 +54,23 @@ public SettlementReportsController( [EnableRevision(activityName: "RequestSettlementReportAPI", entityType: typeof(SettlementReportRequestDto))] public async Task> RequestSettlementReport([FromBody] SettlementReportRequestDto settlementReportRequest) { - if (_userContext.CurrentUser.Actor.MarketRole == FrontendActorMarketRole.EnergySupplier && string.IsNullOrWhiteSpace(settlementReportRequest.Filter.EnergySupplier)) + var marketRole = _userContext.CurrentUser.Actor.MarketRole switch + { + FrontendActorMarketRole.Other => MarketRole.Other, + FrontendActorMarketRole.GridAccessProvider => MarketRole.GridAccessProvider, + FrontendActorMarketRole.EnergySupplier => MarketRole.EnergySupplier, + FrontendActorMarketRole.SystemOperator => MarketRole.SystemOperator, + FrontendActorMarketRole.DataHubAdministrator => MarketRole.DataHubAdministrator, + _ => throw new ArgumentOutOfRangeException(nameof(_userContext.CurrentUser.Actor.MarketRole)), + }; + + if (_userContext.CurrentUser is { MultiTenancy: true, Actor.MarketRole: FrontendActorMarketRole.DataHubAdministrator } && + settlementReportRequest.MarketRoleOverride.HasValue) + { + marketRole = settlementReportRequest.MarketRoleOverride.Value; + } + + if (marketRole == MarketRole.EnergySupplier && string.IsNullOrWhiteSpace(settlementReportRequest.Filter.EnergySupplier)) { settlementReportRequest = settlementReportRequest with { @@ -65,7 +81,7 @@ public async Task> RequestSettlementReport([FromBody] Settlem }; } - if (!IsValid(settlementReportRequest)) + if (!IsValid(settlementReportRequest, marketRole)) { return Forbid(); } @@ -76,16 +92,6 @@ public async Task> RequestSettlementReport([FromBody] Settlem return BadRequest(); } - var marketRole = _userContext.CurrentUser.Actor.MarketRole switch - { - FrontendActorMarketRole.Other => MarketRole.Other, - FrontendActorMarketRole.GridAccessProvider => MarketRole.GridAccessProvider, - FrontendActorMarketRole.EnergySupplier => MarketRole.EnergySupplier, - FrontendActorMarketRole.SystemOperator => MarketRole.SystemOperator, - FrontendActorMarketRole.DataHubAdministrator => MarketRole.DataHubAdministrator, - _ => throw new ArgumentOutOfRangeException(nameof(_userContext.CurrentUser.Actor.MarketRole)), - }; - var chargeOwnerId = marketRole is MarketRole.GridAccessProvider or MarketRole.SystemOperator ? _userContext.CurrentUser.Actor.ActorNumber : null; @@ -142,16 +148,14 @@ await _downloadHandler } } - private bool IsValid(SettlementReportRequestDto req) + private bool IsValid(SettlementReportRequestDto req, MarketRole marketRole) { if (_userContext.CurrentUser.MultiTenancy) { return true; } - var marketRole = _userContext.CurrentUser.Actor.MarketRole; - - if (marketRole == FrontendActorMarketRole.GridAccessProvider) + if (marketRole == MarketRole.GridAccessProvider) { if (!string.IsNullOrWhiteSpace(req.Filter.EnergySupplier)) { @@ -161,12 +165,12 @@ private bool IsValid(SettlementReportRequestDto req) return req.Filter.GridAreas.All(x => _userContext.CurrentUser.Actor.GridAreas.Contains(x.Key)); } - if (marketRole == FrontendActorMarketRole.EnergySupplier) + if (marketRole == MarketRole.EnergySupplier) { return req.Filter.EnergySupplier == _userContext.CurrentUser.Actor.ActorNumber; } - if (marketRole == FrontendActorMarketRole.SystemOperator && + if (marketRole == MarketRole.SystemOperator && req.Filter.CalculationType != CalculationType.BalanceFixing && req.Filter.CalculationType != CalculationType.Aggregation) {