Skip to content

Commit

Permalink
Merge branch 'newsletter-settings' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
macifell committed Mar 4, 2024
2 parents 733712e + 4738b00 commit 9046565
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
67 changes: 32 additions & 35 deletions lib/recognizer/hal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,49 @@ defmodule Recognizer.Hal do
"""

def update_newsletter(user) do
req =
"/accounts/newsletter/interests"
|> build_url()
|> HTTPoison.get!(authorization_headers())
"/accounts/newsletter"
|> build_url()
|> HTTPoison.post!(newsletter_json_body(user), [json_header(), authorization_header()])
end

interests = Jason.decode!(req.body)["interests"]
defp newsletter_json_body(user) do
%{
"email_address" => user["email"],
"status" => "pending",
"interests" => user_interests(user),
"merge_fields" => %{
"FNAME" => user["first_name"],
"LNAME" => user["last_name"]
}
}
|> Jason.encode!()
end

groups =
Enum.reduce(interests, %{}, fn item, acc ->
Map.put(acc, item["id"], String.to_existing_atom(user["newsletter"]))
end)
defp user_interests(user) do
Enum.reduce(all_interests(), %{}, fn item, acc ->
Map.put(acc, item["id"], String.to_existing_atom(user["newsletter"]))
end)
end

defp all_interests() do
req =
"/accounts/newsletter?email_address=#{user["email"]}"
|> build_url()
|> HTTPoison.get!(authorization_headers())

status = Jason.decode!(req.body)["status"]

# only update if not already pending, or subscribed
# note: this means you cannot unsubscribe from recognizer settings
unless Enum.member?(["pending", "subscribed"], status) do
body =
%{
"email_address" => user["email"],
"status" => "pending",
"interests" => groups,
"merge_fields" => %{
"FNAME" => user["first_name"],
"LNAME" => user["last_name"]
}
}
|> Jason.encode!()

"/accounts/newsletter"
"/accounts/newsletter/interests"
|> build_url()
|> HTTPoison.post!(body, [{"content-type", "application/json"}] ++ authorization_headers())
end
|> HTTPoison.get!([authorization_header()])

Jason.decode!(req.body)["interests"]
end

defp build_url(path) do
base_url = Application.get_env(:recognizer, :hal_url)
Path.join([base_url, path])
end

defp authorization_headers() do
[{"authorization", "Recognizer #{Application.get_env(:recognizer, :hal_token)}"}]
defp json_header() do
{"content-type", "application/json"}
end

defp authorization_header() do
{"authorization", "Recognizer #{Application.get_env(:recognizer, :hal_token)}"}
end
end
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule Recognizer.MixProject do
{:cors_plug, "~> 2.0"},
{:cowboy, "~> 2.8", override: true},
{:cowlib, "~> 2.9.1", override: true},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:credo, "~> 1.5", only: [:dev, :"dev.local", :test], runtime: false},
{:decorator, "~> 1.2"},
{:ecto_enum, "~> 1.4"},
{:ecto_sql, "~> 3.4"},
Expand All @@ -60,9 +60,9 @@ defmodule Recognizer.MixProject do
{:redix, ">= 0.0.0"},
{:phoenix_ecto, "~> 4.1"},
{:phoenix_html, "~> 2.11"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_reload, "~> 1.2", only: [:dev, :"dev.local"]},
{:phoenix, "~> 1.5.7"},
{:phx_gen_auth, "~> 0.6", only: [:dev], runtime: false},
{:phx_gen_auth, "~> 0.6", only: [:dev, :"dev.local"], runtime: false},
{:plug_cowboy, "~> 2.4"},
{:pot, "~> 1.0"},
{:saxy, "~> 1.1"},
Expand Down

0 comments on commit 9046565

Please sign in to comment.