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

Notify ninjas on their birthday #155

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d463c61
Notify ninjas on their birthday
pedrofp4444 Jan 13, 2023
9ec76c8
Reformulate functions
pedrofp4444 Jan 20, 2023
cc81943
Adjusted notify_ninja_birthday
pedrofp4444 Jan 20, 2023
db40a67
Corrections in lint the code
pedrofp4444 Jan 20, 2023
f86864c
Merge branch 'main' into pp/send-ninja-email-birthday
pedrofp4444 Feb 20, 2023
4859d6a
Add ninjas reference
pedrofp4444 Mar 4, 2023
247f43a
Update lib/bokken_web/templates/email/ninja_birthday.html.eex
pedrofp4444 Mar 4, 2023
2789b8e
Merge branch 'main' into pp/send-ninja-email-birthday
pedrofp4444 Mar 4, 2023
79550aa
Corrected ninja assign
pedrofp4444 Mar 4, 2023
b095f19
Merge branch 'pp/send-ninja-email-birthday' of github.com:coderdojobr…
pedrofp4444 Mar 4, 2023
b60e09f
Change module name
pedrofp4444 Mar 5, 2023
e3b7c6b
Format code
pedrofp4444 Mar 5, 2023
1f641d4
Config :quantum
pedrofp4444 Mar 5, 2023
25e927c
Config :quantum
pedrofp4444 Mar 5, 2023
14b0328
Merge branch 'main' into pp/send-ninja-email-birthday
pedrofp4444 Mar 7, 2023
037c24d
Fix :quantum config
pedrofp4444 Mar 7, 2023
4fa573f
Fix file name
pedrofp4444 Mar 7, 2023
abad75b
Merge branch 'pp/send-ninja-email-birthday' of github.com:coderdojobr…
pedrofp4444 Mar 7, 2023
0523303
Lint code
pedrofp4444 Mar 7, 2023
1646da1
Prepare new push
pedrofp4444 Mar 7, 2023
2261aec
Tests passing locally
pedrofp4444 Mar 7, 2023
d0f0500
Update lib/bokken/events/event_admin.ex
pedrofp4444 Mar 8, 2023
775591e
Change file name and improve code
pedrofp4444 Mar 10, 2023
6a9146a
Format code
pedrofp4444 Mar 10, 2023
3f4a3ba
Merge branch 'main' into pp/send-ninja-email-birthday
pedrofp4444 Mar 14, 2023
4b37245
Update lib/bokken/birthday_notifier.ex
pedrofp4444 Mar 14, 2023
6a0e9c8
Update lib/bokken/scheduler.ex
pedrofp4444 Mar 14, 2023
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
6 changes: 6 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

config :bokken, Bokken.Scheduler,
jobs: [
# Every midnight
{"@daily", {BirthdayNotifier, :notify_ninja_birthday, []}}
]

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
11 changes: 11 additions & 0 deletions lib/bokken/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ defmodule Bokken.Accounts do
[%Ninja{}, ...]

