Skip to content

Commit

Permalink
Restructure Emails context
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Dec 22, 2017
1 parent 3919903 commit e9f1961
Show file tree
Hide file tree
Showing 40 changed files with 138 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.SparkPost.API do
defmodule CodeCorps.Emails.API do
@moduledoc ~S"""
A wrapper for the SparkPost API.
Expand Down
56 changes: 56 additions & 0 deletions lib/code_corps/emails/emails.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule CodeCorps.Emails do
alias CodeCorps.Emails.{API, Tasks, Transmissions}

defdelegate create_templates, to: Tasks
defdelegate update_templates, to: Tasks

def send_forgot_password_email(user, token) do
user |> Transmissions.ForgotPassword.build(token) |> API.send_transmission
end

def send_message_initiated_by_project_email(message, conversation) do
message
|> Transmissions.MessageInitiatedByProject.build(conversation)
|> API.send_transmission
end

def send_organization_invite_email(invite) do
invite |> Transmissions.OrganizationInvite.build |> API.send_transmission
end

def send_project_approval_request_email(project) do
project
|> Transmissions.ProjectApprovalRequest.build
|> API.send_transmission
end

def send_project_approved_email(project) do
project |> Transmissions.ProjectApproved.build |> API.send_transmission
end

def send_project_user_acceptance_email(project_user) do
project_user
|> Transmissions.ProjectUserAcceptance.build
|> API.send_transmission
end

def send_project_user_request_email(project_user) do
project_user
|> Transmissions.ProjectUserRequest.build
|> API.send_transmission
end

def send_receipt_email(charge, invoice) do
case charge |> Transmissions.Receipt.build(invoice) do
%SparkPost.Transmission{} = transmission ->
transmission |> API.send_transmission
build_failure -> build_failure
end
end

def send_reply_to_conversation_email(part, user) do
part
|> Transmissions.ReplyToConversation.build(user)
|> API.send_transmission
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.SparkPost.ExtendedAPI do
defmodule CodeCorps.Emails.ExtendedAPI do
@moduledoc """
Serves as an extension of the SparkPost API provided by the elixir-sparkpost
library, for features the external package does not support yet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.SparkPost.Emails.Recipient do
defmodule CodeCorps.Emails.Recipient do
@moduledoc ~S"""
In charge of adapting `CodeCorps.User` data into SparkPost recipient data.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.SparkPost.Tasks do
defmodule CodeCorps.Emails.Tasks do
@moduledoc ~S"""
Subcontext holding all commonly performed Tasks related to SparkPost
"""
Expand All @@ -15,7 +15,7 @@ defmodule CodeCorps.SparkPost.Tasks do
"reply-to-conversation"
]

alias CodeCorps.SparkPost.API
alias CodeCorps.Emails.API

@doc ~S"""
Builds a stream, which, when evaluated, makes API requests to create all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule CodeCorps.SparkPost.Emails.ForgotPassword do
defmodule CodeCorps.Emails.Transmissions.ForgotPassword do

alias SparkPost.{Content, Transmission}
alias CodeCorps.{SparkPost.Emails.Recipient, User, WebClient}
alias CodeCorps.{Emails.Recipient, User, WebClient}

