Skip to content

Commit

Permalink
Switch ProjectUserAcceptance email to SparkPost
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Dec 22, 2017
1 parent ad53de0 commit 14dd68d
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 70 deletions.
40 changes: 0 additions & 40 deletions lib/code_corps/emails/project_user_acceptance_email.ex

This file was deleted.

4 changes: 4 additions & 0 deletions lib/code_corps/sparkpost/emails/emails.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
42 changes: 42 additions & 0 deletions lib/code_corps/sparkpost/emails/project_user_acceptance.ex
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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
1 change: 1 addition & 0 deletions lib/code_corps/sparkpost/sparkpost.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions lib/code_corps_web/controllers/project_user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
25 changes: 0 additions & 25 deletions test/lib/code_corps/emails/project_user_acceptance_email_test.exs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 == "[email protected]"

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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 14dd68d

Please sign in to comment.