Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace parcel address because of municipality merger #796

Merged
merged 8 commits into from
Jul 24, 2024
Prev Previous commit
Next Next commit
feat: consume AddressWasRejectedBecauseOfMunicipalityMerger and Addre…
…ssWasRetiredBecauseOfMunicipalityMerger and send domain commands
jvandaal committed Jul 23, 2024
commit 331f494186a1e5f09dd0e43ad17980d47924d5bf
Original file line number Diff line number Diff line change
@@ -99,6 +99,36 @@ await DetachBecauseRejected(
ct);
});

When<AddressWasRejectedBecauseOfMunicipalityMerger>(async (commandHandler, message, ct) =>
{
await using var backOfficeContext = await _backOfficeContextFactory.CreateDbContextAsync(ct);
var relations = backOfficeContext.ParcelAddressRelations
.AsNoTracking()
.Where(x => x.AddressPersistentLocalId == new AddressPersistentLocalId(message.AddressPersistentLocalId))
.ToList();

foreach (var relation in relations)
{
var command = new ReplaceParcelAddressBecauseOfMunicipalityMerger(
new ParcelId(relation.ParcelId),
new AddressPersistentLocalId(message.NewAddressPersistentLocalId),
new AddressPersistentLocalId(message.AddressPersistentLocalId),
FromProvenance(message.Provenance));

await commandHandler.Handle(command, ct);

await backOfficeContext.RemoveIdempotentParcelAddressRelation(
command.ParcelId,
command.PreviousAddressPersistentLocalId,
ct);

await backOfficeContext.AddIdempotentParcelAddressRelation(
command.ParcelId,
command.NewAddressPersistentLocalId,
ct);
}
});

