diff --git a/lib/code_corps/emails/project_user_acceptance_email.ex b/lib/code_corps/emails/project_user_acceptance_email.ex deleted file mode 100644 index 4152da307..000000000 --- a/lib/code_corps/emails/project_user_acceptance_email.ex +++ /dev/null @@ -1,40 +0,0 @@ -defmodule CodeCorps.Emails.ProjectUserAcceptanceEmail do - import Bamboo.Email, only: [to: 2] - import Bamboo.PostmarkHelper - - alias CodeCorps.{Project, ProjectUser, Repo, User, WebClient} - alias CodeCorps.Emails.BaseEmail - alias CodeCorps.Presenters.ImagePresenter - - @spec create(ProjectUser.t) :: Bamboo.Email.t - def create(%ProjectUser{project: project, user: user}) do - BaseEmail.create - |> to(user.email) - |> template(template_id(), build_model(project, user)) - end - - @spec build_model(Project.t, User.t) :: map - defp build_model(%Project{} = project, %User{} = user) do - %{ - project_logo_url: ImagePresenter.large(project), - project_title: project.title, - project_url: project |> preload() |> url(), - subject: "#{project.title} just added you as a contributor", - user_first_name: user.first_name, - user_image_url: ImagePresenter.large(user) - } - end - - @spec preload(Project.t) :: Project.t - defp preload(%Project{} = project), do: project |> Repo.preload(:organization) - - @spec url(Project.t) :: String.t - defp url(project) do - WebClient.url() - |> URI.merge(project.organization.slug <> "/" <> project.slug) - |> URI.to_string - end - - @spec template_id :: String.t - defp template_id, do: Application.get_env(:code_corps, :postmark_project_user_acceptance_template) -end diff --git a/lib/code_corps/sparkpost/emails/emails.ex b/lib/code_corps/sparkpost/emails/emails.ex index 98d847859..8baf394fe 100644 --- a/lib/code_corps/sparkpost/emails/emails.ex +++ b/lib/code_corps/sparkpost/emails/emails.ex @@ -23,6 +23,10 @@ defmodule CodeCorps.SparkPost.Emails do project |> Emails.ProjectApproved.build |> API.send_transmission end + def send_project_user_acceptance_email(project_user) do + project_user |> Emails.ProjectUserAcceptance.build |> API.send_transmission + end + def send_receipt_email(charge, invoice) do case charge |> Emails.Receipt.build(invoice) do %SparkPost.Transmission{} = transmission -> diff --git a/lib/code_corps/sparkpost/emails/project_user_acceptance.ex b/lib/code_corps/sparkpost/emails/project_user_acceptance.ex new file mode 100644 index 000000000..c64a7caa7 --- /dev/null +++ b/lib/code_corps/sparkpost/emails/project_user_acceptance.ex @@ -0,0 +1,42 @@ +defmodule CodeCorps.SparkPost.Emails.ProjectUserAcceptance do + alias SparkPost.{Content, Transmission} + + alias CodeCorps.{ + Presenters.ImagePresenter, + Project, + ProjectUser, + Repo, + SparkPost.Emails.Recipient, + User, + WebClient + } + + @spec build(ProjectUser.t) :: %Transmission{} + def build(%ProjectUser{project: project, user: user}) do + %Transmission{ + content: %Content.TemplateRef{template_id: "project-user-acceptance"}, + options: %Transmission.Options{inline_css: true}, + recipients: [user |> Recipient.build], + substitution_data: %{ + from_name: "Code Corps", + from_email: "team@codecorps.org", + project_logo_url: ImagePresenter.large(project), + project_title: project.title, + project_url: project |> preload() |> url(), + subject: "#{project.title} just added you as a contributor", + user_first_name: user.first_name, + user_image_url: ImagePresenter.large(user) + } + } + end + + @spec preload(Project.t) :: Project.t + defp preload(%Project{} = project), do: project |> Repo.preload(:organization) + + @spec url(Project.t) :: String.t + defp url(project) do + WebClient.url() + |> URI.merge(project.organization.slug <> "/" <> project.slug) + |> URI.to_string + end +end diff --git a/lib/code_corps/sparkpost/sparkpost.ex b/lib/code_corps/sparkpost/sparkpost.ex index 13594b20c..dec38dd8d 100644 --- a/lib/code_corps/sparkpost/sparkpost.ex +++ b/lib/code_corps/sparkpost/sparkpost.ex @@ -9,6 +9,7 @@ defmodule CodeCorps.SparkPost do defdelegate send_organization_invite_email(invite), to: Emails defdelegate send_project_approval_request_email(project), to: Emails defdelegate send_project_approved_email(project), to: Emails + defdelegate send_project_user_acceptance_email(project_user), to: Emails defdelegate send_receipt_email(charge, invoice), to: Emails defdelegate send_reply_to_conversation_email(part, user), to: Emails end diff --git a/lib/code_corps_web/controllers/project_user_controller.ex b/lib/code_corps_web/controllers/project_user_controller.ex index f842f5bfb..dd59fe011 100644 --- a/lib/code_corps_web/controllers/project_user_controller.ex +++ b/lib/code_corps_web/controllers/project_user_controller.ex @@ -2,7 +2,7 @@ defmodule CodeCorpsWeb.ProjectUserController do @moduledoc false use CodeCorpsWeb, :controller - alias CodeCorps.{Emails, Helpers.Query, Mailer, ProjectUser, User} + alias CodeCorps.{Emails, Helpers.Query, Mailer, ProjectUser, SparkPost, User} action_fallback CodeCorpsWeb.FallbackController plug CodeCorpsWeb.Plug.DataToAttributes @@ -85,7 +85,6 @@ defmodule CodeCorpsWeb.ProjectUserController do defp send_acceptance_email(project_user) do project_user |> Repo.preload(@preloads) - |> Emails.ProjectUserAcceptanceEmail.create() - |> Mailer.deliver_now() + |> SparkPost.send_project_user_acceptance_email() end end diff --git a/test/lib/code_corps/emails/project_user_acceptance_email_test.exs b/test/lib/code_corps/emails/project_user_acceptance_email_test.exs deleted file mode 100644 index 6473a9dd3..000000000 --- a/test/lib/code_corps/emails/project_user_acceptance_email_test.exs +++ /dev/null @@ -1,25 +0,0 @@ -defmodule CodeCorps.Emails.ProjectUserAcceptanceEmailTest do - use CodeCorps.ModelCase - use Bamboo.Test - - alias CodeCorps.Emails.ProjectUserAcceptanceEmail - - test "acceptance email works" do - %{project: project, user: user} = project_user = insert(:project_user) - - email = ProjectUserAcceptanceEmail.create(project_user) - assert email.from == "Code Corps" - assert email.to == user.email - - template_model = email.private.template_model - - assert template_model == %{ - project_title: project.title, - project_url: "http://localhost:4200/#{project.organization.slug}/#{project.slug}", - project_logo_url: "#{Application.get_env(:code_corps, :asset_host)}/icons/project_default_large_.png", - user_image_url: "#{Application.get_env(:code_corps, :asset_host)}/icons/user_default_large_.png", - user_first_name: user.first_name, - subject: "#{project.title} just added you as a contributor" - } - end -end diff --git a/test/lib/code_corps/sparkpost/emails/project_user_acceptance_test.exs b/test/lib/code_corps/sparkpost/emails/project_user_acceptance_test.exs new file mode 100644 index 000000000..d81039da1 --- /dev/null +++ b/test/lib/code_corps/sparkpost/emails/project_user_acceptance_test.exs @@ -0,0 +1,38 @@ +defmodule CodeCorps.SparkPost.Emails.ProjectUserAcceptanceTest do + use CodeCorps.DbAccessCase + + alias CodeCorps.SparkPost.Emails.ProjectUserAcceptance + + describe "build/1" do + test "provides substitution data for all keys used by template" do + project_user = insert(:project_user) + + %{substitution_data: data} = ProjectUserAcceptance.build(project_user) + + expected_keys = + "project-user-acceptance" + |> CodeCorps.SparkPostHelpers.get_keys_used_by_template + assert data |> Map.keys == expected_keys + end + + test "builds correct transmission model" do + %{project: project, user: user} = project_user = insert(:project_user) + + %{substitution_data: data, recipients: [recipient]} = + ProjectUserAcceptance.build(project_user) + + assert data.from_name == "Code Corps" + assert data.from_email == "team@codecorps.org" + + assert data.project_title == project.title + assert data.project_url == "http://localhost:4200/#{project.organization.slug}/#{project.slug}" + assert data.project_logo_url == "#{Application.get_env(:code_corps, :asset_host)}/icons/project_default_large_.png" + assert data.user_image_url == "#{Application.get_env(:code_corps, :asset_host)}/icons/user_default_large_.png" + assert data.user_first_name == user.first_name + assert data.subject == "#{project.title} just added you as a contributor" + + assert recipient.address.email == user.email + assert recipient.address.name == user.first_name + end + end +end diff --git a/test/lib/code_corps_web/controllers/project_user_controller_test.exs b/test/lib/code_corps_web/controllers/project_user_controller_test.exs index b06d9ddc5..a7f2ed379 100644 --- a/test/lib/code_corps_web/controllers/project_user_controller_test.exs +++ b/test/lib/code_corps_web/controllers/project_user_controller_test.exs @@ -119,9 +119,9 @@ defmodule CodeCorpsWeb.ProjectUserControllerTest do CodeCorps.ProjectUser |> CodeCorps.Repo.get_by(role: "contributor") |> CodeCorps.Repo.preload([:project, :user]) - |> CodeCorps.Emails.ProjectUserAcceptanceEmail.create() + |> CodeCorps.SparkPost.Emails.ProjectUserAcceptance.build() - assert_delivered_email(email) + assert_received ^email end test "doesn't update and renders 401 when unauthenticated", %{conn: conn} do