Fix annual notification loop

This commit is contained in:
Uku Taht 2022-05-03 17:00:13 +03:00
parent 46ee1ba012
commit 6f3399bb7c
2 changed files with 34 additions and 2 deletions

View File

@ -19,6 +19,12 @@ defmodule Plausible.Workers.NotifyAnnualRenewal do
}
)
sent_notification =
from(
s in "sent_renewal_notifications",
where: s.timestamp > fragment("now() - INTERVAL '1 month'")
)
users =
Repo.all(
from u in Plausible.Auth.User,
@ -26,13 +32,13 @@ defmodule Plausible.Workers.NotifyAnnualRenewal do
on: cs.user_id == u.id,
join: s in Plausible.Billing.Subscription,
on: s.inserted_at == cs.inserted_at,
left_join: sent in "sent_renewal_notifications",
left_join: sent in ^sent_notification,
on: s.user_id == sent.user_id,
where: is_nil(sent.id),
where: s.paddle_plan_id in @yearly_plans,
where:
s.next_bill_date > fragment("now()::date") and
s.next_bill_date <= fragment("now()::date + INTERVAL '7 days'"),
where: is_nil(sent.id) or sent.timestamp < fragment("now() - INTERVAL '1 month'"),
preload: [subscription: s]
)

View File

@ -136,6 +136,32 @@ defmodule Plausible.Workers.NotifyAnnualRenewalTest do
)
end
test "does not send multiple notifications on second year", %{user: user} do
insert(:subscription,
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 7)
)
Repo.insert_all("sent_renewal_notifications", [
%{
user_id: user.id,
timestamp: Timex.shift(Timex.today(), years: -1) |> Timex.to_naive_datetime()
}
])
NotifyAnnualRenewal.perform(nil)
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible subscription is up for renewal"
)
NotifyAnnualRenewal.perform(nil)
assert_no_emails_delivered()
end
test "sends renewal notification to user on v2 yearly pricing plans", %{
user: user
} do