Skip to content

Commit

Permalink
refactor: propose all streetnames for municipality merger within a si…
Browse files Browse the repository at this point in the history
…ngle transaction
  • Loading branch information
jvandaal committed Oct 24, 2024
1 parent 1c31388 commit 1371065
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace StreetNameRegistry.Api.BackOffice.Handlers.Lambda.Handlers
using Be.Vlaanderen.Basisregisters.Sqs.Lambda.Infrastructure;
using Be.Vlaanderen.Basisregisters.Sqs.Responses;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Municipality;
using Municipality.Commands;
using Municipality.Exceptions;
Expand All @@ -17,16 +16,14 @@ namespace StreetNameRegistry.Api.BackOffice.Handlers.Lambda.Handlers
public sealed class ProposeStreetNameForMunicipalityMergerHandler : StreetNameLambdaHandler<ProposeStreetNamesForMunicipalityMergerLambdaRequest>
{
private readonly BackOfficeContext _backOfficeContext;
private readonly ILogger _logger;

public ProposeStreetNameForMunicipalityMergerHandler(
IConfiguration configuration,
ICustomRetryPolicy retryPolicy,
ITicketing ticketing,
IScopedIdempotentCommandHandler idempotentCommandHandler,
BackOfficeContext backOfficeContext,
IMunicipalities municipalities,
ILoggerFactory loggerFactory
IMunicipalities municipalities
)
: base(
configuration,
Expand All @@ -36,54 +33,46 @@ ILoggerFactory loggerFactory
idempotentCommandHandler)
{
_backOfficeContext = backOfficeContext;
_logger = loggerFactory.CreateLogger(GetType());
}

protected override async Task<object> InnerHandle(
ProposeStreetNamesForMunicipalityMergerLambdaRequest request,
CancellationToken cancellationToken)
{
var commands = await BuildCommands(request, cancellationToken);
var command = await BuildCommand(request, cancellationToken);

foreach (var command in commands)
try
{
_logger.LogDebug($"Handling {command.GetType().FullName}");

try
{
await IdempotentCommandHandler.Dispatch(
command.CreateCommandId(),
command,
request.Metadata!,
cancellationToken: cancellationToken);
_logger.LogDebug($"Handled {command.GetType().FullName}");
}
catch (IdempotencyException)
{
// Idempotent: Do Nothing return last etag
_logger.LogDebug($"Skipped due to idempotency {command.GetType().FullName}");
}
await IdempotentCommandHandler.Dispatch(
command.CreateCommandId(),
command,
request.Metadata!,
cancellationToken: cancellationToken);
}
catch (IdempotencyException)
{
// Idempotent: do nothing return last etag
}

var etagResponses = new List<ETagResponse>();

foreach (var command in commands)
foreach (var streetName in command.StreetNames)
{
await _backOfficeContext
.AddIdempotentMunicipalityStreetNameIdRelation(
command.PersistentLocalId,
streetName.PersistentLocalId,
request.MunicipalityId(),
request.NisCode,
cancellationToken);

var lastHash = await GetStreetNameHash(request.MunicipalityId(), command.PersistentLocalId, cancellationToken);
etagResponses.Add(new ETagResponse(string.Format(DetailUrlFormat, command.PersistentLocalId), lastHash));
var lastHash = await GetStreetNameHash(request.MunicipalityId(), streetName.PersistentLocalId, cancellationToken);
etagResponses.Add(new ETagResponse(string.Format(DetailUrlFormat, streetName.PersistentLocalId), lastHash));
}

return etagResponses;
}

private async Task<IList<ProposeStreetNameForMunicipalityMerger>> BuildCommands(
private async Task<ProposeStreetNamesForMunicipalityMerger> BuildCommand(
ProposeStreetNamesForMunicipalityMergerLambdaRequest request,
CancellationToken cancellationToken)
{
Expand All @@ -96,7 +85,7 @@ private async Task<IList<ProposeStreetNameForMunicipalityMerger>> BuildCommands(
new MunicipalityStreamId(new MunicipalityId(municipalityId)), cancellationToken));
}

return request.StreetNames
var streetNames = request.StreetNames
.Select(streetName =>
{
var desiredStatus = streetName.MergedStreetNames
Expand All @@ -111,18 +100,21 @@ private async Task<IList<ProposeStreetNameForMunicipalityMerger>> BuildCommands(
? StreetNameStatus.Current
: StreetNameStatus.Proposed;

return new ProposeStreetNameForMunicipalityMerger(
request.MunicipalityId(),
return new ProposeStreetNamesForMunicipalityMerger.StreetNameToPropose(
desiredStatus,
new Names(new[] { new StreetNameName(streetName.StreetName, Language.Dutch) }),
new Names([new StreetNameName(streetName.StreetName, Language.Dutch)]),
streetName.HomonymAddition is not null
? new HomonymAdditions(new[] { new StreetNameHomonymAddition(streetName.HomonymAddition, Language.Dutch) })
: null,
? new HomonymAdditions([new StreetNameHomonymAddition(streetName.HomonymAddition, Language.Dutch)])
: [],
new PersistentLocalId(streetName.NewPersistentLocalId),
streetName.MergedStreetNames.Select(x => new PersistentLocalId(x.StreetNamePersistentLocalId)).ToList(),
request.Provenance);
streetName.MergedStreetNames.Select(x => new PersistentLocalId(x.StreetNamePersistentLocalId)).ToList());
})
.ToList();

return new ProposeStreetNamesForMunicipalityMerger(
request.MunicipalityId(),
streetNames,
request.Provenance);
}

protected override TicketError? InnerMapDomainException(DomainException exception,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace StreetNameRegistry.Api.BackOffice
using Abstractions.Validation;
using Be.Vlaanderen.Basisregisters.Api.Exceptions;
using Be.Vlaanderen.Basisregisters.Auth.AcmIdm;
using Be.Vlaanderen.Basisregisters.GrAr.Edit.Validators;
using Be.Vlaanderen.Basisregisters.GrAr.Provenance;
using Be.Vlaanderen.Basisregisters.Sqs.Exceptions;
using Consumer;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
namespace StreetNameRegistry.Municipality.Commands
{
using System;
using System.Collections.Generic;
using System.Linq;
using Be.Vlaanderen.Basisregisters.Generators.Guid;
using Be.Vlaanderen.Basisregisters.GrAr.Provenance;
using Be.Vlaanderen.Basisregisters.Utilities;

public sealed class ProposeStreetNamesForMunicipalityMerger : IHasCommandProvenance
{
private static readonly Guid Namespace = new Guid("eaf63006-10de-403f-a692-084707cc5ed4");

public MunicipalityId MunicipalityId { get; }
public List<StreetNameToPropose> StreetNames { get; }
public Provenance Provenance { get; }

public ProposeStreetNamesForMunicipalityMerger(
MunicipalityId municipalityId,
List<StreetNameToPropose> streetNames,
Provenance provenance)
{
MunicipalityId = municipalityId;
StreetNames = streetNames;
Provenance = provenance;
}

public Guid CreateCommandId()
=> Deterministic.Create(Namespace, $"ProposeStreetNamesForMunicipalityMerger-{ToString()}");

public override string? ToString()
=> ToStringBuilder.ToString(IdentityFields());

private IEnumerable<object> IdentityFields()
{
yield return MunicipalityId;

foreach (var streetNameName in StreetNames.SelectMany(x => x.IdentityFields()))
{
yield return streetNameName;
}

foreach (var field in Provenance.GetIdentityFields())
{
yield return field;
}
}

public sealed class StreetNameToPropose
{
public StreetNameStatus DesiredStatus { get; }
public Names StreetNameNames { get; }
public HomonymAdditions HomonymAdditions { get; }
public PersistentLocalId PersistentLocalId { get; }
public List<PersistentLocalId> MergedStreetNamePersistentLocalIds { get; }

public StreetNameToPropose(
StreetNameStatus desiredStatus,
Names streetNameNames,
HomonymAdditions homonymAdditions,
PersistentLocalId persistentLocalId,
List<PersistentLocalId> mergedStreetNamePersistentLocalIds)
{
DesiredStatus = desiredStatus;
StreetNameNames = streetNameNames;
HomonymAdditions = homonymAdditions;
PersistentLocalId = persistentLocalId;
MergedStreetNamePersistentLocalIds = mergedStreetNamePersistentLocalIds;
}

internal IEnumerable<object> IdentityFields()
{
yield return PersistentLocalId;
yield return DesiredStatus;

foreach (var streetNameName in StreetNameNames)
{
yield return streetNameName;
}

foreach (var homonymAddition in HomonymAdditions)
{
yield return homonymAddition;
}

foreach (var mergedStreetNamePersistentLocalId in MergedStreetNamePersistentLocalIds)
{
yield return mergedStreetNamePersistentLocalId;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ public StreetNameCommandHandlerModule(
municipality.ProposeStreetName(message.Command.StreetNameNames, message.Command.PersistentLocalId);
});

For<ProposeStreetNameForMunicipalityMerger>()
For<ProposeStreetNamesForMunicipalityMerger>()
.AddSqlStreamStore(getStreamStore, getUnitOfWork, eventMapping, eventSerializer, getSnapshotStore)
.AddEventHash<ProposeStreetNameForMunicipalityMerger, Municipality>(getUnitOfWork)
.AddEventHash<ProposeStreetNamesForMunicipalityMerger, Municipality>(getUnitOfWork)
.AddProvenance(getUnitOfWork, provenanceFactory)
.Handle(async (message, ct) =>
{
var municipality = await getMunicipalities().GetAsync(new MunicipalityStreamId(message.Command.MunicipalityId), ct);
municipality.ProposeStreetNameForMunicipalityMerger(
message.Command.DesiredStatus,
message.Command.StreetNameNames,
message.Command.HomonymAdditions,
message.Command.PersistentLocalId,
message.Command.MergedStreetNamePersistentLocalIds);

foreach (var streetName in message.Command.StreetNames)
{
municipality.ProposeStreetNameForMunicipalityMerger(
streetName.DesiredStatus,
streetName.StreetNameNames,
streetName.HomonymAdditions,
streetName.PersistentLocalId,
streetName.MergedStreetNamePersistentLocalIds);
}
});

For<ApproveStreetName>()
Expand Down
Loading

0 comments on commit 1371065

Please sign in to comment.