diff --git a/lib/workers/notify_annual_renewal.ex b/lib/workers/notify_annual_renewal.ex index 3d081543f..f4215b842 100644 --- a/lib/workers/notify_annual_renewal.ex +++ b/lib/workers/notify_annual_renewal.ex @@ -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] ) diff --git a/test/workers/notify_annual_renewal_test.exs b/test/workers/notify_annual_renewal_test.exs index c729d616a..93dc72fdd 100644 --- a/test/workers/notify_annual_renewal_test.exs +++ b/test/workers/notify_annual_renewal_test.exs @@ -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