From 4f4e3997ba8d53f7c73503c270398580894ccaf6 Mon Sep 17 00:00:00 2001 From: Alexandre Brandizzi Date: Tue, 25 Aug 2026 14:00:14 -0300 Subject: [PATCH] fix(vendors): normalize location labels --- .../VendorDataServiceTests.cs | 2 +- .../VendorServiceTests.cs | 2 +- .../Implementation/VendorDataService.cs | 19 +++++++++++++++---- .../Implementation/VendorService.cs | 5 +++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Api.SeaHavenIndustries.Tests/VendorDataServiceTests.cs b/Api.SeaHavenIndustries.Tests/VendorDataServiceTests.cs index edbdcbf..3fe4c0d 100644 --- a/Api.SeaHavenIndustries.Tests/VendorDataServiceTests.cs +++ b/Api.SeaHavenIndustries.Tests/VendorDataServiceTests.cs @@ -587,7 +587,7 @@ public class VendorDataServiceTests { Name = "Addr Only Co", NormalizedName = "addr only co", - Address = "123 Main St" + Address = " 123 Main St " }; var soloCompany = new VendorCompany { diff --git a/Api.SeaHavenIndustries.Tests/VendorServiceTests.cs b/Api.SeaHavenIndustries.Tests/VendorServiceTests.cs index b9e0db2..42c22bf 100644 --- a/Api.SeaHavenIndustries.Tests/VendorServiceTests.cs +++ b/Api.SeaHavenIndustries.Tests/VendorServiceTests.cs @@ -689,7 +689,7 @@ public class VendorServiceTests data.Setup(x => x.GetCompaniesForFacetsAsync(null, It.IsAny())) .ReturnsAsync(new List { - new() { Id = 1, Name = "Address Only Co", Address = "789 Warehouse Row" }, + new() { Id = 1, Name = "Address Only Co", Address = " 789 Warehouse Row " }, new() { Id = 2, Name = "City State Co", Address = "12 Ignored Way", City = "Denver", State = "CO" }, new() { Id = 3, Name = "Nowhere Co" } }); diff --git a/SeaHaven.DataServices/Implementation/VendorDataService.cs b/SeaHaven.DataServices/Implementation/VendorDataService.cs index ec5bef3..af67ae7 100644 --- a/SeaHaven.DataServices/Implementation/VendorDataService.cs +++ b/SeaHaven.DataServices/Implementation/VendorDataService.cs @@ -514,16 +514,27 @@ namespace SeaHaven.DataServices.Implementation var stateBlank = IsBlank(state); var addressBlank = IsBlank(address); - var cityCommaState = Concat(Concat(city, Expression.Constant(", ")), state); + var cityLabel = Expression.Condition( + cityBlank, + Expression.Constant(string.Empty), + Expression.Call(city, nameof(string.Trim), Type.EmptyTypes)); + var stateLabel = Expression.Condition( + stateBlank, + Expression.Constant(string.Empty), + Expression.Call(state, nameof(string.Trim), Type.EmptyTypes)); + var cityCommaState = Concat(Concat(cityLabel, Expression.Constant(", ")), stateLabel); - var onlyState = Expression.Condition(stateBlank, Expression.Constant(string.Empty), state); - var cityOrBoth = Expression.Condition(stateBlank, city, cityCommaState); + var onlyState = Expression.Condition(stateBlank, Expression.Constant(string.Empty), stateLabel); + var cityOrBoth = Expression.Condition(stateBlank, cityLabel, cityCommaState); var cityStateLabel = Expression.Condition(cityBlank, onlyState, cityOrBoth); // Exact Location label: City/State whenever either structured value is // present, otherwise the (projected) Address. The blank guard keeps the // label non-null so the caller's ToLower never sees a null reference. - var addressLabel = Expression.Condition(addressBlank, Expression.Constant(string.Empty), address); + var addressLabel = Expression.Condition( + addressBlank, + Expression.Constant(string.Empty), + Expression.Call(address, nameof(string.Trim), Type.EmptyTypes)); return Expression.Condition(Expression.AndAlso(cityBlank, stateBlank), addressLabel, cityStateLabel); } diff --git a/SeaHaven.Services/Implementation/VendorService.cs b/SeaHaven.Services/Implementation/VendorService.cs index f0d603f..c2ac206 100644 --- a/SeaHaven.Services/Implementation/VendorService.cs +++ b/SeaHaven.Services/Implementation/VendorService.cs @@ -693,8 +693,9 @@ namespace SeaHaven.Services.Implementation City = c.City, State = c.State, Label = !string.IsNullOrWhiteSpace(c.City) || !string.IsNullOrWhiteSpace(c.State) - ? string.Join(", ", new[] { c.City, c.State }.Where(value => !string.IsNullOrWhiteSpace(value))) - : c.Address + ? string.Join(", ", new[] { c.City?.Trim(), c.State?.Trim() } + .Where(value => !string.IsNullOrWhiteSpace(value))) + : c.Address!.Trim() }) .DistinctBy(location => location.Label, StringComparer.OrdinalIgnoreCase) .OrderBy(location => location.Label);