Add weekly notification

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-06-05 10:12:08 +02:00
parent 03b1f84fba
commit 44e08c4319
8 changed files with 440 additions and 18 deletions

View File

@@ -144,4 +144,92 @@ defmodule Mobilizon.Service.Notifications.SchedulerTest do
Scheduler.on_day_notification(participant)
end
end
describe "Joining an event registers a job for notification on week of the event" do
test "if the user has allowed it" do
%User{id: user_id} = user = insert(:user, locale: "fr")
settings =
insert(:settings, user_id: user_id, notification_each_week: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
actor = insert(:actor, user: user)
# Make sure event happens next week
%Date{} = event_day = Date.utc_today() |> Date.add(7)
{:ok, %NaiveDateTime{} = event_date} = event_day |> NaiveDateTime.new(~T[16:00:00])
{:ok, begins_on} = DateTime.from_naive(event_date, "Etc/UTC")
%Event{} = event = insert(:event, begins_on: begins_on)
%Participant{} = participant = insert(:participant, actor: actor, event: event)
Scheduler.weekly_notification(participant)
{:ok, scheduled_at} =
begins_on
|> DateTime.to_date()
|> calculate_first_day_of_week("fr")
|> NaiveDateTime.new(~T[08:00:00])
{:ok, scheduled_at} = DateTime.from_naive(scheduled_at, "Europe/Paris")
assert_enqueued(
worker: Notification,
args: %{user_id: user_id, op: :weekly_notification},
scheduled_at: scheduled_at
)
end
test "not if the user hasn't allowed it" do
%User{id: user_id} = user = insert(:user)
actor = insert(:actor, user: user)
%Participant{} = participant = insert(:participant, actor: actor)
Scheduler.weekly_notification(participant)
refute_enqueued(
worker: Notification,
args: %{user_id: user_id, op: :weekly_notification}
)
end
test "not if it's too late" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
actor = insert(:actor, user: user)
{:ok, begins_on} =
Date.utc_today()
|> calculate_first_day_of_week("fr")
|> NaiveDateTime.new(~T[05:00:00])
{:ok, begins_on} = DateTime.from_naive(begins_on, "Europe/Paris")
%Event{} = event = insert(:event, begins_on: begins_on)
%Participant{} = participant = insert(:participant, actor: actor, event: event)
Scheduler.weekly_notification(participant)
refute_enqueued(
worker: Notification,
args: %{user_id: user_id, op: :weekly_notification}
)
end
end
defp calculate_first_day_of_week(%Date{} = date, locale) do
day_number = Date.day_of_week(date)
first_day_number = Cldr.Calendar.first_day_for_locale(locale)
if day_number == first_day_number,
do: date,
else: calculate_first_day_of_week(Date.add(date, -1), locale)
end
end

View File

@@ -207,4 +207,120 @@ defmodule Mobilizon.Service.Workers.NotificationTest do
)
end
end
describe "A weekly_notification job sends an email" do
test "if the user is still participating" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_each_week: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
Notification.perform(
%{"op" => "weekly_notification", "user_id" => user_id},
nil
)
assert_delivered_email(
NotificationMailer.weekly_notification(
user,
[participant],
1
)
)
end
test "unless the person is no longer participating" do
%Event{id: event_id} = insert(:event)
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_each_week: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
{:ok, %Participant{} = participant} =
Events.create_participant(%{actor_id: actor.id, event_id: event_id, role: :participant})
actor = Map.put(participant.actor, :user, user)
participant = Map.put(participant, :actor, actor)
assert {:ok, %Participant{}} = Events.delete_participant(participant)
Notification.perform(
%{"op" => "weekly_notification", "user_id" => user_id},
nil
)
refute_delivered_email(
NotificationMailer.weekly_notification(
user,
[participant],
1
)
)
end
test "unless the event has been cancelled" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_each_week: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
%Event{} = event = insert(:event, status: :cancelled)
%Participant{} =
participant = insert(:participant, role: :participant, event: event, actor: actor)
Notification.perform(
%{"op" => "weekly_notification", "user_id" => user_id},
nil
)
refute_delivered_email(
NotificationMailer.weekly_notification(
user,
[participant],
1
)
)
end
test "with a lot of events" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_each_week: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
participants =
Enum.reduce(0..10, [], fn _i, acc ->
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
acc ++ [participant]
end)
Notification.perform(
%{"op" => "weekly_notification", "user_id" => user_id},
nil
)
refute_delivered_email(
NotificationMailer.weekly_notification(
user,
participants,
3
)
)
end
end
end