Skip to content

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

Redirect user when they've already upgraded

Uku Taht8719865a41ec
diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex
index 9c9cd1e91d..c22f41f4b5 100644
--- a/lib/plausible_web/controllers/billing_controller.ex
+++ b/lib/plausible_web/controllers/billing_controller.ex
@@ -31,19 +31,22 @@ defmodule PlausibleWeb.BillingController do
def upgrade_enterprise_plan(conn, %{"plan_id" => plan_id}) do
user = conn.assigns[:current_user]
+ subscription = user.subscription
plan = Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: user.id, id: plan_id)
- if plan do
- usage = Plausible.Billing.usage(conn.assigns[:current_user])
+ cond do
+ plan && subscription && plan.paddle_plan_id == subscription.paddle_plan_id ->
+ redirect(conn, to: Routes.billing_path(conn, :change_plan_form))
- render(conn, "upgrade_to_plan.html",
- usage: usage,
- user: user,
- plan: plan,
- layout: {PlausibleWeb.LayoutView, "focus.html"}
- )
- else
- render_error(conn, 404)
+ plan ->
+ render(conn, "upgrade_to_plan.html",
+ user: user,
+ plan: plan,
+ layout: {PlausibleWeb.LayoutView, "focus.html"}
+ )
+
+ true ->
+ render_error(conn, 404)
end
end
diff --git a/test/plausible_web/controllers/billing_controller_test.exs b/test/plausible_web/controllers/billing_controller_test.exs
index 072277dc2d..3a8a348f7e 100644
--- a/test/plausible_web/controllers/billing_controller_test.exs
+++ b/test/plausible_web/controllers/billing_controller_test.exs
@@ -40,6 +40,16 @@ defmodule PlausibleWeb.BillingControllerTest do
assert html_response(conn, 200) =~ "Upgrade your free trial"
assert html_response(conn, 200) =~ "enterprise plan"
end
+
+ test "redirects to change-plan page if user is already subscribed to the given enterprise plan",
+ %{conn: conn, user: user} do
+ plan = insert(:enterprise_plan, user: user)
+ insert(:subscription, paddle_plan_id: plan.paddle_plan_id, user: user)
+
+ conn = get(conn, "/billing/upgrade/enterprise/#{plan.id}")
+
+ assert redirected_to(conn) == "/billing/change-plan"
+ end
end
describe "GET /change-plan" do