Skip to content

Commit

Permalink
fix: readdress addresses box number bug
Browse files Browse the repository at this point in the history
  • Loading branch information
emalfroy authored and jvandaal committed Dec 20, 2023
1 parent 379ca96 commit fcec945
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 40 deletions.
63 changes: 36 additions & 27 deletions src/AddressRegistry/StreetName/StreetNameReaddresser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace AddressRegistry.StreetName
using System;
using System.Collections.Generic;
using System.Linq;
using Address;
using Commands;
using DataStructures;
using Exceptions;
Expand All @@ -15,6 +14,10 @@ public sealed class StreetNameReaddresser
private readonly IEnumerable<ReaddressAddressItem> _addressesToReaddress;
private readonly StreetName _streetName;


private readonly IDictionary<StreetNameAddress, List<StreetNameAddress>> _sourceAddressesWithBoxNumbers =
new Dictionary<StreetNameAddress, List<StreetNameAddress>>();

public IEnumerable<ReaddressedAddressData> ReaddressedHouseNumbers
=> ReaddressedAddresses.Select(x => x.Value.readdressedHouseNumber);

Expand All @@ -25,9 +28,7 @@ public IDictionary<
AddressPersistentLocalId,
(
ReaddressedAddressData readdressedHouseNumber,
List<ReaddressedAddressData> readdressedBoxNumbers,
List<AddressPersistentLocalId> rejectedBoxNumberPersistentLocalIds,
List<AddressPersistentLocalId> retiredBoxNumberPersistentLocalIds
List<ReaddressedAddressData> readdressedBoxNumbers
)> ReaddressedAddresses { get; }

public List<(ReaddressAction action, AddressPersistentLocalId addressPersistentLocalId)> Actions { get; }
Expand All @@ -44,10 +45,7 @@ public StreetNameReaddresser(
_streetName = streetName;

ReaddressedAddresses =
new Dictionary<AddressPersistentLocalId, (ReaddressedAddressData readdressedHouseNumber,
List<ReaddressedAddressData> readdressedBoxNumbers, List<AddressPersistentLocalId>
rejectedBoxNumberPersistentLocalIds, List<AddressPersistentLocalId>
retiredBoxNumberPersistentLocalIds)>();
new Dictionary<AddressPersistentLocalId, (ReaddressedAddressData readdressedHouseNumber, List<ReaddressedAddressData> readdressedBoxNumbers)>();
Actions = new List<(ReaddressAction, AddressPersistentLocalId)>();

Readdress();
Expand Down Expand Up @@ -79,9 +77,7 @@ private void Readdress()
sourceAddress.Geometry,
sourceAddress.IsOfficiallyAssigned
),
new List<ReaddressedAddressData>(),
new List<AddressPersistentLocalId>(),
new List<AddressPersistentLocalId>()
new List<ReaddressedAddressData>()
));

ReaddressBoxNumbers(
Expand All @@ -95,6 +91,25 @@ private void Readdress()
RejectOrRetireDestinationAddressBoxNumbers(destinationAddress);
}
}

foreach (var sourceAddressWithBoxNumbers in _sourceAddressesWithBoxNumbers)
{
var sourceHouseNumberAddress = sourceAddressWithBoxNumbers.Key;
foreach (var sourceBoxNumberAddress in sourceAddressWithBoxNumbers.Value)
{
// 11 -> 13 -> 15
// 11A 13A1
// 11B 13B
// Given the above,
// When house number 11 is the current sourceAddress, it is never used as a destination address, so we should keep all of its box numbers.
// When house number 13 is the current sourceAddress, then ReaddressedAddresses will contain boxNumberAddress 13B and we should keep it.
if (IsUsedAsDestinationAddress(sourceHouseNumberAddress) &&
!HasBeenReaddressed(sourceBoxNumberAddress))
{
RejectOrRetireBoxNumberAddress(sourceBoxNumberAddress);
}
}
}
}

private (StreetNameAddress sourceAddress, StreetNameAddress? destinationAddress) GetSourceAndDestinationAddresses(ReaddressAddressItem addressToReaddress)
Expand Down Expand Up @@ -165,15 +180,13 @@ private void ReaddressBoxNumbers(
sourceBoxNumberAddress.IsOfficiallyAssigned
));

