Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert email to html part 2 #1267

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ex338_web/controllers/commish_email_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Ex338Web.CommishEmailController do

alias Ex338.FantasyLeagues.FantasyLeague
alias Ex338.Repo
alias Ex338Web.CommishEmail
alias Ex338Web.CommishNotifier

def new(conn, _params) do
render(
Expand All @@ -17,7 +17,7 @@ defmodule Ex338Web.CommishEmailController do
def create(conn, %{
"commish_email" => %{"leagues" => leagues, "subject" => subject, "message" => message}
}) do
result = CommishEmail.send_email_to_leagues(leagues, subject, message)
result = CommishNotifier.send_email_to_leagues(leagues, subject, message)

case result do
{:ok, _result} ->
Expand Down
13 changes: 3 additions & 10 deletions lib/ex338_web/controllers/trade_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ defmodule Ex338Web.TradeController do
alias Ex338.FantasyTeams
alias Ex338.Trades
alias Ex338Web.Authorization
alias Ex338Web.Mailer
alias Ex338Web.TradeEmail
alias Ex338Web.TradeNotifier

plug(
:load_and_authorize_resource,
Expand Down Expand Up @@ -70,10 +69,7 @@ defmodule Ex338Web.TradeController do
admin_emails = Accounts.get_admin_emails()
recipients = Enum.uniq(Trades.Trade.get_teams_emails(trade) ++ admin_emails)

conn
|> TradeEmail.propose(league, trade, recipients)
|> Mailer.deliver()
|> Mailer.handle_delivery()
TradeNotifier.propose(league, trade, recipients)

conn
|> put_flash(:info, "Trade submitted for approval.")
Expand Down Expand Up @@ -106,10 +102,7 @@ defmodule Ex338Web.TradeController do
admin_emails = Accounts.get_admin_emails()
recipients = Enum.uniq(Trades.Trade.get_teams_emails(trade) ++ admin_emails)

conn
|> TradeEmail.cancel(league, trade, recipients, team)
|> Mailer.deliver()
|> Mailer.handle_delivery()
TradeNotifier.cancel(league, trade, recipients, team)
end

conn
Expand Down
21 changes: 7 additions & 14 deletions lib/ex338_web/controllers/trade_vote_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ defmodule Ex338Web.TradeVoteController do
alias Ex338.FantasyTeams.FantasyTeam
alias Ex338.Trades
alias Ex338Web.Authorization
alias Ex338Web.Mailer
alias Ex338Web.TradeEmail
alias Ex338Web.TradeNotifier

plug(
:load_and_authorize_resource,
Expand All @@ -35,7 +34,7 @@ defmodule Ex338Web.TradeVoteController do
if trade.status == "Proposed" do
trade
|> Trades.maybe_update_for_league_vote()
|> maybe_send_trade_email(conn, team)
|> maybe_send_trade_email(team)
end

conn
Expand All @@ -53,28 +52,22 @@ defmodule Ex338Web.TradeVoteController do

# create

defp maybe_send_trade_email(%{status: "Rejected"} = trade, conn, team) do
defp maybe_send_trade_email(%{status: "Rejected"} = trade, team) do
league = trade.submitted_by_team.fantasy_league
admin_emails = Accounts.get_admin_emails()
recipients = Enum.uniq(Trades.Trade.get_teams_emails(trade) ++ admin_emails)

conn
|> TradeEmail.reject(league, trade, recipients, team)
|> Mailer.deliver()
|> Mailer.handle_delivery()
TradeNotifier.reject(league, trade, recipients, team)
end

defp maybe_send_trade_email(%{status: "Pending"} = trade, conn, _team) do
defp maybe_send_trade_email(%{status: "Pending"} = trade, _team) do
league = trade.submitted_by_team.fantasy_league
recipients = Accounts.get_league_and_admin_emails(league.id)

conn
|> TradeEmail.pending(league, trade, recipients)
|> Mailer.deliver()
|> Mailer.handle_delivery()
TradeNotifier.pending(league, trade, recipients)
end

defp maybe_send_trade_email(trade, _conn, _team), do: trade
defp maybe_send_trade_email(trade, _team), do: trade

defp parse_errors(errors) do
case Keyword.get(errors, :trade) do
Expand Down
49 changes: 0 additions & 49 deletions lib/ex338_web/emails/trade_email.ex

This file was deleted.

2 changes: 1 addition & 1 deletion lib/ex338_web/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Ex338Web.Mailer do
Logger.info("Sent email notification")
{:ok, email}

{:error, reason} ->
{:error, {_, reason}} ->
Logger.warning("Sending email failed: #{inspect(reason)}")
{:error, reason}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
defmodule Ex338Web.CommishEmail do
defmodule Ex338Web.CommishNotifier do
@moduledoc false

alias Ex338.Accounts.User
alias Ex338.FantasyTeams
alias Ex338.Repo
alias Ex338Web.EmailTemplate
alias Ex338Web.Mailer
alias Ex338Web.NotifierTemplate

def send_email_to_leagues(leagues, subject, message) do
owners = FantasyTeams.get_leagues_email_addresses(leagues)
Expand All @@ -24,7 +25,7 @@ defmodule Ex338Web.CommishEmail do
|> Enum.map(fn recipients ->
email_info
|> Map.put(:bcc, recipients)
|> EmailTemplate.plain_text()
|> NotifierTemplate.plain_text()
|> Mailer.deliver()
|> Mailer.handle_delivery()
end)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Ex338Web.EmailTemplate do
defmodule Ex338Web.NotifierTemplate do
@moduledoc false

import Swoosh.Email
Expand Down
149 changes: 149 additions & 0 deletions lib/ex338_web/notifiers/trade_notifier.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
defmodule Ex338Web.TradeNotifier do
@moduledoc false
use Ex338Web, :html

alias Ex338Web.Mailer

def cancel(league, trade, recipients, fantasy_team) do
subject = "#{fantasy_team.team_name} canceled its proposed 338 trade"
assigns = %{fantasy_team: fantasy_team, league: league, trade: trade}

email_body = ~H"""
<h3><%= @fantasy_team.team_name %> canceled its proposed trade</h3>
<.trade_table trade={@trade} />
<h3>Additional Terms:</h3>
<p>
<%= if @trade.additional_terms do %>
<%= @trade.additional_terms %>
<% else %>
None
<% end %>
</p>
...
"""

Mailer.build_and_deliver(recipients, subject, email_body)
end

def pending(league, trade, recipients) do
subject = "New 338 #{league.fantasy_league_name} Trade for Approval"
assigns = %{league: league, trade: trade}

email_body = ~H"""
<h3>The following trade is submitted for <%= @league.fantasy_league_name %> League approval:</h3>
<.trade_table trade={@trade} />
<h3>Additional Terms:</h3>
<p>
<%= if @trade.additional_terms do %>
<%= @trade.additional_terms %>
<% else %>
None
<% end %>
</p>
<br />
<p>
There will be a 3-day voting period for the league to approve the trade. A
no-response during the voting period will be considered an approval.
</p>
<h3>
Please go to the
<.link href={url(~p"/fantasy_leagues/#{@league.id}/trades")}>list of trades</.link>
to place your vote.
</h3>
...
"""

Mailer.build_and_deliver(recipients, subject, email_body)
end

def propose(league, trade, recipients) do
subject = "#{trade.submitted_by_team.team_name} proposed a 338 trade"
assigns = %{league: league, trade: trade}

email_body = ~H"""
<h3><%= @trade.submitted_by_team.team_name %> proposed the following trade:</h3>
<.trade_table trade={@trade} />
<h3>Additional Terms:</h3>
<p>
<%= if @trade.additional_terms do %>
<%= @trade.additional_terms %>
<% else %>
None
<% end %>
</p>
<br />
<h3>Instructions to Accept or Reject the proposed trade:</h3>
<p>
All participating teams must accept the trade before it is submitted to the league for approval. A "yes" vote will accept
the trade on behalf of your team. A "no" vote will reject the trade on behalf of your team.
</p>
<p>
Please go to the
<.link href={url(~p"/fantasy_leagues/#{@league.id}/trades")}>list of proposed trades</.link>
to accept or reject the trade.
</p>
...
"""

Mailer.build_and_deliver(recipients, subject, email_body)
end

def reject(league, trade, recipients, fantasy_team) do
subject = "Proposed trade rejected by #{fantasy_team.team_name}"
assigns = %{fantasy_team: fantasy_team, league: league, trade: trade}

email_body = ~H"""
<h3>
<%= @fantasy_team.team_name %> rejected the trade proposed by <%= @trade.submitted_by_team.team_name %>
</h3>
<.trade_table trade={@trade} />
<h3>Additional Terms:</h3>
<p>
<%= if @trade.additional_terms do %>
<%= @trade.additional_terms %>
<% else %>
None
<% end %>
</p>
...
"""

Mailer.build_and_deliver(recipients, subject, email_body)
end

defp trade_table(assigns) do
~H"""
<table>
<thead>
<tr>
<th>Gaining Team</th>
<th>Fantasy Player/Future Pick</th>
<th>Losing Team</th>
</tr>
</thead>
<tbody>
<%= for line_item <- @trade.trade_line_items do %>
<tr>
<td>
<%= line_item.gaining_team.team_name %>
</td>
<td>
<%= if(line_item.fantasy_player) do %>
<%= display_player(line_item.fantasy_player) %>
<% else %>
<%= display_future_pick(line_item.future_pick) %>
<% end %>
</td>
<td>
<%= line_item.losing_team.team_name %>
</td>
</tr>
<% end %>
</tbody>
</table>
"""
end

def display_player(%{player_name: name, sports_league: %{abbrev: abbrev}}),
do: "#{String.trim(name)}, #{abbrev}"
end
10 changes: 0 additions & 10 deletions lib/ex338_web/templates/email/canceled_trade.html.eex

This file was deleted.

18 changes: 0 additions & 18 deletions lib/ex338_web/templates/email/pending_trade.html.eex

This file was deleted.

19 changes: 0 additions & 19 deletions lib/ex338_web/templates/email/proposed_trade.html.eex

This file was deleted.

Loading
Loading