diff --git a/elixir/apps/domain/lib/domain/auth/identity/sync.ex b/elixir/apps/domain/lib/domain/auth/identity/sync.ex index 923872b89..844916f35 100644 --- a/elixir/apps/domain/lib/domain/auth/identity/sync.ex +++ b/elixir/apps/domain/lib/domain/auth/identity/sync.ex @@ -38,13 +38,11 @@ defmodule Domain.Auth.Identity.Sync do :actor_ids_by_provider_identifier, fn _repo, %{ - plan_identities: {_insert, _update, delete}, - identities: identities, + update_identities_and_actors: update_identities, insert_identities: insert_identities } -> actor_ids_by_provider_identifier = - for identity <- identities ++ insert_identities, - identity.provider_identifier not in delete, + for identity <- update_identities ++ insert_identities, into: %{} do {identity.provider_identifier, identity.actor_id} end @@ -140,10 +138,22 @@ defmodule Domain.Auth.Identity.Sync do |> Enum.filter(fn identity -> identity.provider_identifier in provider_identifiers_to_update end) - # make sure that deleted identities are in the end in case of conflicts - |> Enum.sort_by(& &1.deleted_at, :desc) |> repo.preload(:actor) - |> Map.new(&{&1.provider_identifier, &1}) + |> Enum.reduce(%{}, fn identity, acc -> + acc_identity = Map.get(acc, identity.provider_identifier) + + # make sure that deleted identities are have the least priority in case of conflicts + cond do + is_nil(acc_identity) -> + Map.put(acc, identity.provider_identifier, identity) + + is_nil(acc_identity.deleted_at) -> + acc + + true -> + Map.put(acc, identity.provider_identifier, identity) + end + end) provider_identifiers_to_update |> Enum.uniq() diff --git a/elixir/apps/domain/lib/domain/gateways.ex b/elixir/apps/domain/lib/domain/gateways.ex index 78948304f..42e5cea56 100644 --- a/elixir/apps/domain/lib/domain/gateways.ex +++ b/elixir/apps/domain/lib/domain/gateways.ex @@ -339,7 +339,7 @@ defmodule Domain.Gateways do # Replace the location with the approximate distance to the client |> Enum.map(fn {{gateway_lat, gateway_lon}, gateway} when is_nil(gateway_lat) or is_nil(gateway_lon) -> - {Geo.fetch_radius_of_earth_km!(), gateway} + {nil, gateway} {{gateway_lat, gateway_lon}, gateway} -> distance = Geo.distance({lat, lon}, {gateway_lat, gateway_lon}) diff --git a/elixir/apps/domain/lib/domain/geo.ex b/elixir/apps/domain/lib/domain/geo.ex index c5de2f55d..4f483f263 100644 --- a/elixir/apps/domain/lib/domain/geo.ex +++ b/elixir/apps/domain/lib/domain/geo.ex @@ -1,10 +1,6 @@ defmodule Domain.Geo do @radius_of_earth_km 6371.0 - # Since most of our customers are from US we use - # geographic center of USA as default coordinates - @default_coordinates {39.8283, -98.5795} - @coordinates_by_code %{ "AF" => {33, 65}, "AX" => {60.116667, 19.9}, @@ -259,10 +255,6 @@ defmodule Domain.Geo do |> Enum.map(fn {code, {lat, lon}} -> {code, {lat * 1.0, lon * 1.0}} end) |> Map.new() - def fetch_radius_of_earth_km! do - @radius_of_earth_km - end - def distance({lat1, lon1}, {lat2, lon2}) do d_lat = degrees_to_radians(lat2 - lat1) d_lon = degrees_to_radians(lon2 - lon1) @@ -282,7 +274,10 @@ defmodule Domain.Geo do end def maybe_put_default_coordinates(country_code, {nil, nil}) do - Map.get(@coordinates_by_code, country_code, @default_coordinates) + case Map.get(@coordinates_by_code, country_code) do + {lat, lon} -> {lat, lon} + nil -> {nil, nil} + end end def maybe_put_default_coordinates(_country_code, {lat, lon}) do diff --git a/elixir/apps/domain/lib/domain/relays.ex b/elixir/apps/domain/lib/domain/relays.ex index 01af57c94..e646ae05e 100644 --- a/elixir/apps/domain/lib/domain/relays.ex +++ b/elixir/apps/domain/lib/domain/relays.ex @@ -322,7 +322,7 @@ defmodule Domain.Relays do end) |> Enum.map(fn {{nil, nil}, relay} -> - {Geo.fetch_radius_of_earth_km!(), relay} + {nil, relay} {{relay_lat, relay_lon}, relay} -> distance = Geo.distance({lat, lon}, {relay_lat, relay_lon})