mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-03 06:59:14 +00:00
Enterprises can calculate their distances from a location via geocode lookup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter)->
|
||||
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Geo)->
|
||||
new class Enterprises
|
||||
enterprises_by_id: {}
|
||||
constructor: ->
|
||||
@@ -28,3 +28,24 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer,
|
||||
Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id
|
||||
Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id
|
||||
|
||||
updateDistance: (query) ->
|
||||
if query.length > 0
|
||||
@calculateDistance(query)
|
||||
else
|
||||
@resetDistance()
|
||||
|
||||
calculateDistance: (query) ->
|
||||
Geo.geocode query, (results, status) =>
|
||||
if status == Geo.OK
|
||||
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) ->
|
||||
for enterprise in @enterprises
|
||||
enterprise.distance = Geo.distanceBetween enterprise, location
|
||||
|
||||
resetDistance: ->
|
||||
enterprise.distance = null for enterprise in @enterprises
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
describe "Enterprises service", ->
|
||||
Enterprises = null
|
||||
CurrentHubMock = {}
|
||||
Geo =
|
||||
OK: 'ok'
|
||||
succeed: true
|
||||
geocode: (query, callback) ->
|
||||
if @succeed
|
||||
results = [{geometry: {location: "location"}}]
|
||||
callback(results, @OK)
|
||||
else
|
||||
callback(results, 'Oops')
|
||||
distanceBetween: (locatable, location) ->
|
||||
123
|
||||
|
||||
taxons = [
|
||||
{id: 1, name: "test"}
|
||||
]
|
||||
@@ -19,6 +31,7 @@ describe "Enterprises service", ->
|
||||
module 'Darkswarm'
|
||||
module ($provide)->
|
||||
$provide.value "CurrentHub", CurrentHubMock
|
||||
$provide.value "Geo", Geo
|
||||
null
|
||||
angular.module('Darkswarm').value('enterprises', enterprises)
|
||||
angular.module('Darkswarm').value('taxons', taxons)
|
||||
@@ -73,3 +86,45 @@ describe "Enterprises service", ->
|
||||
expect(Enterprises.producers).toContain Enterprises.enterprises[4]
|
||||
expect(Enterprises.producers).toContain Enterprises.enterprises[5]
|
||||
expect(Enterprises.producers).toContain Enterprises.enterprises[6]
|
||||
|
||||
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")
|
||||
|
||||
it "resets the distance when query is blank", ->
|
||||
spyOn(Enterprises, "resetDistance")
|
||||
Enterprises.updateDistance ""
|
||||
expect(Enterprises.resetDistance).toHaveBeenCalled()
|
||||
|
||||
describe "calculating the distance of enterprises from a location", ->
|
||||
beforeEach ->
|
||||
spyOn(Enterprises, "setDistanceFrom")
|
||||
|
||||
it "calculates distance for all enterprises when geocoding succeeds", ->
|
||||
Geo.succeed = true
|
||||
Enterprises.calculateDistance('query')
|
||||
expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith("location")
|
||||
|
||||
it "resets distance when geocoding fails", ->
|
||||
Geo.succeed = false
|
||||
spyOn(Enterprises, "resetDistance")
|
||||
Enterprises.calculateDistance('query')
|
||||
expect(Enterprises.setDistanceFrom).not.toHaveBeenCalled()
|
||||
expect(Enterprises.resetDistance).toHaveBeenCalled()
|
||||
|
||||
describe "setting the distance of each enterprise from a central location", ->
|
||||
it "sets the distances", ->
|
||||
Enterprises.setDistanceFrom 'location'
|
||||
for e in Enterprises.enterprises
|
||||
expect(e.distance).toEqual 123
|
||||
|
||||
describe "resetting the distance measurement of all enterprises", ->
|
||||
beforeEach ->
|
||||
e.distance = 123 for e in Enterprises.enterprises
|
||||
|
||||
it "resets the distance", ->
|
||||
Enterprises.resetDistance()
|
||||
for e in Enterprises.enterprises
|
||||
expect(e.distance).toBeNull()
|
||||
@@ -1,7 +1,8 @@
|
||||
describe "Groups service", ->
|
||||
Groups = null
|
||||
Enterprises = null
|
||||
CurrentHubMock = {}
|
||||
CurrentHubMock = {}
|
||||
Geo = {}
|
||||
groups = [{
|
||||
id: 1
|
||||
name: "Test Group"
|
||||
@@ -17,17 +18,18 @@ describe "Groups service", ->
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
angular.module('Darkswarm').value('groups', groups)
|
||||
angular.module('Darkswarm').value('enterprises', enterprises)
|
||||
angular.module('Darkswarm').value('groups', groups)
|
||||
angular.module('Darkswarm').value('enterprises', enterprises)
|
||||
module ($provide)->
|
||||
$provide.value "CurrentHub", CurrentHubMock
|
||||
$provide.value "CurrentHub", CurrentHubMock
|
||||
$provide.value "Geo", Geo
|
||||
null
|
||||
inject (_Groups_, _Enterprises_)->
|
||||
Groups = _Groups_
|
||||
Enterprises = _Enterprises_
|
||||
Groups = _Groups_
|
||||
Enterprises = _Enterprises_
|
||||
|
||||
it "dereferences group enterprises", ->
|
||||
expect(Groups.groups[0].enterprises[0]).toBe enterprises[0]
|
||||
|
||||
|
||||
it "dereferences enterprise groups", ->
|
||||
expect(Enterprises.enterprises[0].groups[0]).toBe groups[0]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
describe "Hubs service", ->
|
||||
OfnMap = null
|
||||
CurrentHubMock = {}
|
||||
CurrentHubMock = {}
|
||||
Geo = {}
|
||||
enterprises = [
|
||||
{
|
||||
id: 2
|
||||
@@ -13,12 +14,13 @@ describe "Hubs service", ->
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
angular.module('Darkswarm').value('enterprises', enterprises)
|
||||
angular.module('Darkswarm').value('enterprises', enterprises)
|
||||
module ($provide)->
|
||||
$provide.value "CurrentHub", CurrentHubMock
|
||||
$provide.value "CurrentHub", CurrentHubMock
|
||||
$provide.value "Geo", Geo
|
||||
null
|
||||
inject ($injector)->
|
||||
OfnMap = $injector.get("OfnMap")
|
||||
OfnMap = $injector.get("OfnMap")
|
||||
|
||||
it "builds MapMarkers from enterprises", ->
|
||||
expect(OfnMap.enterprises[0].id).toBe enterprises[0].id
|
||||
|
||||
@@ -10,6 +10,7 @@ describe 'Products service', ->
|
||||
productWithImage = null
|
||||
properties = null
|
||||
taxons = null
|
||||
Geo = {}
|
||||
|
||||
beforeEach ->
|
||||
product =
|
||||
@@ -40,6 +41,7 @@ describe 'Products service', ->
|
||||
$provide.value "currentOrder", currentOrder
|
||||
$provide.value "taxons", taxons
|
||||
$provide.value "properties", properties
|
||||
$provide.value "Geo", Geo
|
||||
null
|
||||
|
||||
inject ($injector, _$httpBackend_)->
|
||||
|
||||
Reference in New Issue
Block a user