"""

def list_ninjas(preloads) when is_list(preloads) do
Ninja
|> Repo.all()
Expand Down Expand Up @@ -484,6 +485,16 @@ defmodule Bokken.Accounts do
end
end

def get_guardian_email_by_ninja(ninja: user) do
user =
user
|> Repo.preload(:ninja)

guardian = get_guardian!(user.ninja.guardian_id, [:user])

guardian.user.email
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
end

alias Bokken.Accounts.Organizer

@doc """
Expand Down
6 changes: 5 additions & 1 deletion lib/bokken/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ defmodule Bokken.Application do
# Start the PubSub system
{Phoenix.PubSub, name: Bokken.PubSub},
# Start the Endpoint (http/https)
BokkenWeb.Endpoint
BokkenWeb.Endpoint,
# Start a worker by calling: Bokken.Worker.start_link(arg)
# {Bokken.Worker, arg}
Bokken.BirthdayNotifier,
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
# Executes a task periodically to notify Ninjas on their birthday
Bokken.Scheduler
# Operates cronjob tasks using :quantum
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
17 changes: 17 additions & 0 deletions lib/bokken/events/event_admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Bokken.Events.EventAdmin do
The admin view config of events
"""
alias Bokken.{Events, Pairings}
alias Bokken.Mailer

def list_actions(_conn) do
[
Expand Down Expand Up @@ -46,4 +47,20 @@ defmodule Bokken.Events.EventAdmin do
enrollments_close: nil
]
end

def send_email(users, email) do
users
|> List.foldl(
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
%{success: [], fail: []},
fn user, accumulator ->
case Mailer.deliver(email.(user)) do
{:ok, _} ->
%{success: [user.email | accumulator[:success]], fail: accumulator[:fail]}

{:error, _} ->
%{success: [accumulator[:success]], fail: [user.email | accumulator[:fail]]}
end
end
)
end
end
26 changes: 26 additions & 0 deletions lib/bokken/periodically.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Bokken.BirthdayNotifier do
@moduledoc """
Executes a task periodically to notify Ninjas on their birthday.
"""

alias Bokken.Accounts
alias BokkenWeb.EventsEmails
import Bokken.Events.EventAdmin

defp notify_ninja_birthday do
ninjas = Accounts.list_ninjas([:user])
current_time = Date.utc_today()

ninjas
|> Enum.filter(fn ninja ->
ninja.birthday.month == current_time.month && ninja.birthday.day == current_time.day &&
not is_nil(ninja.user)
end)
|> Enum.map(fn ninja -> ninja.user end)
|> send_email(fn user ->
EventsEmails.ninja_birthday_email(user.ninja,
to: Accounts.get_guardian_email_by_ninja(ninja: user)
)
end)
end
end
3 changes: 3 additions & 0 deletions lib/bokken/scheduler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Bokken.Scheduler do
use Quantum, otp_app: :your_app
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
end
18 changes: 1 addition & 17 deletions lib/bokken_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule BokkenWeb.EventController do
alias Bokken.Accounts
alias Bokken.Events
alias Bokken.Events.Event
alias Bokken.Mailer
alias BokkenWeb.EventsEmails
import Bokken.Events.EventAdmin

action_fallback BokkenWeb.FallbackController

Expand Down Expand Up @@ -143,20 +143,4 @@ defmodule BokkenWeb.EventController do
|> render("emails.json", res)
end
end

defp send_email(users, email) do
users
|> List.foldl(
%{success: [], fail: []},
fn user, accumulator ->
case Mailer.deliver(email.(user)) do
{:ok, _} ->
%{success: [user.email | accumulator[:success]], fail: accumulator[:fail]}

{:error, _} ->
%{success: [accumulator[:success]], fail: [user.email | accumulator[:fail]]}
end
end
)
end
end
7 changes: 7 additions & 0 deletions lib/bokken_web/emails/event_emails.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ defmodule BokkenWeb.EventsEmails do
|> render_body(:mentor_event_reminder)
end

def ninja_birthday_email(ninja, to: email) do
base_email(to: email)
|> subject("[CoderDojo Braga] O Dojo deseja-te parabéns!")
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
|> assign(:ninja, ninja)
|> render_body(:ninja_birthday)
end

defp base_email(to: email) do
new()
|> from({"CoderDojo Braga", "noreply@coderdojobraga.org"})
Expand Down
72 changes: 72 additions & 0 deletions lib/bokken_web/templates/email/ninja_birthday.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<table class="wrapper" style="border-collapse: collapse;table-layout: fixed;min-width: 320px;width: 100%;background-color: #fff;" role="presentation" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div>
<div class="layout one-col fixed-width stack" style="Margin: 0 auto;max-width: 600px;min-width: 320px; width: 320px;width: calc(28000% - 167400px);overflow-wrap: break-word;word-wrap: break-word;word-break: break-word;">
<div class="layout__inner" style="border-collapse: collapse;display: table;width: 100%;background-color: #ffffff;">
<div class="column" style="text-align: left;color: #000;font-size: 16px;line-height: 24px;font-family: Avenir,sans-serif;">

<div style="Margin-left: 20px;Margin-right: 20px;">
<div style="mso-line-height-rule: exactly;line-height: 23px;font-size: 1px;">&nbsp;</div>
</div>

<div style="Margin-left: 20px;Margin-right: 20px;">
<div style="mso-line-height-rule: exactly;mso-text-raise: 11px;vertical-align: middle;">
<h1 style="Margin-top: 16px;Margin-bottom: 20px; text-align:center">
🥳 Aniversário 🥳
</h1>
</div>
</div>

<div style="Margin-left: 20px;Margin-right: 20px;">
<div style="mso-line-height-rule: exactly;mso-text-raise: 11px;vertical-align: middle;">
<p style="Margin-top: 16px;Margin-bottom: 20px; text-align:center">
Olá, <%= @ninja.first_name %>! 👋
</p>
</div>
</div>

<div style="Margin-left: 20px;Margin-right: 20px;">
<div style="mso-line-height-rule: exactly;mso-text-raise: 11px;vertical-align: middle;">
<p style="Margin-top: 16px;Margin-bottom: 20px;">
O CoderDojo Braga deseja-te um feliz aniversário! 🎂 Esperamos por ti na próxima sessão! 😄
</p>
</div>
</div>
</div>
</div>
</div>

<div style="mso-line-height-rule: exactly;line-height: 20px;font-size: 20px;">&nbsp;</div>

<div style="background-color: #ebebeb;">
<div class="layout one-col stack" style="Margin: 0 auto;max-width: 600px;min-width: 320px; width: 320px;width: calc(28000% - 167400px);overflow-wrap: break-word;word-wrap: break-word;word-break: break-word;">
<div class="layout__inner" style="border-collapse: collapse;display: table;width: 100%;">
<div class="column" style="text-align: left;color: #000;font-size: 16px;line-height: 24px;font-family: Avenir,sans-serif;">

<div style="Margin-left: 20px;Margin-right: 20px;">
<div style="mso-line-height-rule: exactly;line-height: 20px;font-size: 1px;">&nbsp;</div>
</div>

<div style="Margin-left: 20px;Margin-right: 20px;">
<div style="mso-line-height-rule: exactly;line-height: 3px;font-size: 1px;">&nbsp;</div>
</div>

<div style="font-size: 12px;font-style: normal;font-weight: normal;line-height: 19px;" align="center">
<a style="text-decoration: underline;transition: opacity 0.1s ease-in;color: #222d8f;" href="https://coderdojobraga.org/"><img style="border: 0;display: block;height: auto;width: 100%;max-width: 134px;" alt="CoderDojo Braga" src="https://coderdojo-braga.vercel.app/img/logo.svg" width="134"></a>
</div>

<div style="Margin-left: 20px;Margin-right: 20px;Margin-top: 20px;">
<div style="mso-line-height-rule: exactly;line-height: 1px;font-size: 1px;">&nbsp;</div>
</div>

</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
3 changes: 3 additions & 0 deletions lib/bokken_web/templates/email/ninja_birthday.text.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Olá, <%= @ninja.first_name %>! 👋

O CoderDojo Braga deseja-te um feliz aniversário! 🎂 Esperamos por ti na próxima sessão! 😄
5 changes: 4 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ defmodule Bokken.MixProject do
# tools
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.28", only: [:dev], runtime: false}
{:ex_doc, "~> 0.28", only: [:dev], runtime: false},

# periodically
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
{:quantum, "~> 3.0"}
]
end

Expand Down