Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

send yearly renewal and expiration reminders to enterprise plans (#6727)

Marko Saricb937c1b4a6c5
diff --git a/lib/workers/notify_annual_renewal.ex b/lib/workers/notify_annual_renewal.ex
index 4f73af93f2..3bb78beabf 100644
--- a/lib/workers/notify_annual_renewal.ex
+++ b/lib/workers/notify_annual_renewal.ex
@@ -9,6 +9,7 @@ defmodule Plausible.Workers.NotifyAnnualRenewal do
require Plausible.Billing.Subscription.Status
alias Money.Subscription
+ alias Plausible.Billing.EnterprisePlan
alias Plausible.Billing.Subscription
alias Plausible.Teams
@@ -25,6 +26,16 @@ defmodule Plausible.Workers.NotifyAnnualRenewal do
where: s.timestamp > fragment("now() - INTERVAL '1 month'")
)
+ yearly_enterprise_plan =
+ from(
+ ep in EnterprisePlan,
+ where: ep.team_id == parent_as(:team).id,
+ where: ep.paddle_plan_id == parent_as(:subscription).paddle_plan_id,
+ where: ep.paddle_plan_id != ^Subscription.manual_plan_id(),
+ where: ep.billing_interval == :yearly,
+ select: 1
+ )
+
teams =
Repo.all(
from t in Teams.Team,
@@ -32,11 +43,12 @@ defmodule Plausible.Workers.NotifyAnnualRenewal do
inner_join: o in assoc(t, :owners),
left_join: bm in assoc(t, :billing_members),
inner_lateral_join: s in subquery(Teams.last_subscription_join_query()),
+ as: :subscription,
on: true,
left_join: sent in ^sent_notification,
on: o.id == sent.user_id,
where: is_nil(sent.id),
- where: s.paddle_plan_id in @yearly_plans,
+ where: s.paddle_plan_id in @yearly_plans or exists(yearly_enterprise_plan),
where:
s.next_bill_date > fragment("now()::date") and
s.next_bill_date <= fragment("now()::date + INTERVAL '7 days'"),
diff --git a/test/workers/notify_annual_renewal_test.exs b/test/workers/notify_annual_renewal_test.exs
index 76ea02f54c..5b25aea173 100644
--- a/test/workers/notify_annual_renewal_test.exs
+++ b/test/workers/notify_annual_renewal_test.exs
@@ -169,7 +169,61 @@ defmodule Plausible.Workers.NotifyAnnualRenewalTest do
)
end
+ test "sends renewal notification to user on a yearly enterprise plan", %{user: user} do
+ subscribe_to_enterprise_plan(user,
+ billing_interval: :yearly,
+ subscription: [next_bill_date: Date.shift(Date.utc_today(), day: 7)]
+ )
+
+ NotifyAnnualRenewal.perform(nil)
+
+ assert_email_delivered_with(
+ to: [{user.name, user.email}],
+ subject: "Your Plausible subscription is up for renewal"
+ )
+ end
+
+ test "ignores user with a manual enterprise subscription", %{user: user} do
+ subscribe_to_enterprise_plan(user,
+ billing_interval: :yearly,
+ paddle_plan_id: Subscription.manual_plan_id(),
+ subscription: [next_bill_date: Date.shift(Date.utc_today(), day: 7)]
+ )
+
+ NotifyAnnualRenewal.perform(nil)
+
+ assert_no_emails_delivered()
+ end
+
+ test "ignores user on a monthly enterprise plan", %{user: user} do
+ subscribe_to_enterprise_plan(user,
+ billing_interval: :monthly,
+ subscription: [next_bill_date: Date.shift(Date.utc_today(), day: 7)]
+ )
+
+ NotifyAnnualRenewal.perform(nil)
+
+ assert_no_emails_delivered()
+ end
+
describe "expiration" do
+ test "notifies user on a cancelled yearly enterprise plan about expiration", %{user: user} do
+ subscribe_to_enterprise_plan(user,
+ billing_interval: :yearly,
+ subscription: [
+ next_bill_date: Date.shift(Date.utc_today(), day: 7),
+ status: Subscription.Status.deleted()
+ ]
+ )
+
+ NotifyAnnualRenewal.perform(nil)
+
+ assert_email_delivered_with(
+ to: [{user.name, user.email}],
+ subject: "Your Plausible subscription is about to expire"
+ )
+ end
+
test "if user subscription is 'deleted', notify them about expiration instead", %{user: user} do
subscribe_to_plan(user, @yearly_plan,
next_bill_date: Date.shift(Date.utc_today(), day: 7),