Class: SpreeCmCommissioner::PlaceResolver

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/place_resolver.rb

Overview

PlaceResolver finds or builds a SpreeCmCommissioner::Place keyed on a stable upstream identifier instead of the fragile name lookup the polling services used to do (‘Place.find_by(name:)`).

Lookup order:

1. IntegrationMapping for (integration, external_id) on Place — authoritative
2. Legacy backfill: Place.find_by(name:) — adopts pre-mapping rows on first re-sync
3. Build a new Place

The resolver persists the Place and its IntegrationMapping. Callers then wire the Place to a VendorPlace (which has its own mapping).

‘Place#reference` is left to the Google Places geocoder — it’s the canonical Google place_id used by admin tooling and vendor place selection.

Instance Method Summary collapse

Constructor Details

#initialize(integration:, sync_result: nil, geocoder: nil) ⇒ PlaceResolver

Returns a new instance of PlaceResolver.



17
18
19
20
21
# File 'app/services/spree_cm_commissioner/place_resolver.rb', line 17

def initialize(integration:, sync_result: nil, geocoder: nil)
  @integration = integration
  @sync_result = sync_result || SpreeCmCommissioner::Integrations::Base::SyncResult.new
  @geocoder = geocoder || SpreeCmCommissioner::GoogleMapsGeocoder.new
end

Instance Method Details

#resolve(external_id:, name:, code: nil, lat: nil, lon: nil, geocode_context: nil) ⇒ SpreeCmCommissioner::Place

Parameters:

  • external_id (String)

    stable upstream id, e.g. “location-1”, “stop-3”, “branch-65”

  • name (String)

    display name

  • code (String, nil) (defaults to: nil)

    short code

  • lat (Numeric, nil) (defaults to: nil)

    upstream latitude when available (preferred over geocoding)

  • lon (Numeric, nil) (defaults to: nil)

    upstream longitude when available

  • geocode_context (String, nil) (defaults to: nil)

    free-form context for geocoder fallback (e.g. country name)

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/spree_cm_commissioner/place_resolver.rb', line 30

def resolve(external_id:, name:, code: nil, lat: nil, lon: nil, geocode_context: nil) # rubocop:disable Metrics/ParameterLists
  mapping = SpreeCmCommissioner::Place.find_or_initialize_integration_mapping(
    integration_id: @integration.id,
    external_id: external_id
  )

  place = resolve_place(mapping, name: name)
  ensure_coordinates!(place, lat: lat, lon: lon, geocode_context: geocode_context, geocode_query: name)

  @sync_result.track(:place, place) do |tracker|
    attrs = { name: name }
    attrs[:code] = code if code.present?
    place.assign_attributes(attrs)
    tracker.save_if_changed!(place)
  end

  finalize_mapping!(mapping, place: place, name: name)
  place
end