// 11 -> 13 -> 15
// 11A 13A1
// 11B 13B
// Given the above,
// When house number 11 is the current sourceAddress, it is never used as a destination address, so we should keep all of its box numbers.
// When house number 13 is the current sourceAddress, then ReaddressedAddresses will contain boxNumberAddress 13B and we should keep it.
if (IsUsedAsDestinationAddress(sourceAddress) && !HasBeenReaddressed(sourceBoxNumberAddress))
if (_sourceAddressesWithBoxNumbers.ContainsKey(sourceAddress))
{
_sourceAddressesWithBoxNumbers[sourceAddress].Add(sourceBoxNumberAddress);
}
else
{
RejectOrRetireBoxNumberAddress(sourceBoxNumberAddress);
_sourceAddressesWithBoxNumbers.Add(sourceAddress, new List<StreetNameAddress> { sourceBoxNumberAddress });
}
}
}
Expand Down Expand Up @@ -206,15 +219,11 @@ private void RejectOrRetireBoxNumberAddress(StreetNameAddress boxNumberAddress)
switch (boxNumberAddress.Status)
{
case AddressStatus.Proposed:
Actions.Add((ReaddressAction.Reject, boxNumberAddress.AddressPersistentLocalId));
ReaddressedAddresses[boxNumberAddress.Parent!.AddressPersistentLocalId]
.rejectedBoxNumberPersistentLocalIds.Add(boxNumberAddress.AddressPersistentLocalId);
Actions.Add((ReaddressAction.RejectBoxNumber, boxNumberAddress.AddressPersistentLocalId));
break;

case AddressStatus.Current:
Actions.Add((ReaddressAction.Retire, boxNumberAddress.AddressPersistentLocalId));
ReaddressedAddresses[boxNumberAddress.Parent!.AddressPersistentLocalId]
.retiredBoxNumberPersistentLocalIds.Add(boxNumberAddress.AddressPersistentLocalId);
Actions.Add((ReaddressAction.RetireBoxNumber, boxNumberAddress.AddressPersistentLocalId));
break;

default:
Expand Down Expand Up @@ -242,7 +251,7 @@ public enum ReaddressAction
{
ProposeHouseNumber,
ProposeBoxNumber,
Reject,
Retire,
RejectBoxNumber,
RetireBoxNumber,
}
}
13 changes: 6 additions & 7 deletions src/AddressRegistry/StreetName/StreetName_Readdress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace AddressRegistry.StreetName
using System;
using System.Collections.Generic;
using System.Linq;
using Address;
using Commands;
using DataStructures;
using Events;
Expand Down Expand Up @@ -46,11 +45,11 @@ public void Readdress(
readressedData.readdressedBoxNumbers));
}

// Reject Retire Actions
var rejectRetireActions = streetNameReaddresser.Actions.Where(x =>
x.action == ReaddressAction.Reject || x.action == ReaddressAction.Retire);
// Reject or Retire BoxNumber Actions
var rejectRetireBoxNumberActions = streetNameReaddresser.Actions.Where(x =>
x.action is ReaddressAction.RejectBoxNumber or ReaddressAction.RetireBoxNumber);

foreach (var (action, addressPersistentLocalId) in rejectRetireActions)
foreach (var (action, addressPersistentLocalId) in rejectRetireBoxNumberActions)
{
HandleAction(
action,
Expand Down Expand Up @@ -117,11 +116,11 @@ private void HandleAction(
executionContext.AddressesAdded.Add((PersistentLocalId, addressPersistentLocalId));
break;

case ReaddressAction.Reject:
case ReaddressAction.RejectBoxNumber:
RejectAddressBecauseOfReaddress(addressPersistentLocalId);
break;

case ReaddressAction.Retire:
case ReaddressAction.RetireBoxNumber:
RetireAddressBecauseOfReaddress(addressPersistentLocalId);
break;

Expand Down
Loading

0 comments on commit fcec945

Please sign in to comment.