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
pull/3534/head r2024.01.03-301
hq1 3 years ago committed by GitHub
parent af9b34a1c6
commit b6a2acb57a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 """

@ -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

@ -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

Loading…
Cancel
Save