Skip to content
Open
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
349 changes: 349 additions & 0 deletions BinDays.Api.Collectors/Collectors/Councils/BassetlawDistrictCouncil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,349 @@
namespace BinDays.Api.Collectors.Collectors.Councils;

using BinDays.Api.Collectors.Collectors.Vendors;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

This using statement for BinDays.Api.Collectors.Collectors.Vendors is not used within the file. According to the style guide, unused using statements should be removed to keep the code clean and reduce clutter.

References
  1. Unused using statements should be removed to keep code clean and reduce clutter. (link)

using BinDays.Api.Collectors.Models;
using BinDays.Api.Collectors.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;

/// <summary>
/// Collector implementation for Bassetlaw District Council.
/// </summary>
internal sealed class BassetlawDistrictCouncil : GovUkCollectorBase, ICollector
{
/// <inheritdoc/>
public string Name => "Bassetlaw District Council";

/// <inheritdoc/>
public Uri WebsiteUrl => new("https://www.bassetlaw.gov.uk/");

/// <inheritdoc/>
public override string GovUkId => "bassetlaw";

/// <summary>
/// The list of bin types for this collector.
/// </summary>
private readonly IReadOnlyCollection<Bin> _binTypes =
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing garden waste bin.

[
new()
{
Name = "General Waste",
Colour = BinColour.Green,
Keys = [ "Green" ],
},
new()
{
Name = "Recycling",
Colour = BinColour.Blue,
Keys = [ "Blue" ],
},
];
Comment on lines +30 to +43
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

This collection initializer has a couple of style guide violations:

  1. Missing Trailing Commas: The items within the new() blocks and the blocks themselves are missing trailing commas, which are required for cleaner diffs.
  2. Unused Key: The "Custom" key for 'General Waste' is not used for matching in GetBinDays and should be removed.

Please update the initializer to include trailing commas and remove the unnecessary key.

	[
		new()
		{
			Name = "General Waste",
			Colour = BinColour.Green,
			Keys = [ "Green" ],
		},
		new()
		{
			Name = "Recycling",
			Colour = BinColour.Blue,
			Keys = [ "Blue" ],
		},
	];
References
  1. Always use trailing commas in multi-line initializers to make future diffs cleaner and reduce merge conflicts. (link)
  2. Only include keys that will actually be matched against the data source. Do not include unnecessary keys. (link)


/// <summary>
/// The order of layers to query for bin data.
/// </summary>
private readonly int[] _layerOrder = [0, 1, 22, 3, 4];

/// <summary>
/// Base dates for the recycling and general waste rotations by layer id.
/// </summary>
private readonly Dictionary<int, (DateOnly Blue, DateOnly Green)> _baseDates = new()
{
{ 0, (new DateOnly(2022, 1, 31), new DateOnly(2018, 1, 1)) },
{ 1, (new DateOnly(2022, 1, 25), new DateOnly(2022, 2, 1)) },
{ 3, (new DateOnly(2022, 2, 3), new DateOnly(2018, 1, 1)) },
{ 4, (new DateOnly(2022, 2, 4), new DateOnly(2022, 1, 28)) },
{ 22, (new DateOnly(2022, 2, 2), new DateOnly(2022, 1, 26)) },
};
Comment on lines +53 to +60
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

This dictionary initializer is missing a trailing comma on the last entry. The style guide requires trailing commas in all multi-line initializers to improve diff clarity and reduce merge conflicts.