When<AddressWasRetiredV2>(async (commandHandler, message, ct) =>
{
await DetachBecauseRetired(
@@ -135,6 +165,36 @@ await DetachBecauseRetired(
ct);
});

When<AddressWasRetiredBecauseOfMunicipalityMerger>(async (commandHandler, message, ct) =>
{
await using var backOfficeContext = await _backOfficeContextFactory.CreateDbContextAsync(ct);
var relations = backOfficeContext.ParcelAddressRelations
.AsNoTracking()
.Where(x => x.AddressPersistentLocalId == new AddressPersistentLocalId(message.AddressPersistentLocalId))
.ToList();

foreach (var relation in relations)
{
var command = new ReplaceParcelAddressBecauseOfMunicipalityMerger(
new ParcelId(relation.ParcelId),
new AddressPersistentLocalId(message.NewAddressPersistentLocalId),
new AddressPersistentLocalId(message.AddressPersistentLocalId),
FromProvenance(message.Provenance));

await commandHandler.Handle(command, ct);

await backOfficeContext.RemoveIdempotentParcelAddressRelation(
command.ParcelId,
command.PreviousAddressPersistentLocalId,
ct);

await backOfficeContext.AddIdempotentParcelAddressRelation(
command.ParcelId,
command.NewAddressPersistentLocalId,
ct);
}
});

When<AddressWasRemovedV2>(async (commandHandler, message, ct) =>
{
await DetachBecauseRemoved(
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
namespace ParcelRegistry.Tests.ProjectionTests.Consumer.Address
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Api.BackOffice.Abstractions;
using Autofac;
using AutoFixture;
using Be.Vlaanderen.Basisregisters.AggregateSource.Snapshotting;
using Be.Vlaanderen.Basisregisters.GrAr.Contracts.AddressRegistry;
using Be.Vlaanderen.Basisregisters.GrAr.Provenance;
using EventExtensions;
using Fixtures;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
@@ -20,7 +16,6 @@ namespace ParcelRegistry.Tests.ProjectionTests.Consumer.Address
using NodaTime;
using Parcel;
using Parcel.Commands;
using Parcel.Events;
using ParcelRegistry.Consumer.Address;
using ParcelRegistry.Consumer.Address.Projections;
using Tests.BackOffice;
@@ -587,6 +582,88 @@ await Then(async _ =>
});
}

[Fact]
public async Task ReplaceParcelAddressBecauseOfMunicipalityMerger_AddressWasRejectedBecauseOfMunicipalityMerger()
{
var oldAddressPersistentLocalId = 1;
var newAddressPersistentLocalId = 2;

var @event = new AddressWasRejectedBecauseOfMunicipalityMerger(
Fixture.Create<int>(),
oldAddressPersistentLocalId,
newAddressPersistentLocalId,
new Provenance(
Instant.FromDateTimeOffset(DateTimeOffset.Now).ToString(),
Application.ParcelRegistry.ToString(),
Modification.Update.ToString(),
Organisation.Aiv.ToString(),
"test"));

AddParcelAddressRelations(Fixture.Create<ParcelId>(), [oldAddressPersistentLocalId]);
AddParcelAddressRelations(Fixture.Create<ParcelId>(), [oldAddressPersistentLocalId]);

Given(@event);
await Then(async _ =>
{
_mockCommandHandler.Verify(x => x.Handle(
It.IsAny<ReplaceParcelAddressBecauseOfMunicipalityMerger>(), CancellationToken.None),
Times.Exactly(2));

var oldAddressRelations = _fakeBackOfficeContext.ParcelAddressRelations
.Where(x => x.AddressPersistentLocalId == oldAddressPersistentLocalId)
.ToList();

var newAddressRelations = _fakeBackOfficeContext.ParcelAddressRelations
.Where(x => x.AddressPersistentLocalId == newAddressPersistentLocalId)
.ToList();

oldAddressRelations.Should().BeEmpty();
newAddressRelations.Should().HaveCount(2);
await Task.CompletedTask;
});
}

[Fact]
public async Task ReplaceParcelAddressBecauseOfMunicipalityMerger_AddressWasRetiredBecauseOfMunicipalityMerger()
{
var oldAddressPersistentLocalId = 1;
var newAddressPersistentLocalId = 2;

var @event = new AddressWasRetiredBecauseOfMunicipalityMerger(
Fixture.Create<int>(),
oldAddressPersistentLocalId,
newAddressPersistentLocalId,
new Provenance(
Instant.FromDateTimeOffset(DateTimeOffset.Now).ToString(),
Application.ParcelRegistry.ToString(),
Modification.Update.ToString(),
Organisation.Aiv.ToString(),
"test"));

AddParcelAddressRelations(Fixture.Create<ParcelId>(), [oldAddressPersistentLocalId]);
AddParcelAddressRelations(Fixture.Create<ParcelId>(), [oldAddressPersistentLocalId]);

Given(@event);
await Then(async _ =>
{
_mockCommandHandler.Verify(x => x.Handle(
It.IsAny<ReplaceParcelAddressBecauseOfMunicipalityMerger>(), CancellationToken.None),
Times.Exactly(2));

var oldAddressRelations = _fakeBackOfficeContext.ParcelAddressRelations
.Where(x => x.AddressPersistentLocalId == oldAddressPersistentLocalId)
.ToList();

var newAddressRelations = _fakeBackOfficeContext.ParcelAddressRelations
.Where(x => x.AddressPersistentLocalId == newAddressPersistentLocalId)
.ToList();

oldAddressRelations.Should().BeEmpty();
newAddressRelations.Should().HaveCount(2);
await Task.CompletedTask;
});
}

private void AddParcelAddressRelations(ParcelId parcelId, int[] addressPersistentLocalIds)
{
foreach (var addressPersistentLocalId in addressPersistentLocalIds)

Unchanged files with check annotations Beta

{
private static readonly Guid Namespace = new Guid("2e07d81c-ebb9-43ed-96bc-d13911bc30c9");
public Legacy.ParcelId OldParcelId { get; }

Check warning on line 14 in src/ParcelRegistry/Parcel/Commands/MigrateParcel.cs

GitHub Actions / Build

'ParcelId' is obsolete: 'This is a legacy valueobject and should not be used anymore.'
public ParcelId NewParcelId { get; }
public VbrCaPaKey CaPaKey { get; }
public ParcelStatus ParcelStatus { get; }
public Provenance Provenance { get; }
public MigrateParcel(
Legacy.ParcelId parcelId,

Check warning on line 24 in src/ParcelRegistry/Parcel/Commands/MigrateParcel.cs

GitHub Actions / Build

'ParcelId' is obsolete: 'This is a legacy valueobject and should not be used anymore.'
VbrCaPaKey caPaKey,
Legacy.ParcelStatus parcelStatus,

Check warning on line 26 in src/ParcelRegistry/Parcel/Commands/MigrateParcel.cs

GitHub Actions / Build

'ParcelStatus' is obsolete: 'This is a legacy valueobject and should not be used anymore.'
bool isRemoved,
IEnumerable<AddressPersistentLocalId> addressPersistentLocalIds,
ExtendedWkbGeometry extendedWkbGeometry,
public MigrateParcel CreateMigrateCommand(
IEnumerable<AddressPersistentLocalId> addressPersistentLocalIds,
ExtendedWkbGeometry extendedWkbGeometry,
ParcelStatus? parcelStatus = null)

Check warning on line 20 in src/ParcelRegistry/Legacy/Parcel.cs

GitHub Actions / Build

'ParcelStatus' is obsolete: 'This is a legacy valueobject and should not be used anymore.'
{
return new MigrateParcel(
ParcelId,
Organisation.DigitaalVlaanderen));
}
public static Parcel Register(ParcelId id, VbrCaPaKey vbrCaPaKey, IParcelFactory factory)

Check warning on line 38 in src/ParcelRegistry/Legacy/Parcel.cs

GitHub Actions / Build

'ParcelId' is obsolete: 'This is a legacy valueobject and should not be used anymore.'

Check warning on line 38 in src/ParcelRegistry/Legacy/Parcel.cs

GitHub Actions / Build

'IParcelFactory' is obsolete: 'This is a legacy interface and should not be used anymore.'
{
var parcel = factory.Create();
parcel.ApplyChange(new ParcelWasRegistered(id, vbrCaPaKey));
public void ImportSubaddressFromCrab(
CrabSubaddressId subaddressId,
CrabHouseNumberId houseNumberId,
BoxNumber boxNumber,

Check warning on line 168 in src/ParcelRegistry/Legacy/Parcel.cs

GitHub Actions / Build

'BoxNumber' is obsolete: 'This is a legacy valueobject and should not be used anymore.'
CrabBoxNumberType boxNumberType,
CrabLifetime lifetime,
CrabTimestamp timestamp,
/// Fixes state of aggregate because of possible invalid snapshot.
/// </summary>
public void FixGrar3581(
ParcelStatus parcelStatus,

Check warning on line 249 in src/ParcelRegistry/Legacy/Parcel.cs

GitHub Actions / Build

'ParcelStatus' is obsolete: 'This is a legacy valueobject and should not be used anymore.'
IList<AddressId> addressIds)

Check warning on line 250 in src/ParcelRegistry/Legacy/Parcel.cs

GitHub Actions / Build

'AddressId' is obsolete: 'This is a legacy valueobject and should not be used anymore.'
{
if (IsRemoved)
{
public partial class Parcel
{
public ParcelId ParcelId { get; private set; }

Check warning on line 12 in src/ParcelRegistry/Legacy/ParcelState.cs

GitHub Actions / Build

'ParcelId' is obsolete: 'This is a legacy valueobject and should not be used anymore.'
public VbrCaPaKey CaPaKey { get; private set; }
public bool IsRemoved { get; private set; }