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

fix: readdress addresses box number bug #1132

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading