diff --git a/src/AddressRegistry.Api.BackOffice.Abstractions/BackOfficeContext.cs b/src/AddressRegistry.Api.BackOffice.Abstractions/BackOfficeContext.cs index 81102390f..eeab3338c 100644 --- a/src/AddressRegistry.Api.BackOffice.Abstractions/BackOfficeContext.cs +++ b/src/AddressRegistry.Api.BackOffice.Abstractions/BackOfficeContext.cs @@ -71,6 +71,19 @@ public async Task AddIdempotentAddres return relation; } + public async Task RemoveIdempotentMunicipalityMergerAddress(int newAddressPersistentLocalId, CancellationToken cancellationToken) + { + var relation = await MunicipalityMergerAddresses.SingleOrDefaultAsync( + x => x.NewAddressPersistentLocalId == newAddressPersistentLocalId, cancellationToken: cancellationToken); + if (relation is null) + { + return; + } + + MunicipalityMergerAddresses.Remove(relation); + await SaveChangesAsync(cancellationToken); + } + public async Task AddIdempotentMunicipalityMergerAddress( int oldAddressPersistentLocalId, int newStreetNamePersistentLocalId, diff --git a/src/AddressRegistry.Projections.BackOffice/BackOfficeProjections.cs b/src/AddressRegistry.Projections.BackOffice/BackOfficeProjections.cs index a280791c7..90205d4a5 100644 --- a/src/AddressRegistry.Projections.BackOffice/BackOfficeProjections.cs +++ b/src/AddressRegistry.Projections.BackOffice/BackOfficeProjections.cs @@ -25,6 +25,14 @@ await backOfficeContext.AddIdempotentAddressStreetNameIdRelation( message.Message.AddressPersistentLocalId, message.Message.StreetNamePersistentLocalId, cancellationToken); }); + When>(async (_, message, cancellationToken) => + { + await DelayProjection(message.CreatedUtc, delayInSeconds, cancellationToken); + + await using var backOfficeContext = await backOfficeContextFactory.CreateDbContextAsync(cancellationToken); + await backOfficeContext.RemoveIdempotentMunicipalityMergerAddress(message.Message.AddressPersistentLocalId, cancellationToken); + }); + When>(async (_, message, cancellationToken) => { await DelayProjection(message.CreatedUtc, delayInSeconds, cancellationToken); diff --git a/src/AddressRegistry.Projector/Consumers/ConsumerController.cs b/src/AddressRegistry.Projector/Consumers/ConsumerController.cs index e9bc9f425..d65237e4c 100644 --- a/src/AddressRegistry.Projector/Consumers/ConsumerController.cs +++ b/src/AddressRegistry.Projector/Consumers/ConsumerController.cs @@ -46,7 +46,6 @@ public async Task Get( sqlConsumerReadStreetNameLatestItemConnection.QueryFirstAsync( $"SELECT TOP(1) [{nameof(StreetNameLatestItem.VersionTimestamp)}] FROM [{Schema.ConsumerReadStreetName}].[{StreetNameLatestItemConfiguration.TableName}] ORDER BY [{nameof(StreetNameLatestItem.VersionTimestamp)}] DESC"); - await Task.WhenAll(consumerResult, municipalityLatestItemResult, streetNameLatestItemResult); return Ok(new object[] diff --git a/test/AddressRegistry.Tests/ProjectionTests/BackOffice/BackOfficeProjectionsTests.cs b/test/AddressRegistry.Tests/ProjectionTests/BackOffice/BackOfficeProjectionsTests.cs index 6c1ff1120..ffd14194e 100644 --- a/test/AddressRegistry.Tests/ProjectionTests/BackOffice/BackOfficeProjectionsTests.cs +++ b/test/AddressRegistry.Tests/ProjectionTests/BackOffice/BackOfficeProjectionsTests.cs @@ -86,6 +86,39 @@ await Sut }); } + [Fact] + public async Task GivenAddressWasRemovedV2_ThenRelationIsRemoved() + { + var addressWasRemovedV2 = _fixture.Create(); + + var oldAddressPersistentLocalId = _fixture.Create(); + await _fakeBackOfficeContext.AddIdempotentAddressStreetNameIdRelation( + oldAddressPersistentLocalId, + _fixture.Create(), + CancellationToken.None); + + await _fakeBackOfficeContext.AddIdempotentMunicipalityMergerAddress( + oldAddressPersistentLocalId, + addressWasRemovedV2.StreetNamePersistentLocalId, + addressWasRemovedV2.AddressPersistentLocalId, + CancellationToken.None); + + await Sut + .Given(new Envelope(new Envelope(addressWasRemovedV2, new Dictionary + { + { Envelope.CreatedUtcMetadataKey, DateTime.UtcNow } + }))) + .Then(async _ => + { + await Task.Delay(TimeSpan.FromSeconds(DelayInSeconds + 1)); + + var result = await _fakeBackOfficeContext.MunicipalityMergerAddresses + .FindAsync(oldAddressPersistentLocalId); + + result.Should().BeNull(); + }); + } + [Fact] public async Task GivenAddressWasProposedForMunicipalityMerger_ThenRelationIsAdded() { diff --git a/test/AddressRegistry.Tests/ProjectionsHandlesEventTests.cs b/test/AddressRegistry.Tests/ProjectionsHandlesEventTests.cs index 8dd6c7c43..0b5535d7d 100644 --- a/test/AddressRegistry.Tests/ProjectionsHandlesEventTests.cs +++ b/test/AddressRegistry.Tests/ProjectionsHandlesEventTests.cs @@ -201,7 +201,6 @@ public void BackOfficeProjectionHandlesEvents() typeof(AddressWasRejectedBecauseStreetNameWasRetired), typeof(AddressWasRemovedBecauseHouseNumberWasRemoved), typeof(AddressWasRemovedBecauseStreetNameWasRemoved), - typeof(AddressWasRemovedV2), typeof(AddressWasRetiredBecauseHouseNumberWasRetired), typeof(AddressWasRetiredBecauseOfMunicipalityMerger), typeof(AddressWasRetiredBecauseOfReaddress),