@spec build(User.t, String.t) :: %Transmission{}
def build(%User{} = user, token) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule CodeCorps.SparkPost.Emails.MessageInitiatedByProject do
defmodule CodeCorps.Emails.Transmissions.MessageInitiatedByProject do
alias SparkPost.{Content, Transmission}
alias CodeCorps.{
Conversation,
Message,
Project,
SparkPost.Emails.Recipient,
Emails.Recipient,
User,
WebClient
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule CodeCorps.SparkPost.Emails.OrganizationInvite do
defmodule CodeCorps.Emails.Transmissions.OrganizationInvite do
alias SparkPost.{Content, Transmission}
alias CodeCorps.{OrganizationInvite, SparkPost.Emails.Recipient, WebClient}
alias CodeCorps.{OrganizationInvite, Emails.Recipient, WebClient}

@spec build(OrganizationInvite.t) :: %Transmission{}
def build(%OrganizationInvite{} = invite) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule CodeCorps.SparkPost.Emails.ProjectApprovalRequest do
defmodule CodeCorps.Emails.Transmissions.ProjectApprovalRequest do
import Ecto.Query, only: [where: 3]

alias SparkPost.{Content, Transmission}
alias CodeCorps.{
Presenters.ImagePresenter,
Project,
Repo,
SparkPost.Emails.Recipient,
Emails.Recipient,
User,
WebClient
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule CodeCorps.SparkPost.Emails.ProjectApproved do
defmodule CodeCorps.Emails.Transmissions.ProjectApproved do
import Ecto.Query

alias SparkPost.{Content, Transmission}
alias CodeCorps.{
Project, ProjectUser, Repo, SparkPost.Emails.Recipient, User, WebClient
Project, ProjectUser, Repo, Emails.Recipient, User, WebClient
}

@spec build(Project.t) :: %Transmission{}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule CodeCorps.SparkPost.Emails.ProjectUserAcceptance do
defmodule CodeCorps.Emails.Transmissions.ProjectUserAcceptance do
alias SparkPost.{Content, Transmission}

alias CodeCorps.{
Presenters.ImagePresenter,
Project,
ProjectUser,
Repo,
SparkPost.Emails.Recipient,
Emails.Recipient,
User,
WebClient
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.SparkPost.Emails.ProjectUserRequest do
defmodule CodeCorps.Emails.Transmissions.ProjectUserRequest do
import Ecto.Query

alias SparkPost.{Content, Transmission}
Expand All @@ -7,7 +7,7 @@ defmodule CodeCorps.SparkPost.Emails.ProjectUserRequest do
Project,
ProjectUser,
Repo,
SparkPost.Emails.Recipient,
Emails.Recipient,
User,
WebClient
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule CodeCorps.SparkPost.Emails.Receipt do
defmodule CodeCorps.Emails.Transmissions.Receipt do

alias SparkPost.{Content, Transmission}
alias CodeCorps.{
DonationGoal,
Project,
Repo,
SparkPost.Emails.Recipient,
Emails.Recipient,
StripeConnectCharge,
StripeConnectSubscription,
User,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule CodeCorps.SparkPost.Emails.ReplyToConversation do
defmodule CodeCorps.Emails.Transmissions.ReplyToConversation do
alias SparkPost.{Content, Transmission}
alias CodeCorps.{
Conversation,
ConversationPart,
Message,
Organization,
Project,
SparkPost.Emails.Recipient,
Emails.Recipient,
User,
WebClient
}
Expand Down
8 changes: 4 additions & 4 deletions lib/code_corps/messages/emails.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ defmodule CodeCorps.Messages.Emails do
"""
alias CodeCorps.{
ConversationPart,
Emails,
Message,
Repo,
SparkPost
Repo
}

@message_preloads [:project, [conversations: :user]]
Expand All @@ -23,7 +23,7 @@ defmodule CodeCorps.Messages.Emails do

message
|> Map.get(:conversations)
|> Enum.each(&SparkPost.send_message_initiated_by_project_email(message, &1))
|> Enum.each(&Emails.send_message_initiated_by_project_email(message, &1))
end

@part_preloads [
Expand Down Expand Up @@ -52,7 +52,7 @@ defmodule CodeCorps.Messages.Emails do
defp send_reply_to_conversation_emails(%ConversationPart{} = part) do
part
|> get_conversation_participants()
|> Enum.each(&SparkPost.send_reply_to_conversation_email(part, &1))
|> Enum.each(&Emails.send_reply_to_conversation_email(part, &1))
end

@spec get_conversation_participants(ConversationPart.t) :: list(User.t)
Expand Down
6 changes: 3 additions & 3 deletions lib/code_corps/projects/projects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule CodeCorps.Projects do
import CodeCorpsWeb.ProjectController, only: [preload: 1]

alias CodeCorps.{
Analytics.SegmentTracker, Project, Repo, SparkPost, User
Analytics.SegmentTracker, Emails, Project, Repo, User
}
alias Ecto.Changeset

Expand Down Expand Up @@ -85,7 +85,7 @@ defmodule CodeCorps.Projects do
defp send_approval_request_email(project) do
project
|> preload()
|> SparkPost.send_project_approval_request_email()
|> Emails.send_project_approval_request_email()
end

@spec maybe_send_approved_email(Project.t, Project.t) :: any
Expand All @@ -100,6 +100,6 @@ defmodule CodeCorps.Projects do
defp send_approved_email(project) do
project
|> preload()
|> SparkPost.send_project_approved_email()
|> Emails.send_project_approved_email()
end
end
4 changes: 2 additions & 2 deletions lib/code_corps/services/forgot_password.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule CodeCorps.Services.ForgotPasswordService do
# credo:disable-for-this-file Credo.Check.Refactor.PipeChainStart

alias CodeCorps.{AuthToken, Repo, SparkPost, User}
alias CodeCorps.{AuthToken, Emails, Repo, User}

@doc"""
Generates an AuthToken model and sends to the provided email.
Expand All @@ -10,7 +10,7 @@ defmodule CodeCorps.Services.ForgotPasswordService do
with %User{} = user <- Repo.get_by(User, email: email),
{:ok, %AuthToken{value: token}} <- AuthToken.changeset(%AuthToken{}, user) |> Repo.insert
do
user |> SparkPost.send_forgot_password_email(token)
user |> Emails.send_forgot_password_email(token)
else
nil -> nil
end
Expand Down
45 changes: 0 additions & 45 deletions lib/code_corps/sparkpost/emails/emails.ex

This file was deleted.

16 changes: 0 additions & 16 deletions lib/code_corps/sparkpost/sparkpost.ex

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule CodeCorps.StripeService.Events.ConnectChargeSucceeded do

alias SparkPost.Transmission
alias CodeCorps.{
SparkPost,
Emails,
StripeService.StripeConnectChargeService,
StripeConnectCharge
}
Expand All @@ -28,7 +28,7 @@ defmodule CodeCorps.StripeService.Events.ConnectChargeSucceeded do

defp try_send_receipt(%StripeConnectCharge{invoice_id_from_stripe: invoice_id} = charge, account_id) do
with {:ok, %Stripe.Invoice{} = invoice} <- @api.Invoice.retrieve(invoice_id, connect_account: account_id),
{:ok, %Transmission.Response{} = response} <- charge |> SparkPost.send_receipt_email(invoice)
{:ok, %Transmission.Response{} = response} <- charge |> Emails.send_receipt_email(invoice)
do
{:ok, charge, response}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule CodeCorpsWeb.OrganizationInviteController do
@moduledoc false
use CodeCorpsWeb, :controller

alias CodeCorps.{Helpers.Query, OrganizationInvite, SparkPost, User}
alias CodeCorps.{Emails, Helpers.Query, OrganizationInvite, User}

action_fallback CodeCorpsWeb.FallbackController
plug CodeCorpsWeb.Plug.DataToAttributes
Expand All @@ -28,7 +28,7 @@ defmodule CodeCorpsWeb.OrganizationInviteController do
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %OrganizationInvite{}, params),
{:ok, %OrganizationInvite{} = organization_invite} <- %OrganizationInvite{} |> OrganizationInvite.create_changeset(params) |> Repo.insert do

organization_invite |> SparkPost.send_organization_invite_email
organization_invite |> Emails.send_organization_invite_email

conn
|> put_status(:created)
Expand Down
Loading

0 comments on commit e9f1961

Please sign in to comment.