Skip to content

Commit 3cc1911

Browse files
committed
Switch ReplyToConversation email to SparkPost
1 parent 5f38d0e commit 3cc1911

File tree

8 files changed

+142
-102
lines changed

8 files changed

+142
-102
lines changed

lib/code_corps/emails/reply_to_conversation_email.ex

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/code_corps/messages/emails.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ defmodule CodeCorps.Messages.Emails do
44
"""
55
alias CodeCorps.{
66
ConversationPart,
7-
Emails,
8-
Mailer,
97
Message,
108
Repo,
119
SparkPost
@@ -54,8 +52,7 @@ defmodule CodeCorps.Messages.Emails do
5452
defp send_reply_to_conversation_emails(%ConversationPart{} = part) do
5553
part
5654
|> get_conversation_participants()
57-
|> Enum.map(&Emails.ReplyToConversationEmail.create(part, &1))
58-
|> Enum.each(&Mailer.deliver_now/1)
55+
|> Enum.each(&SparkPost.send_reply_to_conversation_email(part, &1))
5956
end
6057

6158
@spec get_conversation_participants(ConversationPart.t) :: list(User.t)

lib/code_corps/sparkpost/emails/emails.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ defmodule CodeCorps.SparkPost.Emails do
2222
build_failure -> build_failure
2323
end
2424
end
25+
26+
def send_reply_to_conversation_email(part, user) do
27+
part |> Emails.ReplyToConversation.build(user) |> API.send_transmission
28+
end
2529
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
defmodule CodeCorps.SparkPost.Emails.ReplyToConversation do
2+
alias SparkPost.{Content, Transmission}
3+
alias CodeCorps.{
4+
Conversation,
5+
ConversationPart,
6+
Message,
7+
Organization,
8+
Project,
9+
SparkPost.Emails.Recipient,
10+
User,
11+
WebClient
12+
}
13+
14+
@spec build(ConversationPart.t, User.t) :: %Transmission{}
15+
def build(
16+
%ConversationPart{
17+
author: %User{} = author,
18+
conversation: %Conversation{
19+
message: %Message{
20+
project: %Project{} = project
21+
}
22+
} = conversation
23+
},
24+
%User{} = user) do
25+
26+
%Transmission{
27+
content: %Content.TemplateRef{template_id: "reply-to-conversation"},
28+
options: %Transmission.Options{inline_css: true},
29+
recipients: [user |> Recipient.build],
30+
substitution_data: %{
31+
author_name: author.first_name,
32+
from_name: "Code Corps",
33+
from_email: "team@codecorps.org",
34+
conversation_url: project |> conversation_url(conversation),
35+
name: user |> get_name(),
36+
project_title: project.title,
37+
subject: "#{author.first_name} replied to your conversation in #{project.title}"
38+
}
39+
}
40+
end
41+
42+
@spec conversation_url(Project.t, Conversation.t) :: String.t
43+
defp conversation_url(
44+
%Project{organization: %Organization{slug: slug}, slug: project_slug},
45+
%Conversation{id: id}) do
46+
47+
WebClient.url()
48+
|> URI.merge("#{slug}/#{project_slug}/conversations/#{id}")
49+
|> URI.to_string
50+
end
51+
52+
@spec get_name(User.t) :: String.t
53+
defp get_name(%User{first_name: nil}), do: "there"
54+
defp get_name(%User{first_name: name}), do: name
55+
end

lib/code_corps/sparkpost/sparkpost.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ defmodule CodeCorps.SparkPost do
88
defdelegate send_message_initiated_by_project_email(message, conversation), to: Emails
99
defdelegate send_organization_invite_email(invite), to: Emails
1010
defdelegate send_receipt_email(charge, invoice), to: Emails
11+
defdelegate send_reply_to_conversation_email(part, user), to: Emails
1112
end

test/lib/code_corps/emails/reply_to_conversation_email_test.exs

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/lib/code_corps/messages/messages_test.exs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ defmodule CodeCorps.MessagesTest do
88
import Ecto.Query, only: [where: 2]
99

1010
alias CodeCorps.{
11-
Conversation, ConversationPart, Emails, Message, Messages,
12-
SparkPost.Emails.MessageInitiatedByProject
11+
Conversation, ConversationPart, Message, Messages,
12+
SparkPost.Emails.MessageInitiatedByProject,
13+
SparkPost.Emails.ReplyToConversation
1314
}
1415
alias Ecto.Changeset
1516

@@ -302,10 +303,14 @@ defmodule CodeCorps.MessagesTest do
302303

303304
part = part |> Repo.preload([:author, conversation: [message: [[project: :organization]]]])
304305

305-
refute_delivered_email Emails.ReplyToConversationEmail.create(part, part_author)
306-
assert_delivered_email Emails.ReplyToConversationEmail.create(part, target_user)
307-
assert_delivered_email Emails.ReplyToConversationEmail.create(part, message_author)
308-
assert_delivered_email Emails.ReplyToConversationEmail.create(part, other_participant)
306+
part_author_email = ReplyToConversation.build(part, part_author)
307+
target_user_email = ReplyToConversation.build(part, target_user)
308+
message_author_email = ReplyToConversation.build(part, message_author)
309+
other_participant_email = ReplyToConversation.build(part, other_participant)
310+
refute_received ^part_author_email
311+
assert_received ^target_user_email
312+
assert_received ^message_author_email
313+
assert_received ^other_participant_email
309314
end
310315

311316
test "when replied by conversation user, sends appropriate email to other participants" do
@@ -324,10 +329,14 @@ defmodule CodeCorps.MessagesTest do
324329

325330
part = part |> Repo.preload([:author, conversation: [message: [[project: :organization]]]])
326331

327-
refute_delivered_email Emails.ReplyToConversationEmail.create(part, part_author)
328-
assert_delivered_email Emails.ReplyToConversationEmail.create(part, target_user)
329-
assert_delivered_email Emails.ReplyToConversationEmail.create(part, message_author)
330-
assert_delivered_email Emails.ReplyToConversationEmail.create(part, other_participant)
332+
part_author_email = ReplyToConversation.build(part, part_author)
333+
target_user_email = ReplyToConversation.build(part, target_user)
334+
message_author_email = ReplyToConversation.build(part, message_author)
335+
other_participant_email = ReplyToConversation.build(part, other_participant)
336+
refute_received ^part_author_email
337+
assert_received ^target_user_email
338+
assert_received ^message_author_email
339+
assert_received ^other_participant_email
331340
end
332341
end
333342

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
defmodule CodeCorps.SparkPost.Emails.ReplyToConversationTest do
2+
use CodeCorps.DbAccessCase
3+
4+
alias CodeCorps.{SparkPost.Emails.ReplyToConversation, WebClient}
5+
6+
describe "build/1" do
7+
test "provides substitution data for all keys used by template" do
8+
message = insert(:message)
9+
10+
preloads = [:author, conversation: [message: [[project: :organization]]]]
11+
12+
conversation = insert(:conversation, message: message)
13+
14+
conversation_part =
15+
:conversation_part
16+
|> insert(conversation: conversation)
17+
|> Repo.preload(preloads)
18+
19+
user = insert(:user)
20+
21+
%{substitution_data: data} =
22+
ReplyToConversation.build(conversation_part, user)
23+
24+
expected_keys =
25+
"reply-to-conversation"
26+
|> CodeCorps.SparkPostHelpers.get_keys_used_by_template
27+
assert data |> Map.keys == expected_keys
28+
end
29+
30+
test "builds correct transmission model" do
31+
message = insert(:message)
32+
33+
preloads = [:author, conversation: [message: [[project: :organization]]]]
34+
35+
conversation = insert(:conversation, message: message)
36+
37+
conversation_part =
38+
:conversation_part
39+
|> insert(conversation: conversation)
40+
|> Repo.preload(preloads)
41+
42+
%{project: %{organization: %{slug: slug}, slug: project_slug} = project} = message
43+
44+
user = insert(:user)
45+
46+
%{substitution_data: data, recipients: [recipient]} =
47+
ReplyToConversation.build(conversation_part, user)
48+
49+
assert data.from_name == "Code Corps"
50+
assert data.from_email == "team@codecorps.org"
51+
52+
assert data.author_name == conversation_part.author.first_name
53+
assert data.conversation_url == "#{WebClient.url()}/#{slug}/#{project_slug}/conversations/#{conversation.id}"
54+
assert data.name == user.first_name
55+
assert data.project_title == project.title
56+
assert data.subject == "#{conversation_part.author.first_name} replied to your conversation in #{project.title}"
57+
58+
assert recipient.address.email == user.email
59+
assert recipient.address.name == user.first_name
60+
end
61+
end
62+
end

0 commit comments

Comments
 (0)