Send email notification when sites are locked at grace period end

pull/1479/head
Uku Taht 5 years ago
parent 29cb7462e6
commit d547d0f996

@ -107,13 +107,19 @@ defmodule Plausible.Billing do
PaddleApi.update_subscription_preview(subscription.paddle_subscription_id, new_plan_id)
end
def needs_to_upgrade?(%Plausible.Auth.User{trial_expiry_date: nil}), do: true
def needs_to_upgrade?(%Plausible.Auth.User{trial_expiry_date: nil}), do: {true, :no_trial}
def needs_to_upgrade?(user) do
if Timex.before?(user.trial_expiry_date, Timex.today()) do
!subscription_is_active?(user.subscription)
else
false
trial_is_over = Timex.before?(user.trial_expiry_date, Timex.today())
subscription_active = subscription_is_active?(user.subscription)
grace_period_ended =
user.grace_period_end && Timex.before?(user.grace_period_end, Timex.today())
cond do
trial_is_over && !subscription_active -> {true, :no_active_subscription}
grace_period_ended -> {true, :grace_period_ended}
true -> false
end
end

@ -2,10 +2,16 @@ defmodule Plausible.Billing.SiteLocker do
use Plausible.Repo
def check_sites_for(user) do
if Plausible.Billing.needs_to_upgrade?(user) do
set_lock_status_for(user, true)
else
set_lock_status_for(user, false)
case Plausible.Billing.needs_to_upgrade?(user) do
{true, :grace_period_ended} ->
set_lock_status_for(user, true)
send_grace_period_end_email(user)
{true, _} ->
set_lock_status_for(user, true)
_ ->
set_lock_status_for(user, false)
end
end
@ -26,4 +32,20 @@ defmodule Plausible.Billing.SiteLocker do
Repo.update_all(site_q, set: [locked: status])
end
defp send_grace_period_end_email(user) do
{_, last_cycle} = Plausible.Billing.last_two_billing_cycles(user)
{_, last_cycle_usage} = Plausible.Billing.last_two_billing_months_usage(user)
suggested_plan = Plausible.Billing.Plans.suggested_plan(user, last_cycle)
template =
PlausibleWeb.Email.dashboard_locked(
user,
last_cycle_usage,
last_cycle,
suggested_plan
)
Plausible.Mailer.send_email_safe(template)
end
end

@ -142,6 +142,19 @@ defmodule PlausibleWeb.Email do
})
end
def dashboard_locked(user, usage, last_cycle, suggested_plan) do
base_email()
|> to(user)
|> tag("dashboard-locked")
|> subject("[Action required] Your Plausible dashboard is now locked")
|> render("dashboard_locked.html", %{
user: user,
usage: usage,
last_cycle: last_cycle,
suggested_plan: suggested_plan
})
end
def yearly_renewal_notification(user) do
date = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}")

@ -0,0 +1,25 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
Last week we sent a reminder that your traffic has exceeded your Plausible Analytics subscription tier two months in a row.
<br /><br />
Your dashboard is now locked. We're still counting your stats, but you no longer have access to the stats. As you have outgrown your subscription tier, we kindly ask you to upgrade your subscription to accommodate your new traffic levels.
<br /><br />
Click here to go to your site settings. In the last billing cycle (<%= date_format(@last_cycle.first) %> to <%= date_format(@last_cycle.last) %>), your account has used <%= PlausibleWeb.StatsView.large_number_format(@usage) %> billable pageviews.
<%= if @usage <= 20_000_000 do %>
Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan.
<br /><br />
You can upgrade your subscription using our self-serve platform. The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire.
<br /><br />
<a href="https://plausible.io/settings">Click here</a> to go to your site settings. You can upgrade your subscription tier by clicking the 'change plan' link.
<% else %>
This is more than our standard plans, so please reply back to this email to get a quote for your volume.
<% end %>
<br /><br />
The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire. We will unlock your dashboard immediately after your account has been upgraded to the appropriate tier.
<br /><br />
Thanks for understanding and for being a Plausible subscriber!
<br /><br />
Thanks,<br />
Uku and Marko<br />
--<br />
<%= plausible_url() %><br />

@ -49,6 +49,17 @@ defmodule Plausible.Billing.SiteLockerTest do
refute Repo.reload!(site).locked
end
test "does not lock user who has an active subscription and is on grace period" do
user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: 1))
insert(:subscription, status: "active", user: user)
user = Repo.preload(user, :subscription)
site = insert(:site, members: [user])
SiteLocker.check_sites_for(user)
refute Repo.reload!(site).locked
end
test "locks user who cancelled subscription and the cancelled subscription has expired" do
user = insert(:user)
@ -67,6 +78,31 @@ defmodule Plausible.Billing.SiteLockerTest do
refute Repo.reload!(site).locked
end
test "locks all sites if user has active subscription but grace period has ended" do
user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: -1))
insert(:subscription, status: "active", user: user)
user = Repo.preload(user, :subscription)
site = insert(:site, members: [user])
SiteLocker.check_sites_for(user)
assert Repo.reload!(site).locked
end
test "sends email if grace period has ended" do
user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: -1))
insert(:subscription, status: "active", user: user)
user = Repo.preload(user, :subscription)
insert(:site, members: [user])
SiteLocker.check_sites_for(user)
assert_email_delivered_with(
to: [user],
subject: "[Action required] Your Plausible dashboard is now locked"
)
end
test "locks all sites if user has no trial or active subscription" do
user =
insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))

@ -112,6 +112,7 @@ defmodule Plausible.Factory do
status: "active",
next_bill_amount: "6.00",
next_bill_date: Timex.today(),
last_bill_date: Timex.today(),
currency_code: "USD"
}
end

Loading…
Cancel
Save