From 061a94552798ee87041c464702f297960aaee1dc Mon Sep 17 00:00:00 2001 From: jvandaal Date: Tue, 30 Jul 2024 14:44:31 +0200 Subject: [PATCH] fix: new address can be null for rejected or retired address because of municipality merger --- paket.dependencies | 20 +++--- paket.lock | 30 ++++---- .../CommandHandlingKafkaProjection.cs | 24 ++++++- .../CommandHandlingKafkaProjectionTests.cs | 70 +++++++++++++++++++ 4 files changed, 117 insertions(+), 27 deletions(-) diff --git a/paket.dependencies b/paket.dependencies index 24255860..81c2f443 100755 --- a/paket.dependencies +++ b/paket.dependencies @@ -79,16 +79,16 @@ nuget Be.Vlaanderen.Basisregisters.Projector 15.0.0 nuget Be.Vlaanderen.Basisregisters.Crab 4.0.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Common 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Notifications 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Contracts 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Edit 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Import 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Legacy 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Provenance 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Provenance.AcmIdm 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Extracts 21.14.0 -nuget Be.Vlaanderen.Basisregisters.GrAr.Oslo 21.14.0 +nuget Be.Vlaanderen.Basisregisters.GrAr.Common 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Notifications 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Contracts 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Edit 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Import 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Legacy 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Provenance 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Provenance.AcmIdm 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Extracts 21.14.1 +nuget Be.Vlaanderen.Basisregisters.GrAr.Oslo 21.14.1 nuget Be.Vlaanderen.Basisregisters.MessageHandling.AwsSqs.Simple 5.0.1 nuget Be.Vlaanderen.Basisregisters.MessageHandling.Kafka.Simple 5.0.1 diff --git a/paket.lock b/paket.lock index e8c03cec..36275fbb 100644 --- a/paket.lock +++ b/paket.lock @@ -253,18 +253,18 @@ NUGET Autofac.Extensions.DependencyInjection (>= 9.0) Be.Vlaanderen.Basisregisters.EventHandling (5.0) Be.Vlaanderen.Basisregisters.Generators.Guid.Deterministic (4.0) - Be.Vlaanderen.Basisregisters.GrAr.Common (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Common (21.14.1) Be.Vlaanderen.Basisregisters.AggregateSource (>= 9.0.1) Be.Vlaanderen.Basisregisters.CommandHandling (>= 9.0.1) NetTopologySuite (>= 2.5) NodaTime (>= 3.1.11) - Be.Vlaanderen.Basisregisters.GrAr.Contracts (21.14) - Be.Vlaanderen.Basisregisters.GrAr.Edit (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Contracts (21.14.1) + Be.Vlaanderen.Basisregisters.GrAr.Edit (21.14.1) NetTopologySuite (>= 2.5) - Be.Vlaanderen.Basisregisters.GrAr.Extracts (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Extracts (21.14.1) Be.Vlaanderen.Basisregisters.Api (>= 21.0) Be.Vlaanderen.Basisregisters.Shaperon (>= 10.0.2) - Be.Vlaanderen.Basisregisters.GrAr.Import (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Import (21.14.1) Autofac (>= 8.0) Be.Vlaanderen.Basisregisters.AggregateSource.SqlStreamStore (>= 9.0.1) Be.Vlaanderen.Basisregisters.CommandHandling (>= 9.0.1) @@ -279,31 +279,31 @@ NUGET Serilog (>= 3.1.1) Serilog.Extensions.Logging (>= 8.0) System.Threading.Tasks.Dataflow (>= 8.0) - Be.Vlaanderen.Basisregisters.GrAr.Legacy (21.14) - Be.Vlaanderen.Basisregisters.GrAr.Common (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Legacy (21.14.1) + Be.Vlaanderen.Basisregisters.GrAr.Common (21.14.1) Be.Vlaanderen.Basisregisters.Utilities.Rfc3339DateTimeOffset (>= 4.0) Newtonsoft.Json (>= 13.0.3) - Be.Vlaanderen.Basisregisters.GrAr.Notifications (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Notifications (21.14.1) AWSSDK.SimpleNotificationService (>= 3.7.301.3) System.Text.Json (>= 8.0.3) - Be.Vlaanderen.Basisregisters.GrAr.Oslo (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Oslo (21.14.1) Be.Vlaanderen.Basisregisters.AspNetCore.Mvc.Formatters.Json (>= 5.0) - Be.Vlaanderen.Basisregisters.GrAr.Common (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Common (21.14.1) Be.Vlaanderen.Basisregisters.Utilities.Rfc3339DateTimeOffset (>= 4.0) Microsoft.Extensions.Configuration (>= 8.0) Microsoft.Extensions.Http.Polly (>= 8.0.3) Newtonsoft.Json (>= 13.0.3) - Be.Vlaanderen.Basisregisters.GrAr.Provenance (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Provenance (21.14.1) Be.Vlaanderen.Basisregisters.CommandHandling (>= 9.0.1) Be.Vlaanderen.Basisregisters.Crab (>= 4.0) - Be.Vlaanderen.Basisregisters.GrAr.Common (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Common (21.14.1) Microsoft.CSharp (>= 4.7) - Be.Vlaanderen.Basisregisters.GrAr.Provenance.AcmIdm (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Provenance.AcmIdm (21.14.1) Be.Vlaanderen.Basisregisters.Auth.AcmIdm (>= 2.0) Be.Vlaanderen.Basisregisters.CommandHandling (>= 9.0.1) Be.Vlaanderen.Basisregisters.Crab (>= 4.0) - Be.Vlaanderen.Basisregisters.GrAr.Common (21.14) - Be.Vlaanderen.Basisregisters.GrAr.Provenance (21.14) + Be.Vlaanderen.Basisregisters.GrAr.Common (21.14.1) + Be.Vlaanderen.Basisregisters.GrAr.Provenance (21.14.1) Microsoft.CSharp (>= 4.7) Be.Vlaanderen.Basisregisters.MessageHandling.AwsSqs.Simple (5.0.1) AWSSDK.Core (>= 3.7.302.15) diff --git a/src/ParcelRegistry.Consumer.Address/Projections/CommandHandlingKafkaProjection.cs b/src/ParcelRegistry.Consumer.Address/Projections/CommandHandlingKafkaProjection.cs index b0bb89cc..22a7f306 100644 --- a/src/ParcelRegistry.Consumer.Address/Projections/CommandHandlingKafkaProjection.cs +++ b/src/ParcelRegistry.Consumer.Address/Projections/CommandHandlingKafkaProjection.cs @@ -109,9 +109,19 @@ await DetachBecauseRejected( foreach (var relation in relations) { + if (!message.NewAddressPersistentLocalId.HasValue) + { + await DetachBecauseRejected( + commandHandler, + new AddressPersistentLocalId(message.AddressPersistentLocalId), + message.Provenance, + ct); + return; + } + var command = new ReplaceParcelAddressBecauseOfMunicipalityMerger( new ParcelId(relation.ParcelId), - new AddressPersistentLocalId(message.NewAddressPersistentLocalId), + new AddressPersistentLocalId(message.NewAddressPersistentLocalId.Value), new AddressPersistentLocalId(message.AddressPersistentLocalId), FromProvenance(message.Provenance)); @@ -175,9 +185,19 @@ await DetachBecauseRetired( foreach (var relation in relations) { + if (!message.NewAddressPersistentLocalId.HasValue) + { + await DetachBecauseRetired( + commandHandler, + new AddressPersistentLocalId(message.AddressPersistentLocalId), + message.Provenance, + ct); + return; + } + var command = new ReplaceParcelAddressBecauseOfMunicipalityMerger( new ParcelId(relation.ParcelId), - new AddressPersistentLocalId(message.NewAddressPersistentLocalId), + new AddressPersistentLocalId(message.NewAddressPersistentLocalId.Value), new AddressPersistentLocalId(message.AddressPersistentLocalId), FromProvenance(message.Provenance)); diff --git a/test/ParcelRegistry.Tests/ProjectionTests/Consumer.Address/CommandHandlingKafkaProjectionTests.cs b/test/ParcelRegistry.Tests/ProjectionTests/Consumer.Address/CommandHandlingKafkaProjectionTests.cs index c5f3de9d..6a6f49c7 100644 --- a/test/ParcelRegistry.Tests/ProjectionTests/Consumer.Address/CommandHandlingKafkaProjectionTests.cs +++ b/test/ParcelRegistry.Tests/ProjectionTests/Consumer.Address/CommandHandlingKafkaProjectionTests.cs @@ -623,6 +623,41 @@ await Then(async _ => }); } + [Fact] + public async Task DetachParcelAddressBecauseOfMunicipalityMerger_AddressWasRejectedBecauseOfMunicipalityMerger() + { + var oldAddressPersistentLocalId = 1; + + var @event = new AddressWasRejectedBecauseOfMunicipalityMerger( + Fixture.Create(), + oldAddressPersistentLocalId, + null, + new Provenance( + Instant.FromDateTimeOffset(DateTimeOffset.Now).ToString(), + Application.ParcelRegistry.ToString(), + Modification.Update.ToString(), + Organisation.Aiv.ToString(), + "test")); + + AddParcelAddressRelations(Fixture.Create(), [oldAddressPersistentLocalId]); + AddParcelAddressRelations(Fixture.Create(), [oldAddressPersistentLocalId]); + + Given(@event); + await Then(async _ => + { + _mockCommandHandler.Verify(x => x.Handle( + It.IsAny(), CancellationToken.None), + Times.Exactly(2)); + + var oldAddressRelations = _fakeBackOfficeContext.ParcelAddressRelations + .Where(x => x.AddressPersistentLocalId == oldAddressPersistentLocalId) + .ToList(); + + oldAddressRelations.Should().BeEmpty(); + await Task.CompletedTask; + }); + } + [Fact] public async Task ReplaceParcelAddressBecauseOfMunicipalityMerger_AddressWasRetiredBecauseOfMunicipalityMerger() { @@ -664,6 +699,41 @@ await Then(async _ => }); } + [Fact] + public async Task DetachParcelAddressBecauseOfMunicipalityMerger_AddressWasRetiredBecauseOfMunicipalityMerger() + { + var oldAddressPersistentLocalId = 1; + + var @event = new AddressWasRetiredBecauseOfMunicipalityMerger( + Fixture.Create(), + oldAddressPersistentLocalId, + null, + new Provenance( + Instant.FromDateTimeOffset(DateTimeOffset.Now).ToString(), + Application.ParcelRegistry.ToString(), + Modification.Update.ToString(), + Organisation.Aiv.ToString(), + "test")); + + AddParcelAddressRelations(Fixture.Create(), [oldAddressPersistentLocalId]); + AddParcelAddressRelations(Fixture.Create(), [oldAddressPersistentLocalId]); + + Given(@event); + await Then(async _ => + { + _mockCommandHandler.Verify(x => x.Handle( + It.IsAny(), CancellationToken.None), + Times.Exactly(2)); + + var oldAddressRelations = _fakeBackOfficeContext.ParcelAddressRelations + .Where(x => x.AddressPersistentLocalId == oldAddressPersistentLocalId) + .ToList(); + + oldAddressRelations.Should().BeEmpty(); + await Task.CompletedTask; + }); + } + private void AddParcelAddressRelations(ParcelId parcelId, int[] addressPersistentLocalIds) { foreach (var addressPersistentLocalId in addressPersistentLocalIds)