Skip to content

Commit

Permalink
feat: ParcelAddressesWereReaddressed
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandaal committed May 15, 2024
1 parent 411e7c5 commit 78b52bf
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ await DetachBecauseRemoved(
.Where(x => sourceAddressPersistentLocalIds.Contains(x.AddressPersistentLocalId))
.ToListAsync(cancellationToken: ct);

var commands = parcelAddressRelations
var commandByParcels = parcelAddressRelations
.GroupBy(
relation => relation.ParcelId,
relation => readdresses.Where(x => x.SourceAddressPersistentLocalId == relation.AddressPersistentLocalId))
Expand All @@ -193,7 +193,7 @@ await DetachBecauseRemoved(
x.SelectMany(a => a),
FromProvenance(message.Provenance)));

foreach (var command in commands)
foreach (var command in commandByParcels)
{
await commandHandler.Handle(command, ct);
}
Expand Down Expand Up @@ -230,7 +230,6 @@ await DetachBecauseRejected(
ct);
});


When<AddressWasRetiredBecauseOfReaddress>(async (commandHandler, message, ct) =>
{
await DetachBecauseRetired(
Expand All @@ -240,7 +239,7 @@ await DetachBecauseRetired(
ct);
});
}

private async Task DetachBecauseRemoved(
CommandHandler commandHandler,
AddressPersistentLocalId addressPersistentLocalId,
Expand Down
2 changes: 1 addition & 1 deletion src/ParcelRegistry/Parcel/AddressCommandHandlerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public AddressCommandHandlerModule(
var streamId = new ParcelStreamId(message.Command.ParcelId);
var parcel = await parcelRepository().GetAsync(streamId, ct);

parcel.ReplaceAttachedAddressesBecauseAddressesWereReaddressed(message.Command.Readdresses);
parcel.ReaddressAddresses(message.Command.Readdresses);
});
}
}
Expand Down

This file was deleted.

This file was deleted.

