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 4 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
19 changes: 16 additions & 3 deletions lib/bokken/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,11 @@ defmodule Bokken.Accounts do

"""

@spec list_ninjas(map()) :: list(Ninja.t())
def list_ninjas(args \\ %{})
def list_ninjas(preloads) when is_list(preloads) do
Ninja
|> Repo.all()
|> Repo.preload(preloads)
end

def list_ninjas(%{"team_id" => team_id}) do
team_id
Expand All @@ -253,7 +256,7 @@ defmodule Bokken.Accounts do
|> Map.fetch!(:ninjas)
end

def list_ninjas(_args) do
def list_ninjas do
Ninja
|> Repo.all()
end
Expand Down Expand Up @@ -380,6 +383,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
4 changes: 3 additions & 1 deletion lib/bokken/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ 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.Periodically
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
# Executes a task periodically
]

# 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
48 changes: 48 additions & 0 deletions lib/bokken/periodically.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule Bokken.Periodically do
@moduledoc """
Executes a task periodically.
"""
use GenServer
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved

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

@one_day 24 * 60 * 60 * 1000

def start_link(_opts) do
GenServer.start_link(__MODULE__, %{})
end

def init(state) do
schedule_work()
{:ok, state}
end

def handle_info(:work, state) do
notify_ninja_birthday()
schedule_work()
{:noreply, state}
end

defp schedule_work do
Process.send_after(self(), :work, @one_day)
end

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
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 @@ -54,6 +54,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", "[email protected]"})
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á! 👋
</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 dia feliz e um ótimo aniversário. Esperamos por ti na pŕoxima sessão! 😄
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
</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á! 👋

O CoderDojo Braga deseja-te um dia feliz e um ótimo aniversário. Esperamos por ti na próxima sessão! 😄
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved