feat(platform): include identity resolution in vehicle detail
This commit is contained in:
@@ -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