feat(platform): include identity resolution in vehicle detail
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user