feat(platform): resolve vehicle identity before navigation
This commit is contained in:
@@ -56,6 +56,58 @@ func (s *Service) Vehicles(ctx context.Context, query url.Values) (Page[VehicleR
|
||||
return s.store.Vehicles(ctx, query)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
vehicleQuery := url.Values{"keyword": {keyword}, "limit": {"20"}}
|
||||
if protocol = strings.TrimSpace(protocol); protocol != "" {
|
||||
vehicleQuery.Set("protocol", protocol)
|
||||
}
|
||||
vehicles, err := s.store.Vehicles(ctx, vehicleQuery)
|
||||
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
|
||||
}
|
||||
|
||||
func (s *Service) VehicleCoverage(ctx context.Context, query url.Values) (Page[VehicleCoverageRow], error) {
|
||||
return s.store.VehicleCoverage(ctx, query)
|
||||
}
|
||||
@@ -419,3 +471,16 @@ func sourceNames(statuses []VehicleSourceStatus) []string {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func appendIfMissing(values []string, value string) []string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return values
|
||||
}
|
||||
for _, current := range values {
|
||||
if current == value {
|
||||
return values
|
||||
}
|
||||
}
|
||||
return append(values, value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user