	private readonly Dictionary<int, (DateOnly Blue, DateOnly Green)> _baseDates = new()
	{
		{ 0, (new DateOnly(2022, 1, 31), new DateOnly(2018, 1, 1)) },
		{ 1, (new DateOnly(2022, 1, 25), new DateOnly(2022, 2, 1)) },
		{ 3, (new DateOnly(2022, 2, 3), new DateOnly(2018, 1, 1)) },
		{ 4, (new DateOnly(2022, 2, 4), new DateOnly(2022, 1, 28)) },
		{ 22, (new DateOnly(2022, 2, 2), new DateOnly(2022, 1, 26)) },
	};
References
  1. Always use trailing commas in multi-line initializers to make future diffs cleaner and reduce merge conflicts. (link)


/// <inheritdoc/>
public GetAddressesResponse GetAddresses(string postcode, ClientSideResponse? clientSideResponse)
{
// Prepare client-side request for getting addresses
if (clientSideResponse == null)
{
var clientSideRequest = new ClientSideRequest
{
RequestId = 1,
Url = $"https://utility.arcgis.com/usrsvcs/servers/6fc1ccfdd03249299be8216a9f596935/rest/services/OSPlacesCascade_UPRN_basic/GeocodeServer/findAddressCandidates?f=json&SingleLine={postcode}",
Method = "GET",
Headers = new()
{
{ "user-agent", Constants.UserAgent },
},
};

var getAddressesResponse = new GetAddressesResponse
{
NextClientSideRequest = clientSideRequest,
};

return getAddressesResponse;
}
// Process addresses from response
else if (clientSideResponse.RequestId == 1)
{
var responseJson = JsonSerializer.Deserialize<JsonObject>(clientSideResponse.Content)!;
var rawAddresses = responseJson["candidates"]!.AsArray()!;

// Iterate through each address, and create a new address object
var addresses = new List<Address>();
foreach (var rawAddress in rawAddresses)
{
var attributes = rawAddress!["attributes"]!.AsObject();
var location = rawAddress["location"]!.AsObject();

var uprn = attributes["UPRN"]!.GetValue<string>().Trim();
var property = attributes["ADDRESS"]!.GetValue<string>().Trim();

var x = location["x"]!.GetValue<double>().ToString(CultureInfo.InvariantCulture);
var y = location["y"]!.GetValue<double>().ToString(CultureInfo.InvariantCulture);

// Uid format: "uprn;x-coordinate;y-coordinate"
var address = new Address
{
Property = property,
Postcode = postcode,
Uid = $"{uprn};{x};{y}",
};
Comment on lines +106 to +111
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

The Uid is being used to pass multiple data points (uprn, x, y) to the GetBinDays method, which is a great use of this pattern. However, the style guide recommends documenting the format of the concatenated Uid for clarity in both the method that creates it and the one that consumes it.

				// Uid format: "uprn;x-coordinate;y-coordinate"
				var address = new Address
				{
					Property = property,
					Postcode = postcode,
					Uid = $"{uprn};{x};{y}",
				};
References
  1. When concatenating multiple data parts into the UID, document the UID format with a comment in both methods (GetAddresses and GetBinDays) for clarity. (link)


addresses.Add(address);
}

var getAddressesResponse = new GetAddressesResponse
{
Addresses = [.. addresses],
};

return getAddressesResponse;
}

// Throw exception for invalid request
throw new InvalidOperationException("Invalid client-side request.");
}

/// <inheritdoc/>
public GetBinDaysResponse GetBinDays(Address address, ClientSideResponse? clientSideResponse)
{
// Prepare client-side request for getting bin polygons
if (clientSideResponse == null)
{
// Uid format: "uprn;x-coordinate;y-coordinate"
var coordinateParts = address.Uid!.Split(';');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

To improve clarity as per the style guide, please add a comment here documenting the expected format of the Uid that is being split.

			// Uid format: "uprn;x-coordinate;y-coordinate"
			var coordinateParts = address.Uid!.Split(';');
References
  1. When splitting a concatenated UID, document the UID format with a comment for clarity. (link)

var x = coordinateParts[1];
var y = coordinateParts[2];

var clientSideRequest = CreateLayerRequest(1, 0, x, y);

var getBinDaysResponse = new GetBinDaysResponse
{
NextClientSideRequest = clientSideRequest,
};

return getBinDaysResponse;
}
// Process layer responses
else if (clientSideResponse.RequestId is >= 1 and <= 5)
{
var layerPosition = int.Parse(clientSideResponse.Options.Metadata["layerPosition"], CultureInfo.InvariantCulture);
var x = clientSideResponse.Options.Metadata["x"];
var y = clientSideResponse.Options.Metadata["y"];

var responseJson = JsonSerializer.Deserialize<JsonObject>(clientSideResponse.Content)!;
var features = responseJson["features"]!.AsArray()!;

if (features.Count == 0)
{
var nextLayerPosition = layerPosition + 1;

if (nextLayerPosition >= _layerOrder.Length)
{
throw new InvalidOperationException("No bin data found for address.");
}

var nextClientSideRequest = CreateLayerRequest(clientSideResponse.RequestId + 1, nextLayerPosition, x, y);

var nextLayerResponse = new GetBinDaysResponse
{
NextClientSideRequest = nextClientSideRequest,
};

return nextLayerResponse;
}

var layerId = _layerOrder[layerPosition];
var attributes = features[0]!["attributes"]!.AsObject();

var collectionDay = GetCollectionDay(attributes);

var (Blue, Green) = _baseDates[layerId];
var blueDates = BuildUpcomingDates(Blue, collectionDay);
var greenDates = BuildUpcomingDates(Green, collectionDay);

var binDays = new List<BinDay>();

// Iterate through each general waste date, and create a new bin day object
foreach (var date in greenDates)
{
var binDay = new BinDay
{
Date = date,
Address = address,
Bins = ProcessingUtilities.GetMatchingBins(_binTypes, "Green"),
};

binDays.Add(binDay);
}

// Iterate through each recycling date, and create a new bin day object
foreach (var date in blueDates)
{
var binDay = new BinDay
{
Date = date,
Address = address,
Bins = ProcessingUtilities.GetMatchingBins(_binTypes, "Blue"),
};

binDays.Add(binDay);
}

var getBinDaysResponse = new GetBinDaysResponse
{
BinDays = ProcessingUtilities.ProcessBinDays(binDays),
};

return getBinDaysResponse;
}

// Throw exception for invalid request
throw new InvalidOperationException("Invalid client-side request.");
}

/// <summary>
/// Creates a client-side request for a specific feature layer.
/// </summary>
private ClientSideRequest CreateLayerRequest(int requestId, int layerPosition, string x, string y)
{
var layerId = _layerOrder[layerPosition];

var clientSideRequest = new ClientSideRequest
{
RequestId = requestId,
Url = $"https://services1.arcgis.com/P2LV4qXI9z8W2RdA/arcgis/rest/services/Bassetlaw_District_Bin_Collection_WFL1/FeatureServer/{layerId}/query?f=json&geometry={x},{y}&geometryType=esriGeometryPoint&inSR=27700&spatialRel=esriSpatialRelIntersects&outFields=*",
Method = "GET",
Headers = new()
{
{ "user-agent", Constants.UserAgent },
},
Options = new ClientSideOptions
{
Metadata =
{
{ "x", x },
{ "y", y },
{ "layerPosition", layerPosition.ToString(CultureInfo.InvariantCulture) },
},
},
};

return clientSideRequest;
}

/// <summary>
/// Determines the collection day from the feature attributes.
/// </summary>
private static DayOfWeek GetCollectionDay(JsonObject attributes)
{
var dayFields = new (string Field, DayOfWeek Day)[]
{
("Sunday", DayOfWeek.Sunday),
("Monday", DayOfWeek.Monday),
("Tuesday", DayOfWeek.Tuesday),
("Wednesday", DayOfWeek.Wednesday),
("Thursday", DayOfWeek.Thursday),
("Friday", DayOfWeek.Friday),
("Saturday", DayOfWeek.Saturday),
};

foreach (var (Field, Day) in dayFields)
{
if (attributes.TryGetPropertyValue(Field, out var valueNode))
{
var value = valueNode!.GetValue<string>().Trim();

if (value.Equals("Yes", StringComparison.OrdinalIgnoreCase))
{
return Day;
}
}
}

var dayName = attributes["Day_"]!.GetValue<string>().Trim();

return Enum.Parse<DayOfWeek>(dayName);
}

/// <summary>
/// Builds a list of upcoming collection dates using the fortnightly schedule.
/// </summary>
private static IReadOnlyCollection<DateOnly> BuildUpcomingDates(DateOnly baseDate, DayOfWeek collectionDay)
{
const int pickupWeekInterval = 2;
const int occurrences = 6;

var today = DateOnly.FromDateTime(DateTime.UtcNow);

var initialDate = GetNextCollectionDate(
today,
baseDate,
collectionDay,
pickupWeekInterval
);

var dates = new List<DateOnly>
{
initialDate,
};

for (var index = 1; index < occurrences; index++)
{
dates.Add(dates[index - 1].AddDays(pickupWeekInterval * 7));
}

return dates;
}

/// <summary>
/// Calculates the next collection date based on the base week and pickup interval.
/// </summary>
private static DateOnly GetNextCollectionDate(
DateOnly today,
DateOnly baseDate,
DayOfWeek collectionDay,
int pickupWeekInterval
)
{
var todayWeekStart = today.AddDays(0 - (int)today.DayOfWeek);
var baseWeekStart = baseDate.AddDays(0 - (int)baseDate.DayOfWeek);

var weeksBetween = (todayWeekStart.ToDateTime(TimeOnly.MinValue) - baseWeekStart.ToDateTime(TimeOnly.MinValue)).Days / 7;
var weekRemainder = ((weeksBetween % pickupWeekInterval) + pickupWeekInterval) % pickupWeekInterval;

var daysUntilCollection = ((int)collectionDay - (int)today.DayOfWeek + 7) % 7;

if (weekRemainder > 0)
{
daysUntilCollection += 7 * (pickupWeekInterval - weekRemainder);
}
else if (daysUntilCollection == 0)
{
daysUntilCollection += pickupWeekInterval * 7;
}

return today.AddDays(daysUntilCollection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace BinDays.Api.IntegrationTests.Collectors.Councils;

using BinDays.Api.Collectors.Collectors.Councils;
using BinDays.Api.IntegrationTests.Helpers;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

public class BassetlawDistrictCouncilTests
{
private readonly IntegrationTestClient _client;
private readonly ITestOutputHelper _outputHelper;
private static readonly string _govUkId = new BassetlawDistrictCouncil().GovUkId;

public BassetlawDistrictCouncilTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
_client = new IntegrationTestClient(outputHelper);
}

[Theory]
[InlineData("DN22 7EW")]
public async Task GetBinDaysTest(string postcode)
{
await TestSteps.EndToEnd(
_client,
postcode,
_govUkId,
_outputHelper
);
}
}