mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-27 06:05:19 +00:00
When a name match is found, calculate distances from that enterprise, rather than from the geocoded lookup
This commit is contained in:
@@ -12,8 +12,7 @@ Darkswarm.controller "EnterprisesCtrl", ($scope, Enterprises, Search, $document,
|
||||
|
||||
|
||||
$scope.$watch "query", (query)->
|
||||
Enterprises.flagMatching query
|
||||
Enterprises.updateDistance query
|
||||
Enterprises.evaluateQuery query
|
||||
Search.search query
|
||||
|
||||
$rootScope.$on "$locationChangeSuccess", (newRoute, oldRoute) ->
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo, $rootScope)->
|
||||
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo)->
|
||||
new class Enterprises
|
||||
enterprises_by_id: {}
|
||||
constructor: ->
|
||||
@@ -28,6 +28,10 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer,
|
||||
Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id
|
||||
Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id
|
||||
|
||||
evaluateQuery: (query) ->
|
||||
@flagMatching query
|
||||
@calculateDistance query
|
||||
|
||||
flagMatching: (query) ->
|
||||
for enterprise in @enterprises
|
||||
enterprise.matches_name_query = if query? && query.length > 0
|
||||
@@ -35,25 +39,30 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer,
|
||||
else
|
||||
false
|
||||
|
||||
updateDistance: (query) ->
|
||||
firstMatching: ->
|
||||
(enterprise for enterprise in @enterprises when enterprise.matches_name_query)[0]
|
||||
|
||||
calculateDistance: (query) ->
|
||||
if query?.length > 0
|
||||
@calculateDistance(query)
|
||||
if @firstMatching()?
|
||||
@setDistanceFrom @firstMatching()
|
||||
else
|
||||
@calculateDistanceGeo(query)
|
||||
else
|
||||
@resetDistance()
|
||||
|
||||
calculateDistance: (query) ->
|
||||
calculateDistanceGeo: (query) ->
|
||||
Geo.geocode query, (results, status) =>
|
||||
if status == Geo.OK
|
||||
console.log "Geocoded #{query} -> #{results[0].geometry.location}."
|
||||
#console.log "Geocoded #{query} -> #{results[0].geometry.location}."
|
||||
@setDistanceFrom results[0].geometry.location
|
||||
else
|
||||
console.log "Geocoding failed for the following reason: #{status}"
|
||||
@resetDistance()
|
||||
|
||||
setDistanceFrom: (location) ->
|
||||
$rootScope.$apply =>
|
||||
for enterprise in @enterprises
|
||||
enterprise.distance = Geo.distanceBetween enterprise, location
|
||||
setDistanceFrom: (locatable) ->
|
||||
for enterprise in @enterprises
|
||||
enterprise.distance = Geo.distanceBetween enterprise, locatable
|
||||
|
||||
resetDistance: ->
|
||||
enterprise.distance = null for enterprise in @enterprises
|
||||
|
||||
@@ -101,35 +101,52 @@ describe "Enterprises service", ->
|
||||
Enterprises.flagMatching ''
|
||||
expect(e.matches_name_query).toBe false for e in enterprises
|
||||
|
||||
describe "updating distance of enterprises from a location", ->
|
||||
it "calculates the distance when a query is provided", ->
|
||||
spyOn(Enterprises, "calculateDistance")
|
||||
Enterprises.updateDistance "asdf"
|
||||
expect(Enterprises.calculateDistance).toHaveBeenCalledWith("asdf")
|
||||
describe "finding the first enterprise matched by a query", ->
|
||||
it "returns the enterprise when one exists", ->
|
||||
enterprises[0].matches_name_query = false
|
||||
enterprises[1].matches_name_query = true
|
||||
expect(Enterprises.firstMatching()).toEqual enterprises[1]
|
||||
|
||||
it "returns undefined otherwise", ->
|
||||
e.matches_name_query = false for e in enterprises
|
||||
expect(Enterprises.firstMatching()).toBeUndefined()
|
||||
|
||||
describe "calculating the distance of enterprises from a location", ->
|
||||
describe "when a query is provided", ->
|
||||
it "sets the distance from the enterprise when a name match is available", ->
|
||||
spyOn(Enterprises, "firstMatching").andReturn('match')
|
||||
spyOn(Enterprises, "setDistanceFrom")
|
||||
Enterprises.calculateDistance "asdf"
|
||||
expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith('match')
|
||||
|
||||
it "calculates the distance from the geocoded query otherwise", ->
|
||||
spyOn(Enterprises, "calculateDistanceGeo")
|
||||
Enterprises.calculateDistance "asdf"
|
||||
expect(Enterprises.calculateDistanceGeo).toHaveBeenCalledWith("asdf")
|
||||
|
||||
it "resets the distance when query is null", ->
|
||||
spyOn(Enterprises, "resetDistance")
|
||||
Enterprises.updateDistance null
|
||||
Enterprises.calculateDistance null
|
||||
expect(Enterprises.resetDistance).toHaveBeenCalled()
|
||||
|
||||
it "resets the distance when query is blank", ->
|
||||
spyOn(Enterprises, "resetDistance")
|
||||
Enterprises.updateDistance ""
|
||||
Enterprises.calculateDistance ""
|
||||
expect(Enterprises.resetDistance).toHaveBeenCalled()
|
||||
|
||||
describe "calculating the distance of enterprises from a location", ->
|
||||
describe "calculating the distance of enterprises from a location by geocoding", ->
|
||||
beforeEach ->
|
||||
spyOn(Enterprises, "setDistanceFrom")
|
||||
|
||||
it "calculates distance for all enterprises when geocoding succeeds", ->
|
||||
Geo.succeed = true
|
||||
Enterprises.calculateDistance('query')
|
||||
Enterprises.calculateDistanceGeo('query')
|
||||
expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith("location")
|
||||
|
||||
it "resets distance when geocoding fails", ->
|
||||
Geo.succeed = false
|
||||
spyOn(Enterprises, "resetDistance")
|
||||
Enterprises.calculateDistance('query')
|
||||
Enterprises.calculateDistanceGeo('query')
|
||||
expect(Enterprises.setDistanceFrom).not.toHaveBeenCalled()
|
||||
expect(Enterprises.resetDistance).toHaveBeenCalled()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user