fix(vendors): normalize location labels

This commit is contained in:
Alexandre Brandizzi 2026-08-25 14:00:14 -03:00
parent bf7d1fcdf5
commit 4f4e3997ba
4 changed files with 20 additions and 8 deletions

View file

@ -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
{

View file

@ -689,7 +689,7 @@ public class VendorServiceTests
data.Setup(x => x.GetCompaniesForFacetsAsync(null, It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<VendorCompany>
{
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" }
});

View file

@ -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);
}

View file

@ -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);