Make sure only the current subcription is used in expiry notifications

This commit is contained in:
Uku Taht 2021-07-19 15:03:15 +03:00
parent 78e96a6d67
commit fef5337a64
2 changed files with 41 additions and 10 deletions

View File

@ -9,11 +9,22 @@ defmodule Plausible.Workers.NotifyAnnualRenewal do
Sends a notification at most 7 days and at least 1 day before the renewal of an annual subscription Sends a notification at most 7 days and at least 1 day before the renewal of an annual subscription
""" """
def perform(_job) do def perform(_job) do
current_subscriptions = from(
s in Plausible.Billing.Subscription,
group_by: s.user_id,
select: %{
user_id: s.user_id,
inserted_at: max(s.inserted_at)
}
)
users = users =
Repo.all( Repo.all(
from u in Plausible.Auth.User, from u in Plausible.Auth.User,
join: cs in subquery(current_subscriptions),
on: cs.user_id == u.id,
join: s in Plausible.Billing.Subscription, join: s in Plausible.Billing.Subscription,
on: s.user_id == u.id, on: s.inserted_at == cs.inserted_at,
left_join: sent in "sent_renewal_notifications", left_join: sent in "sent_renewal_notifications",
on: s.user_id == sent.user_id, on: s.user_id == sent.user_id,
where: s.paddle_plan_id in @yearly_plans, where: s.paddle_plan_id in @yearly_plans,

View File

@ -39,6 +39,26 @@ defmodule Plausible.Workers.NotifyAnnualRenewalTest do
assert_no_emails_delivered() assert_no_emails_delivered()
end end
test "ignores user with old yearly subscription that's been superseded by a newer one", %{user: user} do
insert(:subscription,
inserted_at: Timex.shift(Timex.now(), days: -1),
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 5)
)
insert(:subscription,
inserted_at: Timex.now(),
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 30)
)
NotifyAnnualRenewal.perform(nil)
assert_no_emails_delivered()
end
test "sends renewal notification to user whose subscription is due for renewal in 7 days", %{ test "sends renewal notification to user whose subscription is due for renewal in 7 days", %{
user: user user: user
} do } do