-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1038 from Energinet-DataHub/xvibe/move-grid-areas…
…-to-actor Consolidate Actor service
- Loading branch information
Showing
16 changed files
with
678 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...ant/Energinet.DataHub.MarketParticipant.Application/Services/ActorConsolidationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Energinet.DataHub.MarketParticipant.Domain.Exception; | ||
using Energinet.DataHub.MarketParticipant.Domain.Model; | ||
using Energinet.DataHub.MarketParticipant.Domain.Repositories; | ||
using NodaTime; | ||
|
||
namespace Energinet.DataHub.MarketParticipant.Application.Services; | ||
|
||
public sealed class ActorConsolidationService : IActorConsolidationService | ||
{ | ||
private readonly IActorConsolidationAuditLogRepository _actorConsolidationAuditLogRepository; | ||
private readonly IActorCredentialsRemovalService _actorCredentialsRemovalService; | ||
private readonly IActorRepository _actorRepository; | ||
private readonly IAuditIdentityProvider _auditIdentityProvider; | ||
private readonly IDomainEventRepository _domainEventRepository; | ||
private readonly IGridAreaRepository _gridAreaRepository; | ||
|
||
public ActorConsolidationService( | ||
IActorConsolidationAuditLogRepository actorConsolidationAuditLogRepository, | ||
IActorCredentialsRemovalService actorCredentialsRemovalService, | ||
IActorRepository actorRepository, | ||
IAuditIdentityProvider auditIdentityProvider, | ||
IDomainEventRepository domainEventRepository, | ||
IGridAreaRepository gridAreaRepository) | ||
{ | ||
_actorConsolidationAuditLogRepository = actorConsolidationAuditLogRepository; | ||
_actorCredentialsRemovalService = actorCredentialsRemovalService; | ||
_actorRepository = actorRepository; | ||
_auditIdentityProvider = auditIdentityProvider; | ||
_domainEventRepository = domainEventRepository; | ||
_gridAreaRepository = gridAreaRepository; | ||
} | ||
|
||
public async Task ConsolidateAsync(ActorConsolidation actorConsolidation) | ||
{ | ||
ArgumentNullException.ThrowIfNull(actorConsolidation); | ||
|
||
var fromActor = await _actorRepository.GetAsync(actorConsolidation.ActorFromId).ConfigureAwait(false); | ||
NotFoundValidationException.ThrowIfNull(fromActor, actorConsolidation.ActorFromId.Value); | ||
|
||
var toActor = await _actorRepository.GetAsync(actorConsolidation.ActorToId).ConfigureAwait(false); | ||
NotFoundValidationException.ThrowIfNull(toActor, actorConsolidation.ActorToId.Value); | ||
|
||
if (fromActor.MarketRole.Function is EicFunction.GridAccessProvider | ||
&& toActor.MarketRole.Function is EicFunction.GridAccessProvider) | ||
{ | ||
var actorGridAreasToTransfer = fromActor.MarketRole.GridAreas.ToList(); | ||
|
||
toActor.TransferGridAreasFrom(fromActor); | ||
|
||
await UpdateGridAreasValidToDateAsync(actorGridAreasToTransfer, actorConsolidation.ConsolidateAt).ConfigureAwait(false); | ||
await AuditLogConsolidationCompletedAsync(actorGridAreasToTransfer, actorConsolidation).ConfigureAwait(false); | ||
} | ||
|
||
await DeactivateActorAsync(fromActor).ConfigureAwait(false); | ||
|
||
await _actorRepository | ||
.AddOrUpdateAsync(fromActor) | ||
.ConfigureAwait(false); | ||
|
||
await _actorRepository | ||
.AddOrUpdateAsync(toActor) | ||
.ConfigureAwait(false); | ||
|
||
await _domainEventRepository | ||
.EnqueueAsync(fromActor) | ||
.ConfigureAwait(false); | ||
|
||
await _domainEventRepository | ||
.EnqueueAsync(toActor) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
private async Task AuditLogConsolidationCompletedAsync( | ||
IEnumerable<ActorGridArea> actorGridAreas, | ||
ActorConsolidation actorConsolidation) | ||
{ | ||
foreach (var actorGridArea in actorGridAreas) | ||
{ | ||
await _actorConsolidationAuditLogRepository.AuditAsync( | ||
_auditIdentityProvider.IdentityId, | ||
GridAreaAuditedChange.ConsolidationCompleted, | ||
actorConsolidation, | ||
actorGridArea.Id).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
private async Task DeactivateActorAsync(Actor actor) | ||
{ | ||
await _actorCredentialsRemovalService.RemoveActorCredentialsAsync(actor).ConfigureAwait(false); | ||
actor.Deactivate(); | ||
} | ||
|
||
private async Task UpdateGridAreasValidToDateAsync(ICollection<ActorGridArea> actorGridAreasToTransfer, Instant scheduledAt) | ||
{ | ||
var allGridAreas = await _gridAreaRepository.GetAsync().ConfigureAwait(false); | ||
var gridAreasToUpdate = allGridAreas.Where(ga => actorGridAreasToTransfer.Select(aga => aga.Id).Contains(ga.Id)); | ||
foreach (var gridArea in gridAreasToUpdate) | ||
{ | ||
gridArea.ValidTo = scheduledAt.ToDateTimeOffset(); | ||
await _gridAreaRepository | ||
.AddOrUpdateAsync(gridArea) | ||
.ConfigureAwait(false); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...nerginet.DataHub.MarketParticipant.Application/Services/ActorCredentialsRemovalService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Energinet.DataHub.MarketParticipant.Domain.Model; | ||
using Energinet.DataHub.MarketParticipant.Domain.Services; | ||
|
||
namespace Energinet.DataHub.MarketParticipant.Application.Services; | ||
|
||
public sealed class ActorCredentialsRemovalService : IActorCredentialsRemovalService | ||
{ | ||
private readonly ICertificateService _certificateService; | ||
private readonly IActorClientSecretService _actorClientSecretService; | ||
|
||
public ActorCredentialsRemovalService(ICertificateService certificateService, IActorClientSecretService actorClientSecretService) | ||
{ | ||
_certificateService = certificateService; | ||
_actorClientSecretService = actorClientSecretService; | ||
} | ||
|
||
public async Task RemoveActorCredentialsAsync(Actor actor) | ||
{ | ||
ArgumentNullException.ThrowIfNull(actor); | ||
if (actor.Credentials is null) | ||
return; | ||
|
||
switch (actor.Credentials) | ||
{ | ||
case ActorCertificateCredentials certificateCredentials: | ||
await _certificateService.RemoveCertificateAsync(certificateCredentials.KeyVaultSecretIdentifier).ConfigureAwait(false); | ||
break; | ||
case ActorClientSecretCredentials when actor.ExternalActorId is not null: | ||
await _actorClientSecretService.RemoveSecretAsync(actor).ConfigureAwait(false); | ||
break; | ||
default: | ||
throw new InvalidOperationException($"Actor with id {actor.Id} does not have a known type of credentials assigned"); | ||
} | ||
|
||
actor.Credentials = null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...nt/Energinet.DataHub.MarketParticipant.Application/Services/IActorConsolidationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Threading.Tasks; | ||
using Energinet.DataHub.MarketParticipant.Domain.Model; | ||
|
||
namespace Energinet.DataHub.MarketParticipant.Application.Services; | ||
|
||
public interface IActorConsolidationService | ||
{ | ||
Task ConsolidateAsync(ActorConsolidation actorConsolidation); | ||
} |
23 changes: 23 additions & 0 deletions
23
...erginet.DataHub.MarketParticipant.Application/Services/IActorCredentialsRemovalService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Threading.Tasks; | ||
using Energinet.DataHub.MarketParticipant.Domain.Model; | ||
|
||
namespace Energinet.DataHub.MarketParticipant.Application.Services; | ||
|
||
public interface IActorCredentialsRemovalService | ||
{ | ||
Task RemoveActorCredentialsAsync(Actor actor); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.