From b6a2acb57a3015949d616cc5d1dce8bb4dbf9996 Mon Sep 17 00:00:00 2001 From: hq1 Date: Wed, 3 Jan 2024 11:19:25 +0100 Subject: [PATCH] Update accept_traffic_until notifications (#3665) * Update accept_traffic_until notifications: - extend has_stats to 48h - ensure the user is properly greeted * Clarify the structure passed to email template function --- lib/plausible_web/email.ex | 18 ++++++--- .../accept_traffic_until_notification.ex | 7 ++-- test/workers/accept_traffic_until_test.exs | 39 ++++++++++++++++++- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index 113ba25f1..81aaff7e9 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -353,20 +353,26 @@ defmodule PlausibleWeb.Email do }) end - def approaching_accept_traffic_until(user) do + def approaching_accept_traffic_until(notification) do base_email() - |> to(user.email) + |> to(notification.email) |> tag("drop-traffic-warning-first") |> subject("We'll stop counting your stats") - |> render("approaching_accept_traffic_until.html", time: "next week", user: user) + |> render("approaching_accept_traffic_until.html", + time: "next week", + user: %{email: notification.email, name: notification.name} + ) end - def approaching_accept_traffic_until_tomorrow(user) do + def approaching_accept_traffic_until_tomorrow(notification) do base_email() - |> to(user.email) + |> to(notification.email) |> tag("drop-traffic-warning-final") |> subject("A reminder that we'll stop counting your stats tomorrow") - |> render("approaching_accept_traffic_until.html", time: "tomorrow") + |> render("approaching_accept_traffic_until.html", + time: "tomorrow", + user: %{email: notification.email, name: notification.name} + ) end @doc """ diff --git a/lib/workers/accept_traffic_until_notification.ex b/lib/workers/accept_traffic_until_notification.ex index 6ac7cbba3..7d6890996 100644 --- a/lib/workers/accept_traffic_until_notification.ex +++ b/lib/workers/accept_traffic_until_notification.ex @@ -40,7 +40,8 @@ defmodule Plausible.Workers.AcceptTrafficUntil do id: u.id, email: u.email, deadline: u.accept_traffic_until, - site_ids: fragment("array_agg(?.site_id)", sm) + site_ids: fragment("array_agg(?.site_id)", sm), + name: u.name }, group_by: u.id ) @@ -68,11 +69,11 @@ defmodule Plausible.Workers.AcceptTrafficUntil do end defp has_stats?(site_ids, today) do - yesterday = Date.add(today, -1) + ago_2d = Date.add(today, -2) ClickhouseRepo.exists?( from e in "events_v2", - where: fragment("toDate(?) >= ?", e.timestamp, ^yesterday), + where: fragment("toDate(?) >= ?", e.timestamp, ^ago_2d), where: e.site_id in ^site_ids ) end diff --git a/test/workers/accept_traffic_until_test.exs b/test/workers/accept_traffic_until_test.exs index b66b05908..af76df44b 100644 --- a/test/workers/accept_traffic_until_test.exs +++ b/test/workers/accept_traffic_until_test.exs @@ -12,6 +12,7 @@ defmodule Plausible.Workers.AcceptTrafficUntilTest do tomorrow = today |> Date.add(+1) user1 = insert(:user, accept_traffic_until: next_week) + user2 = insert(:user, accept_traffic_until: tomorrow) _site1 = insert(:site, members: [user1]) @@ -24,6 +25,37 @@ defmodule Plausible.Workers.AcceptTrafficUntilTest do refute_notifications(user2.email) end + test "does not send any notifications when site has stats older than 2d" do + today = Date.utc_today() + next_week = today |> Date.add(+7) + + user = insert(:user, accept_traffic_until: next_week) + + :site + |> insert(members: [user]) + |> populate_stats([build(:pageview, timestamp: Date.add(today, -3))]) + + {:ok, 1} = AcceptTrafficUntil.perform(nil) + + refute_notifications(user.email) + end + + test "does send notification when last stat is 2d old" do + today = Date.utc_today() + next_week = today |> Date.add(+7) + + user = + insert(:user, accept_traffic_until: next_week) + + :site + |> insert(members: [user]) + |> populate_stats([build(:pageview, timestamp: Date.add(today, -2))]) + + {:ok, 1} = AcceptTrafficUntil.perform(nil) + + assert_weekly_notification(user.email) + end + test "tomorrow: sends one e-mail" do tomorrow = Date.utc_today() |> Date.add(+1) user = insert(:user, accept_traffic_until: tomorrow) @@ -107,16 +139,19 @@ defmodule Plausible.Workers.AcceptTrafficUntilTest do defp assert_weekly_notification(email) when is_binary(email) do assert_email_delivered_with( + html_body: ~r/Hey Jane,/, to: [nil: email], - subject: PlausibleWeb.Email.approaching_accept_traffic_until(%{email: email}).subject + subject: + PlausibleWeb.Email.approaching_accept_traffic_until(%{name: "", email: email}).subject ) end defp assert_final_notification(email) when is_binary(email) do assert_email_delivered_with( + html_body: ~r/Hey Jane,/, to: [nil: email], subject: - PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow(%{email: email}).subject + PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow(%{name: "", email: email}).subject ) end