-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into ghatighroias/fix_test
- Loading branch information
Showing
28 changed files
with
298 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule CoursePlanner.Notifier.Server do | ||
@moduledoc false | ||
use GenServer | ||
alias CoursePlanner.{Mailer, Mailer.UserEmail, Notification, Repo, Users} | ||
require Logger | ||
|
||
@spec handle_cast({atom(), Notification.t} | atom(), any()) :: {:noreply, any()} | ||
def handle_cast({:send_email, notification}, state) do | ||
email = UserEmail.build_email(notification) | ||
case Mailer.deliver(email) do | ||
{:ok, _} -> | ||
{:noreply, state} | ||
{:error, reason} -> | ||
Logger.error("Email delivery failed: #{Kernel.inspect reason}") | ||
{:noreply, [{:error, reason, email} | state]} | ||
end | ||
end | ||
def handle_cast({:save_email, notification}, state) do | ||
changeset = Notification.changeset(notification) | ||
case Repo.insert(changeset) do | ||
{:ok, _} -> | ||
{:noreply, state} | ||
{:error, %{errors: errors, data: email}} -> | ||
Logger.error("Email saving failed: #{Kernel.inspect errors}") | ||
{:noreply, [{:error, errors, email} | state]} | ||
end | ||
end | ||
def handle_cast({:notify_all, user}, state) do | ||
email = UserEmail.build_summary(user) | ||
case Mailer.deliver(email) do | ||
{:ok, _} -> | ||
Users.update_notifications(user) | ||
{:noreply, state} | ||
{:error, reason} -> | ||
Logger.error("Email delivery failed: #{Kernel.inspect reason}") | ||
{:noreply, [{:error, reason, email} | state]} | ||
end | ||
end | ||
def handle_cast(_, state), do: {:noreply, state} | ||
|
||
@doc """ | ||
This function is used to suppress unhandled message warnings from `Swoosh.Adapters.Test` during | ||
unit tests | ||
""" | ||
@spec handle_info(any(), any()) :: {:noreply, any()} | ||
def handle_info(_, state), do: {:noreply, state} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule CoursePlanner.NotifierScheduler do | ||
@moduledoc false | ||
use Quantum.Scheduler, | ||
otp_app: :course_planner | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
priv/repo/migrations/20170803180536_add_notification_configuration.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule CoursePlanner.Repo.Migrations.AddNotificationConfiguration do | ||
use Ecto.Migration | ||
|
||
def up do | ||
alter table(:users) do | ||
add :notified_at, :naive_datetime | ||
add :notification_period_days, :integer, null: false, default: 1 | ||
end | ||
end | ||
|
||
def down do | ||
alter table(:users) do | ||
remove :notified_at | ||
remove :notification_period_days | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule CoursePlanner.NotificationsTest do | ||
use CoursePlanner.ModelCase | ||
doctest CoursePlanner.Notifications | ||
|
||
import CoursePlanner.Factory | ||
|
||
alias CoursePlanner.Notifications | ||
|
||
test "send notification when it's day after" do | ||
now = Timex.now() | ||
|
||
user1 = insert(:user, %{notified_at: Timex.shift(now, days: -1), notification_period_days: 1}) | ||
insert(:notification, %{user_id: user1.id}) | ||
|
||
user2 = insert(:user, %{notified_at: Timex.shift(now, days: -1), notification_period_days: 2}) | ||
insert(:notification, %{user_id: user2.id}) | ||
|
||
user3 = insert(:user, %{notified_at: nil}) | ||
insert(:notification, %{user_id: user3.id}) | ||
|
||
[user_to_notiy1, user_to_notiy2] = Notifications.get_notifiable_users(now) |> Enum.sort(&(&1.id < &2.id)) | ||
assert user_to_notiy1.id == user1.id | ||
assert user_to_notiy2.id == user3.id | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule CoursePlanner.Notifier.ServerTest do | ||
use CoursePlanner.ModelCase | ||
doctest CoursePlanner.Notifier.Server | ||
|
||
import ExUnit.CaptureLog | ||
import CoursePlanner.Factory | ||
import Swoosh.TestAssertions | ||
|
||
alias CoursePlanner.{Notifier.Server, Notifications, User} | ||
|
||
test "save notification to send later" do | ||
user = insert(:user) | ||
notification = Notifications.new() | ||
|> Notifications.type(:user_modified) | ||
|> Notifications.resource_path("/") | ||
|> Notifications.to(user) | ||
|
||
Server.handle_cast({:save_email, notification}, []) | ||
saved_user = Repo.get(User, user.id) |> Repo.preload(:notifications) | ||
[saved_notification] = saved_user.notifications | ||
assert saved_notification.type == "user_modified" | ||
end | ||
|
||
test "send saved notifications" do | ||
user = insert(:user) | ||
insert(:notification, %{user_id: user.id}) | ||
insert(:notification, %{user_id: user.id}) | ||
insert(:notification, %{user_id: user.id}) | ||
|
||
saved_user = Repo.get(User, user.id) |> Repo.preload(:notifications) | ||
|
||
Server.handle_cast({:notify_all, saved_user}, []) | ||
assert_email_sent subject: "Activity Summary" | ||
sent_user = Repo.get(User, user.id) |> Repo.preload(:notifications) | ||
assert sent_user.notifications == [] | ||
assert Timex.to_date(sent_user.notified_at) == Timex.today() | ||
end | ||
|
||
test "send email" do | ||
user = insert(:user) | ||
notification = Notifications.new() | ||
|> Notifications.type(:user_modified) | ||
|> Notifications.resource_path("/") | ||
|> Notifications.to(user) | ||
|
||
Server.handle_cast({:send_email, notification}, []) | ||
assert_email_sent subject: "Your profile is updated" | ||
end | ||
|
||
test "do not save notification without type" do | ||
user = insert(:user) | ||
notification = Notifications.new() | ||
|> Notifications.resource_path("/") | ||
|> Notifications.to(user) | ||
|
||
assert capture_log(fn -> Server.handle_cast({:save_email, notification}, []) end) =~ "Email saving failed" | ||
|
||
saved_user = Repo.get(User, user.id) |> Repo.preload(:notifications) | ||
assert [] == saved_user.notifications | ||
end | ||
|
||
end |
Oops, something went wrong.