diff --git a/Api.SeaHavenIndustries.Tests/VendorDataServiceTests.cs b/Api.SeaHavenIndustries.Tests/VendorDataServiceTests.cs index 650c911..2bed953 100644 --- a/Api.SeaHavenIndustries.Tests/VendorDataServiceTests.cs +++ b/Api.SeaHavenIndustries.Tests/VendorDataServiceTests.cs @@ -364,6 +364,109 @@ public class VendorDataServiceTests items.Should().Contain(i => i.ContactName == "No Company Tech" && i.VendorId != null); } + [Fact] + public async Task GetDirectoryPagedAsync_UsesCanonicalCompanyLocationAndEmailFallback() + { + await using var context = NewContext(); + var company = new VendorCompany + { + Name = "Canonical Co", + NormalizedName = "canonical co", + Email = "dispatch@canonical.example", + City = "Chicago", + State = "IL" + }; + context.VendorCompanies.Add(company); + context.Vendors.AddRange( + new Vendor + { + CompanyName = "Canonical Co", + ContactName = "Company Fallback", + Email = " ", + City = "Stale City", + State = "TX", + IsActive = true, + IsDeleted = false, + Company = company + }, + new Vendor + { + CompanyName = "Canonical Co", + ContactName = "Technician Email", + Email = "technician@canonical.example", + IsActive = true, + IsDeleted = false, + Company = company + }, + new Vendor + { + CompanyName = "Legacy Co", + ContactName = "Unlinked Technician", + Email = "legacy@example.com", + City = "Austin", + State = "TX", + IsActive = true, + IsDeleted = false, + CompanyId = null + }); + await context.SaveChangesAsync(); + + var service = new VendorDataService(context); + var (items, totalCount) = await service.GetDirectoryPagedAsync(1, 50, isActive: true); + + totalCount.Should().Be(3); + var companyFallback = items.Single(i => i.ContactName == "Company Fallback"); + companyFallback.Email.Should().Be("dispatch@canonical.example"); + companyFallback.City.Should().Be("Chicago"); + companyFallback.State.Should().Be("IL"); + + var technicianEmail = items.Single(i => i.ContactName == "Technician Email"); + technicianEmail.Email.Should().Be("technician@canonical.example"); + technicianEmail.City.Should().Be("Chicago"); + technicianEmail.State.Should().Be("IL"); + + var legacy = items.Single(i => i.ContactName == "Unlinked Technician"); + legacy.Email.Should().Be("legacy@example.com"); + legacy.City.Should().Be("Austin"); + legacy.State.Should().Be("TX"); + } + + [Fact] + public async Task GetDirectoryPagedAsync_SearchAndLocationFilterUseCanonicalCompanyLocation() + { + await using var context = NewContext(); + var company = new VendorCompany + { + Name = "Searchable Co", + NormalizedName = "searchable co", + City = "Chicago", + State = "IL" + }; + context.VendorCompanies.Add(company); + context.Vendors.Add(new Vendor + { + CompanyName = "Searchable Co", + ContactName = "Pat", + City = null, + State = null, + IsActive = true, + IsDeleted = false, + Company = company + }); + await context.SaveChangesAsync(); + + var service = new VendorDataService(context); + var search = await service.GetDirectoryPagedAsync(1, 50, search: "chicago", isActive: true); + var location = await service.GetDirectoryPagedAsync( + 1, + 50, + isActive: true, + locations: new[] { "Chicago, IL" }); + + search.Items.Should().ContainSingle(i => i.ContactName == "Pat"); + location.Items.Should().ContainSingle(i => i.ContactName == "Pat"); + } + [Theory] [InlineData("trade")] [InlineData("location")] diff --git a/SeaHaven.DataServices/Implementation/VendorDataService.cs b/SeaHaven.DataServices/Implementation/VendorDataService.cs index 01a5be6..81f77ba 100644 --- a/SeaHaven.DataServices/Implementation/VendorDataService.cs +++ b/SeaHaven.DataServices/Implementation/VendorDataService.cs @@ -191,13 +191,21 @@ namespace SeaHaven.DataServices.Implementation CompanyId = v.CompanyId ?? 0, CompanyName = v.CompanyName, ContactName = v.ContactName, - Email = v.Email, + Email = v.Email != null && v.Email.Trim() != "" + ? v.Email + : v.Company != null + ? v.Company.Email + : v.Email, Phone = v.Phone, CompanyPhone = v.CompanyPhone, PreferredContact = v.PreferredContact, Address = v.Address, - City = v.City, - State = v.State, + City = v.Company != null && v.Company.City != null && v.Company.City.Trim() != "" + ? v.Company.City + : v.City, + State = v.Company != null && v.Company.State != null && v.Company.State.Trim() != "" + ? v.Company.State + : v.State, Zip = v.Zip, TradeSpecialties = v.TradeSpecialties, GoogleMapsUrl = v.GoogleMapsUrl,