Skip to content

Commit 9021c60

Browse files
committed
refactor: simplify email delivery function by consolidating parameters into a single opts map
1 parent d333abf commit 9021c60

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

lib/algora/activities/jobs/send_campaign_email.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ defmodule Algora.Activities.Jobs.SendCampaignEmail do
2323
}) do
2424
recipient = Algora.Util.base64_to_term!(encoded_recipient)
2525
template_params = Algora.Util.base64_to_term!(encoded_template_params)
26-
from = {from_name, from_email}
2726
token = Algora.Admin.token!()
2827

2928
with {:ok, repo} <- Workspace.ensure_repository(token, recipient["repo_owner"], recipient["repo_name"]),
3029
{:ok, _owner} <- Workspace.ensure_user(token, recipient["repo_owner"]),
3130
{:ok, _contributors} <- Workspace.ensure_contributors(token, repo),
3231
{:ok, _languages} <- Workspace.ensure_repo_tech_stack(token, repo) do
33-
CampaignLive.deliver_email(recipient, subject, template_params, from, preheader: preheader)
32+
CampaignLive.deliver_email(
33+
recipient: recipient,
34+
subject: subject,
35+
template_params: template_params,
36+
from: {from_name, from_email},
37+
preheader: preheader
38+
)
3439
end
3540
end
3641
end

lib/algora_web/live/admin/campaign_live.ex

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -331,49 +331,38 @@ defmodule AlgoraWeb.Admin.CampaignLive do
331331
end)
332332
end
333333

334-
@spec deliver_email(
335-
recipient :: map(),
336-
subject :: String.t(),
337-
template_params :: Keyword.t(),
338-
from :: {String.t(), String.t()},
339-
opts :: Keyword.t()
340-
) ::
341-
{:ok, term} | {:error, term}
342-
def deliver_email(recipient, subject, template_params, from, opts \\ []) do
334+
def deliver_email(opts) do
335+
recipient = opts[:recipient]
336+
343337
case :get
344338
|> Finch.build("https://algora.io/og/go/#{recipient["repo_owner"]}/#{recipient["repo_name"]}")
345339
|> Finch.request(Algora.Finch) do
346340
{:ok, %Finch.Response{status: status, body: body}} when status in 200..299 ->
347-
deliver(
348-
recipient["email"],
349-
subject,
350-
template_params,
351-
from,
352-
[
353-
Swoosh.Attachment.new({:data, body},
354-
filename: "#{recipient["repo_owner"]}.png",
355-
content_type: "image/png",
356-
type: :inline
357-
)
358-
],
359-
opts
360-
)
341+
opts
342+
|> Keyword.put(:attachments, [
343+
Swoosh.Attachment.new({:data, body},
344+
filename: "#{recipient["repo_owner"]}.png",
345+
content_type: "image/png",
346+
type: :inline
347+
)
348+
])
349+
|> deliver()
361350

362351
{:error, reason} ->
363352
raise reason
364353
end
365354
end
366355

367-
defp deliver(to, subject, template_params, from, attachments, opts \\ []) do
356+
defp deliver(opts) do
368357
email =
369358
Email.new()
370-
|> Email.to(to)
371-
|> Email.from(from)
372-
|> Email.subject(subject)
373-
|> Email.text_body(Mailer.text_template(template_params))
374-
|> Email.html_body(Mailer.html_template(template_params, preheader: opts[:preheader]))
359+
|> Email.to(opts[:recipient]["email"])
360+
|> Email.from(opts[:from])
361+
|> Email.subject(opts[:subject])
362+
|> Email.text_body(Mailer.text_template(opts[:template_params]))
363+
|> Email.html_body(Mailer.html_template(opts[:template_params], preheader: opts[:preheader]))
375364

376-
email = Enum.reduce(attachments, email, fn attachment, acc -> Email.attachment(acc, attachment) end)
365+
email = Enum.reduce(opts[:attachments], email, fn attachment, acc -> Email.attachment(acc, attachment) end)
377366

378367
Mailer.deliver_with_logging(email)
379368
end

0 commit comments

Comments
 (0)