2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Address do
|
2019-03-14 17:31:14 +00:00
|
|
|
@moduledoc """
|
|
|
|
Handles the comment-related GraphQL calls
|
|
|
|
"""
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2019-03-19 10:16:03 +00:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-03-14 17:31:14 +00:00
|
|
|
alias Mobilizon.Service.Geospatial
|
|
|
|
|
2020-01-22 01:14:42 +00:00
|
|
|
require Logger
|
|
|
|
|
2019-03-19 10:16:03 +00:00
|
|
|
@doc """
|
|
|
|
Search an address
|
|
|
|
"""
|
2020-01-26 19:34:25 +00:00
|
|
|
@spec search(map, map, map) :: {:ok, [Address.t()]}
|
|
|
|
def search(
|
|
|
|
_parent,
|
2021-02-12 17:19:49 +00:00
|
|
|
%{query: query, locale: locale, page: _page, limit: _limit} = args,
|
2020-01-26 19:34:25 +00:00
|
|
|
%{context: %{ip: ip}}
|
|
|
|
) do
|
2019-11-08 18:37:14 +00:00
|
|
|
geolix = Geolix.lookup(ip)
|
2019-03-14 17:31:14 +00:00
|
|
|
|
2019-11-08 18:37:14 +00:00
|
|
|
country_code =
|
|
|
|
case geolix do
|
|
|
|
%{country: %{iso_code: country_code}} -> String.downcase(country_code)
|
|
|
|
_ -> nil
|
|
|
|
end
|
2019-03-14 17:31:14 +00:00
|
|
|
|
2021-02-12 17:19:49 +00:00
|
|
|
addresses =
|
|
|
|
Geospatial.service().search(query,
|
|
|
|
lang: locale,
|
|
|
|
country_code: country_code,
|
|
|
|
type: Map.get(args, :type)
|
|
|
|
)
|
2019-08-22 13:57:44 +00:00
|
|
|
|
2019-03-14 17:31:14 +00:00
|
|
|
{:ok, addresses}
|
|
|
|
end
|
|
|
|
|
2019-03-19 10:16:03 +00:00
|
|
|
@doc """
|
|
|
|
Reverse geocode some coordinates
|
|
|
|
"""
|
2020-01-26 19:34:25 +00:00
|
|
|
@spec reverse_geocode(map, map, map) :: {:ok, [Address.t()]}
|
2019-11-08 18:37:14 +00:00
|
|
|
def reverse_geocode(
|
|
|
|
_parent,
|
|
|
|
%{longitude: longitude, latitude: latitude, zoom: zoom, locale: locale},
|
|
|
|
_context
|
|
|
|
) do
|
|
|
|
addresses = Geospatial.service().geocode(longitude, latitude, lang: locale, zoom: zoom)
|
2019-03-14 17:31:14 +00:00
|
|
|
|
|
|
|
{:ok, addresses}
|
|
|
|
end
|
|
|
|
end
|