From f977351ae2ba4d79f432978317a49e64dcaafa89 Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Mon, 6 Nov 2023 14:01:55 +0000 Subject: [PATCH] Fix upgrade page for legacy trials + bug fixes (#3486) * allow using Stats API and Props for free_10k subscriptions * return v3 plans for legacy trials * do not display grandfathering notice for legacy trials * set a more accurate BT release date * fix bug on dev env Allow the `find/1` function to find sandbox plans * add error handling and tests for change_plan_preview * fix feature warning bug * fix credo warnings * fix tests * set BT release date further into the future * rename function and some vars * bugfix with limit exceeding * fix test --- lib/plausible/billing/plans.ex | 13 ++++-- lib/plausible/billing/quota.ex | 16 +++++-- .../site/memberships/create_invitation.ex | 6 +-- lib/plausible/sites.ex | 2 +- .../controllers/billing_controller.ex | 29 +++++++++---- .../controllers/site_controller.ex | 4 +- lib/plausible_web/live/choose_plan.ex | 14 ++++--- lib/workers/check_usage.ex | 12 +++--- test/plausible/billing/plans_test.exs | 17 ++++++-- test/plausible/billing/quota_test.exs | 26 ++++++++---- .../controllers/billing_controller_test.exs | 30 +++++++++++++ test/plausible_web/live/choose_plan_test.exs | 42 ++++++++++++++++--- test/support/paddle_api_mock.ex | 19 +++++++++ test/support/test_utils.ex | 2 +- 14 files changed, 185 insertions(+), 47 deletions(-) diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index bd7860fdf..6a175338e 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -27,7 +27,7 @@ defmodule Plausible.Billing.Plans do Module.put_attribute(__MODULE__, :external_resource, path) end - @business_tier_launch ~D[2023-11-07] + @business_tier_launch ~N[2023-12-01 12:00:00] @spec growth_plans_for(User.t()) :: [Plan.t()] @doc """ @@ -44,7 +44,9 @@ defmodule Plausible.Billing.Plans do cond do Application.get_env(:plausible, :environment) == "dev" -> @sandbox_plans - !owned_plan -> if v4_available, do: @plans_v4, else: @plans_v3 + is_nil(owned_plan) && Timex.before?(user.inserted_at, @business_tier_launch) -> @plans_v3 + is_nil(owned_plan) && v4_available -> @plans_v4 + is_nil(owned_plan) -> @plans_v3 owned_plan.kind == :business -> @plans_v4 owned_plan.generation == 1 -> @plans_v1 owned_plan.generation == 2 -> @plans_v2 @@ -60,6 +62,7 @@ defmodule Plausible.Billing.Plans do cond do Application.get_env(:plausible, :environment) == "dev" -> @sandbox_plans + is_nil(owned_plan) && Timex.before?(user.inserted_at, @business_tier_launch) -> @plans_v3 owned_plan && owned_plan.generation < 4 -> @plans_v3 true -> @plans_v4 end @@ -242,6 +245,10 @@ defmodule Plausible.Billing.Plans do end defp all() do - @legacy_plans ++ @plans_v1 ++ @plans_v2 ++ @plans_v3 ++ @plans_v4 + @legacy_plans ++ @plans_v1 ++ @plans_v2 ++ @plans_v3 ++ @plans_v4 ++ sandbox_plans() + end + + defp sandbox_plans() do + if Application.get_env(:plausible, :environment) == "dev", do: @sandbox_plans, else: [] end end diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex index d11f7333e..348e66354 100644 --- a/lib/plausible/billing/quota.ex +++ b/lib/plausible/billing/quota.ex @@ -230,7 +230,7 @@ defmodule Plausible.Billing.Quota do case Plans.get_subscription_plan(user.subscription) do %EnterprisePlan{} -> Feature.list() %Plan{features: features} -> features - :free_10k -> [Goals] + :free_10k -> [Goals, Props, StatsAPI] nil -> Feature.list() end end @@ -241,11 +241,21 @@ defmodule Plausible.Billing.Quota do select: %{site_id: sm.site_id} end + @spec below_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean() + @doc """ + Returns whether the usage is below the limit or not. + Returns false if usage is equal to the limit. + """ + def below_limit?(usage, limit) do + if limit == :unlimited, do: true, else: usage < limit + end + @spec within_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean() @doc """ - Returns whether the limit has been exceeded or not. + Returns whether the usage is within the limit or not. + Returns true if usage is equal to the limit. """ def within_limit?(usage, limit) do - if limit == :unlimited, do: true, else: usage < limit + if limit == :unlimited, do: true, else: usage <= limit end end diff --git a/lib/plausible/site/memberships/create_invitation.ex b/lib/plausible/site/memberships/create_invitation.ex index 91244d8d3..8bbe99ee4 100644 --- a/lib/plausible/site/memberships/create_invitation.ex +++ b/lib/plausible/site/memberships/create_invitation.ex @@ -117,14 +117,14 @@ defmodule Plausible.Site.Memberships.CreateInvitation do site_usage = Plausible.Repo.aggregate(Quota.team_member_usage_query(site.owner, site), :count) usage_after_transfer = current_usage + site_usage - Quota.within_limit?(usage_after_transfer, limit) + Quota.below_limit?(usage_after_transfer, limit) end defp within_site_limit_after_transfer?(new_owner) do limit = Quota.site_limit(new_owner) usage_after_transfer = Quota.site_usage(new_owner) + 1 - Quota.within_limit?(usage_after_transfer, limit) + Quota.below_limit?(usage_after_transfer, limit) end defp has_access_to_site_features?(site, new_owner) do @@ -174,7 +174,7 @@ defmodule Plausible.Site.Memberships.CreateInvitation do limit = Quota.team_member_limit(site.owner) usage = Quota.team_member_usage(site.owner) - if Quota.within_limit?(usage, limit), + if Quota.below_limit?(usage, limit), do: :ok, else: {:error, {:over_limit, limit}} end diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.ex index 1df4b9b7e..3532e3b9d 100644 --- a/lib/plausible/sites.ex +++ b/lib/plausible/sites.ex @@ -87,7 +87,7 @@ defmodule Plausible.Sites do limit = Quota.site_limit(user) usage = Quota.site_usage(user) - if Quota.within_limit?(usage, limit), do: {:ok, usage}, else: {:error, limit} + if Quota.below_limit?(usage, limit), do: {:ok, usage}, else: {:error, limit} end) |> Ecto.Multi.insert(:site, site_changeset) |> Ecto.Multi.insert(:site_membership, fn %{site: site} -> diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex index e8a67de81..1c46395db 100644 --- a/lib/plausible_web/controllers/billing_controller.ex +++ b/lib/plausible_web/controllers/billing_controller.ex @@ -124,14 +124,12 @@ defmodule PlausibleWeb.BillingController do end def change_plan_preview(conn, %{"plan_id" => new_plan_id}) do - with {:ok, {subscription, preview_info}} <- - preview_subscription(conn.assigns.current_user, new_plan_id) do + user = conn.assigns.current_user + business_tier_enabled? = FunWithFlags.enabled?(:business_tier, for: user) + + with {:ok, {subscription, preview_info}} <- preview_subscription(user, new_plan_id) do back_action = - if FunWithFlags.enabled?(:business_tier, for: conn.assigns.current_user) do - :choose_plan - else - :change_plan_form - end + if business_tier_enabled?, do: :choose_plan, else: :change_plan_form render(conn, "change_plan_preview.html", back_link: Routes.billing_path(conn, back_action), @@ -142,7 +140,22 @@ defmodule PlausibleWeb.BillingController do ) else _ -> - redirect(conn, to: Routes.billing_path(conn, :upgrade)) + redirect_to = if business_tier_enabled?, do: :choose_plan, else: :upgrade + + msg = + "Something went wrong with loading your plan change information. Please try again, or contact us at support@plausible.io if the issue persists." + + Sentry.capture_message("Error loading change plan preview", + extra: %{ + message: msg, + new_plan_id: new_plan_id, + user_id: user.id + } + ) + + conn + |> put_flash(:error, msg) + |> redirect(to: Routes.billing_path(conn, redirect_to)) end end diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index a41179734..c4a493c01 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -13,12 +13,12 @@ defmodule PlausibleWeb.SiteController do limit = Plausible.Billing.Quota.site_limit(current_user) usage = Plausible.Billing.Quota.site_usage(current_user) - within_limit? = Plausible.Billing.Quota.within_limit?(usage, limit) + below_limit? = Plausible.Billing.Quota.below_limit?(usage, limit) render(conn, "new.html", changeset: Plausible.Site.changeset(%Plausible.Site{}), is_first_site: usage == 0, - is_at_limit: !within_limit?, + is_at_limit: !below_limit?, site_limit: limit, layout: {PlausibleWeb.LayoutView, "focus.html"} ) diff --git a/lib/plausible_web/live/choose_plan.ex b/lib/plausible_web/live/choose_plan.ex index 58a87028e..2dfaaacb6 100644 --- a/lib/plausible_web/live/choose_plan.ex +++ b/lib/plausible_web/live/choose_plan.ex @@ -285,7 +285,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do <.contact_button class="bg-indigo-600 hover:bg-indigo-500 text-white" /> <% end %> - <%= if @kind == :growth && @plan_to_render.generation < 4 do %> + <%= if @owned && @kind == :growth && @plan_to_render.generation < 4 do %> <.growth_grandfathering_notice /> <% else %>