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 1 commit
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 lib/bokken/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ defmodule Bokken.Accounts do
|> Repo.all()
end

def list_ninjas_preload(args) do
Ninja
|> Repo.all()
|> Repo.preload(args)
end

@doc """
Gets a single ninja.

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 an endpoint periodically
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
24 changes: 24 additions & 0 deletions lib/bokken/periodically.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Bokken.Periodically do
use GenServer
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved

alias BokkenWeb.NinjaController

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
NinjaController.notify_ninja_birthday()
schedule_work()
{:noreply, state}
end

defp schedule_work() do
Process.send_after(self(), :work, 24 * 60 * 60 * 1000)
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
end
end
27 changes: 27 additions & 0 deletions lib/bokken_web/controllers/ninja_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ defmodule BokkenWeb.NinjaController do

alias Bokken.Accounts
alias Bokken.Accounts.Ninja
alias BokkenWeb.EventsEmails
alias Bokken.Events.Event
alias Bokken.Events.Lecture
alias Bokken.Events.TeamNinja
alias Bokken.Mailer

action_fallback BokkenWeb.FallbackController

Expand Down Expand Up @@ -85,4 +87,29 @@ defmodule BokkenWeb.NinjaController do
send_resp(conn, :no_content, "")
end
end

def notify_ninja_birthday() do
ninjas = Accounts.list_ninjas_preload([:user])
current_time = DateTime.utc_now()
ninjas
|> Enum.map(fn ninja -> (ninja.birthday == current_time)
pedrofp4444 marked this conversation as resolved.
Show resolved Hide resolved
send_email(ninja, EventsEmails.ninja_birthday_email(to: ninja.user.email))
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
8 changes: 8 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,14 @@ defmodule BokkenWeb.EventsEmails do
|> render_body(:mentor_event_reminder)
end

def ninja_birthday_email(to: email) do
frontend_url = Application.fetch_env!(:bokken, BokkenWeb.Endpoint)[:frontend_url]

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

defp base_email(to: email) do
new()
|> from({"CoderDojo Braga", "[email protected]"})
Expand Down
2 changes: 2 additions & 0 deletions lib/bokken_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ defmodule BokkenWeb.Router do
post "/notify_signup", EventController, :notify_signup
post "/notify_selected", EventController, :notify_selected

post "/notify_ninja_birthday", NinjaController, :notify_ninja_birthday

resources "/bot", BotController, except: [:new, :edit]
end

Expand Down
Empty file.
Empty file.