diff --git a/vehicle-data-platform/apps/api/internal/platform/handler_test.go b/vehicle-data-platform/apps/api/internal/platform/handler_test.go index e946ab1b..bbb460b7 100644 --- a/vehicle-data-platform/apps/api/internal/platform/handler_test.go +++ b/vehicle-data-platform/apps/api/internal/platform/handler_test.go @@ -75,6 +75,18 @@ func TestHandlerVehicleDetailResolvesPlateToVIN(t *testing.T) { if !strings.Contains(rec.Body.String(), `"vin":"LB9A32A24R0LS1426"`) { t.Fatalf("response should resolve plate to VIN: %s", rec.Body.String()) } + var body struct { + Data VehicleDetail `json:"data"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String()) + } + if body.Data.Resolution == nil || !body.Data.Resolution.Resolved || body.Data.Resolution.VIN != "LB9A32A24R0LS1426" { + t.Fatalf("vehicle detail should include canonical identity resolution, got %+v body=%s", body.Data.Resolution, rec.Body.String()) + } + if len(body.Data.Resolution.Protocols) < 2 { + t.Fatalf("vehicle detail resolution should include source protocols, got %+v", body.Data.Resolution.Protocols) + } } func TestHandlerVehicleDetailAcceptsKeyword(t *testing.T) { diff --git a/vehicle-data-platform/apps/api/internal/platform/model.go b/vehicle-data-platform/apps/api/internal/platform/model.go index f0d3fafe..022aa7d3 100644 --- a/vehicle-data-platform/apps/api/internal/platform/model.go +++ b/vehicle-data-platform/apps/api/internal/platform/model.go @@ -67,18 +67,19 @@ type VehicleIdentityResolution struct { } type VehicleDetail struct { - VIN string `json:"vin"` - LookupKey string `json:"lookupKey"` - LookupResolved bool `json:"lookupResolved"` - Identity *VehicleRow `json:"identity,omitempty"` - RealtimeSummary *VehicleRealtimeRow `json:"realtimeSummary,omitempty"` - Sources []string `json:"sources"` - SourceStatus []VehicleSourceStatus `json:"sourceStatus"` - Realtime []RealtimeLocationRow `json:"realtime"` - History Page[HistoryLocationRow] `json:"history"` - Raw Page[RawFrameRow] `json:"raw"` - Mileage Page[DailyMileageRow] `json:"mileage"` - Quality Page[QualityIssueRow] `json:"quality"` + VIN string `json:"vin"` + LookupKey string `json:"lookupKey"` + LookupResolved bool `json:"lookupResolved"` + Resolution *VehicleIdentityResolution `json:"resolution,omitempty"` + Identity *VehicleRow `json:"identity,omitempty"` + RealtimeSummary *VehicleRealtimeRow `json:"realtimeSummary,omitempty"` + Sources []string `json:"sources"` + SourceStatus []VehicleSourceStatus `json:"sourceStatus"` + Realtime []RealtimeLocationRow `json:"realtime"` + History Page[HistoryLocationRow] `json:"history"` + Raw Page[RawFrameRow] `json:"raw"` + Mileage Page[DailyMileageRow] `json:"mileage"` + Quality Page[QualityIssueRow] `json:"quality"` } type VehicleSourceStatus struct { diff --git a/vehicle-data-platform/apps/api/internal/platform/service.go b/vehicle-data-platform/apps/api/internal/platform/service.go index 040a3d0c..04a7c375 100644 --- a/vehicle-data-platform/apps/api/internal/platform/service.go +++ b/vehicle-data-platform/apps/api/internal/platform/service.go @@ -58,9 +58,8 @@ func (s *Service) Vehicles(ctx context.Context, query url.Values) (Page[VehicleR func (s *Service) ResolveVehicleIdentity(ctx context.Context, keyword string, protocol string) (VehicleIdentityResolution, error) { keyword = strings.TrimSpace(keyword) - result := VehicleIdentityResolution{LookupKey: keyword, Protocols: []string{}} if keyword == "" { - return result, nil + return VehicleIdentityResolution{LookupKey: keyword, Protocols: []string{}}, nil } vehicleQuery := url.Values{"keyword": {keyword}, "limit": {"20"}} if protocol = strings.TrimSpace(protocol); protocol != "" { @@ -70,42 +69,7 @@ func (s *Service) ResolveVehicleIdentity(ctx context.Context, keyword string, pr if err != nil { return VehicleIdentityResolution{}, err } - identity := resolveVehicleIdentity(keyword, vehicles.Items) - if identity == nil || strings.TrimSpace(identity.VIN) == "" { - if isLikelyVIN(keyword) { - result.Resolved = true - result.VIN = keyword - } - return result, nil - } - result.Resolved = true - result.VIN = identity.VIN - result.Plate = identity.Plate - result.Phone = identity.Phone - result.OEM = identity.OEM - for _, vehicle := range vehicles.Items { - if !strings.EqualFold(vehicle.VIN, identity.VIN) { - continue - } - if vehicle.Online { - result.Online = true - } - if vehicle.LastSeen > result.LastSeen { - result.LastSeen = vehicle.LastSeen - } - if result.Plate == "" { - result.Plate = vehicle.Plate - } - if result.Phone == "" { - result.Phone = vehicle.Phone - } - if result.OEM == "" { - result.OEM = vehicle.OEM - } - result.Protocols = appendIfMissing(result.Protocols, vehicle.Protocol) - } - sort.Strings(result.Protocols) - return result, nil + return buildVehicleIdentityResolution(keyword, vehicles.Items), nil } func (s *Service) VehicleCoverage(ctx context.Context, query url.Values) (Page[VehicleCoverageRow], error) { @@ -128,12 +92,13 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string if err != nil { return VehicleDetail{}, err } + resolution := buildVehicleIdentityResolution(keyword, vehicles.Items) identity := resolveVehicleIdentity(keyword, vehicles.Items) resolvedVIN := "" if identity != nil && strings.TrimSpace(identity.VIN) != "" { resolvedVIN = identity.VIN - } else if isLikelyVIN(keyword) { - resolvedVIN = keyword + } else if resolution.Resolved { + resolvedVIN = resolution.VIN } queryVIN := resolvedVIN if queryVIN == "" { @@ -152,6 +117,7 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string VIN: "", LookupKey: keyword, LookupResolved: false, + Resolution: &resolution, Sources: []string{}, SourceStatus: []VehicleSourceStatus{}, Realtime: []RealtimeLocationRow{}, @@ -207,6 +173,7 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string VIN: resolvedVIN, LookupKey: keyword, LookupResolved: resolvedVIN != "", + Resolution: &resolution, Identity: identity, RealtimeSummary: summary, Sources: sourceNames(sourceStatus), @@ -484,3 +451,44 @@ func appendIfMissing(values []string, value string) []string { } return append(values, value) } + +func buildVehicleIdentityResolution(keyword string, vehicles []VehicleRow) VehicleIdentityResolution { + keyword = strings.TrimSpace(keyword) + result := VehicleIdentityResolution{LookupKey: keyword, Protocols: []string{}} + identity := resolveVehicleIdentity(keyword, vehicles) + if identity == nil || strings.TrimSpace(identity.VIN) == "" { + if isLikelyVIN(keyword) { + result.Resolved = true + result.VIN = keyword + } + return result + } + result.Resolved = true + result.VIN = identity.VIN + result.Plate = identity.Plate + result.Phone = identity.Phone + result.OEM = identity.OEM + for _, vehicle := range vehicles { + if !strings.EqualFold(vehicle.VIN, identity.VIN) { + continue + } + if vehicle.Online { + result.Online = true + } + if vehicle.LastSeen > result.LastSeen { + result.LastSeen = vehicle.LastSeen + } + if result.Plate == "" { + result.Plate = vehicle.Plate + } + if result.Phone == "" { + result.Phone = vehicle.Phone + } + if result.OEM == "" { + result.OEM = vehicle.OEM + } + result.Protocols = appendIfMissing(result.Protocols, vehicle.Protocol) + } + sort.Strings(result.Protocols) + return result +} diff --git a/vehicle-data-platform/apps/web/src/api/types.ts b/vehicle-data-platform/apps/web/src/api/types.ts index 329ba042..23385b4d 100644 --- a/vehicle-data-platform/apps/web/src/api/types.ts +++ b/vehicle-data-platform/apps/web/src/api/types.ts @@ -67,6 +67,7 @@ export interface VehicleDetail { vin: string; lookupKey: string; lookupResolved: boolean; + resolution?: VehicleIdentityResolution; identity?: VehicleRow; realtimeSummary?: VehicleRealtimeRow; sources: string[]; diff --git a/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx b/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx index f21a6c83..28f4efff 100644 --- a/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx +++ b/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx @@ -73,15 +73,16 @@ export function VehicleDetail({ const identity = detail?.identity; const summary = detail?.realtimeSummary; const latest = detail?.realtime[0]; - const resolvedVIN = detail?.vin || summary?.vin || identity?.vin || latest?.vin || query.keyword; - const hasResolvedVIN = detail?.lookupResolved ?? isLikelyVIN(resolvedVIN); + const resolution = detail?.resolution; + const resolvedVIN = resolution?.vin || detail?.vin || summary?.vin || identity?.vin || latest?.vin || query.keyword; + const hasResolvedVIN = resolution?.resolved ?? detail?.lookupResolved ?? isLikelyVIN(resolvedVIN); const displayVIN = hasResolvedVIN ? resolvedVIN : '-'; - const displayLookupKey = detail?.lookupKey || query.keyword; - const protocols = useMemo(() => detail?.sources ?? [], [detail?.sources]); + const displayLookupKey = resolution?.lookupKey || detail?.lookupKey || query.keyword; + const protocols = useMemo(() => resolution?.protocols?.length ? resolution.protocols : detail?.sources ?? [], [detail?.sources, resolution?.protocols]); const latestRaw = detail?.raw.items[0]; const qualityCount = detail?.quality.total ?? 0; - const online = summary?.online || identity?.online || false; - const lastSeen = summary?.lastSeen || latest?.lastSeen || '-'; + const online = resolution?.online || summary?.online || identity?.online || false; + const lastSeen = resolution?.lastSeen || summary?.lastSeen || latest?.lastSeen || '-'; const formKey = `${query.keyword}-${query.protocol ?? ''}`; const qualityTable = (