From d3311898c59f7e963a8714c4fc08e084325becef Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Thu, 4 Nov 2021 11:46:41 +0200 Subject: [PATCH] Show message when grace period is over --- assets/package-lock.json | 6 ++- .../templates/layout/_notice.html.eex | 49 +++++++++++++++++++ .../templates/site/index.html.eex | 2 +- lib/plausible_web/views/layout_view.ex | 20 ++++++++ lib/workers/check_usage.ex | 4 +- test/workers/check_usage_test.exs | 22 +++++++++ 6 files changed, 98 insertions(+), 5 deletions(-) diff --git a/assets/package-lock.json b/assets/package-lock.json index 2db912dcb..11781af6c 100644 --- a/assets/package-lock.json +++ b/assets/package-lock.json @@ -1,9 +1,11 @@ { "name": "assets", + "version": "1.4.0", "lockfileVersion": 2, "requires": true, "packages": { "": { + "version": "1.4.0", "license": "MIT", "dependencies": { "@babel/core": "^7.14.3", @@ -62,11 +64,11 @@ } }, "../deps/phoenix": { - "version": "1.4.17", + "version": "1.5.13", "license": "MIT" }, "../deps/phoenix_html": { - "version": "2.14.2" + "version": "2.14.3" }, "node_modules/@babel/code-frame": { "version": "7.12.13", diff --git a/lib/plausible_web/templates/layout/_notice.html.eex b/lib/plausible_web/templates/layout/_notice.html.eex index 069c9d215..1572644f4 100644 --- a/lib/plausible_web/templates/layout/_notice.html.eex +++ b/lib/plausible_web/templates/layout/_notice.html.eex @@ -2,6 +2,55 @@ <%= render("_flash.html", assigns) %> <% end %> + +<%= if on_grace_period?(@conn.assigns[:current_user]) do %> +
+
+
+
+ +
+
+

+ Please upgrade your account +

+
+

+ In order to keep your stats running, we require you to upgrade your account. If you do not upgrade your account <%= grace_period_end(@conn.assigns[:current_user]) %>, we will lock your sites and they won't be accessible. <%= link("Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800") %> +

+
+
+
+
+
+<% end %> + +<%= if grace_period_over?(@conn.assigns[:current_user]) do %> +
+
+
+
+ +
+
+

+ Dashboard locked +

+
+

+ As you have outgrown your subscription tier, we kindly ask you to upgrade your subscription to accommodate your new traffic levels. <%= link("Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800") %> +

+
+
+
+
+
+<% end %> + <%= if @conn.assigns[:current_user] && @conn.assigns[:current_user].subscription && @conn.assigns[:current_user].subscription.status == "past_due" do %>
diff --git a/lib/plausible_web/templates/site/index.html.eex b/lib/plausible_web/templates/site/index.html.eex index 0fab8fbf1..79fd51ffd 100644 --- a/lib/plausible_web/templates/site/index.html.eex +++ b/lib/plausible_web/templates/site/index.html.eex @@ -1,6 +1,6 @@
- <%= if @needs_to_upgrade do %> + <%= if @needs_to_upgrade == {true, :no_active_subscription} do %>
diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex index 6cad0c241..ec2bb57da 100644 --- a/lib/plausible_web/views/layout_view.ex +++ b/lib/plausible_web/views/layout_view.ex @@ -55,6 +55,26 @@ defmodule PlausibleWeb.LayoutView do end end + def on_grace_period?(user) do + user.grace_period && + Timex.diff(user.grace_period.end_date, Timex.today(), :days) >= 0 + end + + def grace_period_over?(user) do + user.grace_period && + Timex.diff(user.grace_period.end_date, Timex.today(), :days) < 0 + end + + def grace_period_end(user) do + end_date = user.grace_period.end_date + + case Timex.diff(end_date, Timex.today(), :days) do + 0 -> "today" + 1 -> "tomorrow" + n -> "within #{n} days" + end + end + @doc "http://blog.plataformatec.com.br/2018/05/nested-layouts-with-phoenix/" def render_layout(layout, assigns, do: content) do render(layout, Map.put(assigns, :inner_layout, content)) diff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.ex index ca0193025..4bdf864b9 100644 --- a/lib/workers/check_usage.ex +++ b/lib/workers/check_usage.ex @@ -104,13 +104,13 @@ defmodule Plausible.Workers.CheckUsage do end defp check_pageview_limit(subscriber, billing_mod) do - allowance = Plausible.Billing.Plans.allowance(subscriber.subscription) + allowance = Plausible.Billing.Plans.allowance(subscriber.subscription) * 1.1 {_, last_cycle} = billing_mod.last_two_billing_cycles(subscriber) {last_last_cycle_usage, last_cycle_usage} = billing_mod.last_two_billing_months_usage(subscriber) - if last_last_cycle_usage > allowance && last_cycle_usage > allowance do + if last_last_cycle_usage >= allowance && last_cycle_usage >= allowance do {:over_limit, {last_cycle, last_cycle_usage}} else {:within_limit, {last_cycle, last_cycle_usage}} diff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exs index 3dc641857..73bdbbf52 100644 --- a/test/workers/check_usage_test.exs +++ b/test/workers/check_usage_test.exs @@ -49,6 +49,28 @@ defmodule Plausible.Workers.CheckUsageTest do assert Repo.reload(user).grace_period == nil end + test "does not send an email if account is over the limit by less than 10%", %{ + user: user + } do + billing_stub = + Plausible.Billing + |> stub(:last_two_billing_cycles, fn _user -> + {Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())} + end) + |> stub(:last_two_billing_months_usage, fn _user -> {10_999, 11_000} end) + + insert(:subscription, + user: user, + paddle_plan_id: @paddle_id_10k, + last_bill_date: Timex.shift(Timex.today(), days: -1) + ) + + CheckUsage.perform(nil, billing_stub) + + assert_no_emails_delivered() + assert Repo.reload(user).grace_period == nil + end + test "sends an email when an account is over their limit for two consecutive billing months", %{ user: user } do