102 changes: 102 additions & 0 deletions src/ParcelRegistry/Parcel/Events/ParcelAddressesWereReaddressed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
namespace ParcelRegistry.Parcel.Events
{
using System;
using System.Collections.Generic;
using System.Linq;
using Be.Vlaanderen.Basisregisters.EventHandling;
using Be.Vlaanderen.Basisregisters.GrAr.Common;
using Be.Vlaanderen.Basisregisters.GrAr.Provenance;
using Commands;
using Newtonsoft.Json;

[EventTags(EventTag.For.Sync, EventTag.For.Edit, Tag.Address)]
[EventName(EventName)]
[EventDescription("De adresherkoppelingen op het perceel door heradressering.")]
public sealed class ParcelAddressesWereReaddressed : IParcelEvent
{
public const string EventName = "ParcelAddressesWereReaddressed"; // BE CAREFUL CHANGING THIS!!

[EventPropertyDescription("Interne GUID van het perceel.")]
public Guid ParcelId { get; }

[EventPropertyDescription("CaPaKey (= objectidentificator) van het perceel, waarbij forward slashes vervangen zijn door koppeltekens i.f.v. gebruik in URI's.")]
public string CaPaKey { get; }

[EventPropertyDescription("Objectidentificatoren van nieuw gekoppelde adressen.")]
public IEnumerable<int> AttachedAddressPersistentLocalIds { get; }

[EventPropertyDescription("Objectidentificatoren van ontkoppelde adressen.")]
public IEnumerable<int> DetachedAddressPersistentLocalIds { get; }

[EventPropertyDescription("De geheradresseerde adressen uit het Adressenregister.")]
public IEnumerable<AddressRegistryReaddress> AddressRegistryReaddresses { get; }

[EventPropertyDescription("Metadata bij het event.")]
public ProvenanceData Provenance { get; private set; }

public ParcelAddressesWereReaddressed(
ParcelId parcelId,
VbrCaPaKey vbrCaPaKey,
IEnumerable<AddressPersistentLocalId> attachedAddressPersistentLocalIds,
IEnumerable<AddressPersistentLocalId> detachedAddressPersistentLocalIds,
IEnumerable<AddressRegistryReaddress> addressRegistryReaddresses)
{
ParcelId = parcelId;
CaPaKey = vbrCaPaKey;
AttachedAddressPersistentLocalIds = attachedAddressPersistentLocalIds.Select(x => (int)x).ToList();
DetachedAddressPersistentLocalIds = detachedAddressPersistentLocalIds.Select(x => (int)x).ToList();
AddressRegistryReaddresses = addressRegistryReaddresses;
}

[JsonConstructor]
public ParcelAddressesWereReaddressed(
Guid parcelId,
string caPaKey,
IEnumerable<int> attachedAddressPersistentLocalIds,
IEnumerable<int> detachedAddressPersistentLocalIds,
IEnumerable<AddressRegistryReaddress> addressRegistryReaddresses,
ProvenanceData provenance)
: this(
new ParcelId(parcelId),
new VbrCaPaKey(caPaKey),
attachedAddressPersistentLocalIds.Select(x => new AddressPersistentLocalId(x)),
detachedAddressPersistentLocalIds.Select(x => new AddressPersistentLocalId(x)),
addressRegistryReaddresses)
=> ((ISetProvenance)this).SetProvenance(provenance.ToProvenance());

void ISetProvenance.SetProvenance(Provenance provenance) => Provenance = new ProvenanceData(provenance);

public IEnumerable<string> GetHashFields()
{
var fields = Provenance.GetHashFields().ToList();
fields.Add(ParcelId.ToString("D"));
fields.Add(CaPaKey);
fields.AddRange(AttachedAddressPersistentLocalIds.Select(addressPersistentLocalId => addressPersistentLocalId.ToString()));
fields.AddRange(DetachedAddressPersistentLocalIds.Select(addressPersistentLocalId => addressPersistentLocalId.ToString()));

return fields;
}

public string GetHash() => this.ToEventHash(EventName);
}

public class AddressRegistryReaddress
{
public int SourceAddressPersistentLocalId { get; }
public int DestinationAddressPersistentLocalId { get; }

public AddressRegistryReaddress(
ReaddressData readdressData)
: this((int)readdressData.SourceAddressPersistentLocalId, readdressData.DestinationAddressPersistentLocalId)
{ }

[JsonConstructor]
private AddressRegistryReaddress(
int sourceAddressPersistentLocalId,
int destinationAddressPersistentLocalId)
{
SourceAddressPersistentLocalId = sourceAddressPersistentLocalId;
DestinationAddressPersistentLocalId = destinationAddressPersistentLocalId;
}
}
}
26 changes: 11 additions & 15 deletions src/ParcelRegistry/Parcel/Parcel_Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,32 @@ public void DetachAddressBecauseAddressWasRetired(AddressPersistentLocalId addre
// previousAddressPersistentLocalId));
//}

public void ReplaceAttachedAddressesBecauseAddressesWereReaddressed(
public void ReaddressAddresses(
IReadOnlyList<ReaddressData> readdresses)
{
var addressPersistentLocalIdsToAttach = readdresses
.Select(x => x.DestinationAddressPersistentLocalId)
.Except(readdresses.Select(x => x.SourceAddressPersistentLocalId))
.Except(AddressPersistentLocalIds)
.ToList();

var addressPersistentLocalIdsToDetach = readdresses
.Select(x => x.SourceAddressPersistentLocalId)
.Except(readdresses.Select(x => x.DestinationAddressPersistentLocalId))
.Where(AddressPersistentLocalIds.Contains)
.ToList();
// replace event: de readdresses waarvan hun Source of DestinationAddressId in geen ander readdress wordt gebruikt?

foreach (var addressPersistentLocalId in addressPersistentLocalIdsToDetach)
{
ApplyChange(new ParcelAddressWasDetachedBecauseAddressWasReaddressed(
ParcelId,
CaPaKey,
addressPersistentLocalId));
}

foreach (var addressPersistentLocalId in addressPersistentLocalIdsToAttach)
if (!addressPersistentLocalIdsToAttach.Any() && !addressPersistentLocalIdsToDetach.Any())
{
ApplyChange(new ParcelAddressWasAttachedBecauseAddressWasReaddressed(
ParcelId,
CaPaKey,
addressPersistentLocalId));
return;
}

ApplyChange(new ParcelAddressesWereReaddressed(
ParcelId,
CaPaKey,
addressPersistentLocalIdsToAttach,
addressPersistentLocalIdsToDetach,
readdresses.Select(x => new AddressRegistryReaddress(x))));
}
}
}
16 changes: 16 additions & 0 deletions src/ParcelRegistry/Parcel/Parcel_State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private Parcel()
Register<ParcelAddressWasDetachedBecauseAddressWasRejected>(When);
Register<ParcelAddressWasDetachedBecauseAddressWasRetired>(When);
Register<ParcelAddressWasReplacedBecauseAddressWasReaddressed>(When);
Register<ParcelAddressesWereReaddressed>(When);

Register<ParcelSnapshotV2>(When);
}
Expand Down Expand Up @@ -146,6 +147,21 @@ private void When(ParcelAddressWasReplacedBecauseAddressWasReaddressed @event)
_lastEvent = @event;
}

private void When(ParcelAddressesWereReaddressed @event)
{
foreach (var addressPersistentLocalId in @event.DetachedAddressPersistentLocalIds)
{
_addressPersistentLocalIds.Remove(new AddressPersistentLocalId(addressPersistentLocalId));
}

foreach (var addressPersistentLocalId in @event.AttachedAddressPersistentLocalIds)
{
_addressPersistentLocalIds.Add(new AddressPersistentLocalId(addressPersistentLocalId));
}

_lastEvent = @event;
}

private void When(ParcelSnapshotV2 @event)
{
ParcelId = new ParcelId(@event.ParcelId);
Expand Down
Loading

0 comments on commit 78b52bf

Please sign in to comment.