Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
CRM: edit enterprise plan (#5528)
Adam Rutkowski3068abd211a0
diff --git a/extra/lib/plausible_web/live/customer_support/live/team.ex b/extra/lib/plausible_web/live/customer_support/live/team.exindex 6ed15a2f04..a805b77332 100644--- a/extra/lib/plausible_web/live/customer_support/live/team.ex+++ b/extra/lib/plausible_web/live/customer_support/live/team.ex@@ -93,6 +93,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team doplans: plans,plan_form: plan_form,show_plan_form?: false,+ editing_plan: nil,tab: "billing",cost_estimate: 0,cost_estimate_tier: :business@@ -319,6 +320,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do<.th>Paddle Plan ID</.th><.th>Limits</.th><.th>Features</.th>+ <.th invisible>Actions</.th></:thead><:tbody :let={plan}><.td class="align-top">@@ -352,6 +354,9 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do<.td class="align-top"><span :for={feat <- plan.features}>{feat.display_name()}<br /></span></.td>+ <.td class="align-top">+ <.edit_button phx-click="edit-plan" phx-value-id={plan.id} phx-target={@myself} />+ </.td></:tbody></.table>@@ -360,7 +365,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do:if={@show_plan_form?}for={@plan_form}id="save-plan"- phx-submit="save-plan"+ phx-submit={if @editing_plan, do: "update-plan", else: "save-plan"}phx-target={@myself}phx-change="estimate-cost">@@ -420,7 +425,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do:if={not mod.free?()}x-on:change="featureChangeCallback(event)"type="checkbox"- value={mod in (f.source.changes[:features] || [])}+ value={mod in (f.source.changes[:features] || f.source.data.features || [])}name={"#{f.name}[features[]][#{mod.name()}]"}label={mod.display_name()}/>@@ -448,7 +453,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do</.button><.button type="submit">- Save Custom Plan+ {if @editing_plan, do: "Update Plan", else: "Save Custom Plan"}</.button></div></.form>@@ -632,7 +637,20 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team doenddef handle_event("show-plan-form", _, socket) do- {:noreply, assign(socket, show_plan_form?: true)}+ {:noreply, assign(socket, show_plan_form?: true, editing_plan: nil)}+ end++ def handle_event("edit-plan", %{"id" => plan_id}, socket) do+ {plan_id, _} = Integer.parse(plan_id)+ plan = Enum.find(socket.assigns.plans, &(&1.id == plan_id))++ if plan do+ plan_form = to_form(EnterprisePlan.changeset(plan, %{}))++ {:noreply, assign(socket, show_plan_form?: true, editing_plan: plan, plan_form: plan_form)}+ else+ {:noreply, socket}+ endenddef handle_event("remove-domain", %{"identifier" => i}, socket) do@@ -642,7 +660,7 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team doenddef handle_event("hide-plan-form", _, socket) do- {:noreply, assign(socket, show_plan_form?: false)}+ {:noreply, assign(socket, show_plan_form?: false, editing_plan: nil)}enddef handle_event("save-team", %{"team" => params}, socket) do@@ -724,10 +742,42 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team doplans = get_plans(socket.assigns.team.id){:noreply,- assign(socket, plans: plans, plan_form: to_form(changeset), show_plan_form?: false)}+ assign(socket,+ plans: plans,+ plan_form: to_form(changeset),+ show_plan_form?: false,+ editing_plan: nil+ )}{:error, changeset} ->- failure(socket, "Error saving team: #{inspect(changeset.errors)}")+ failure(socket, "Error saving plan: #{inspect(changeset.errors)}")+ {:noreply, assign(socket, plan_form: to_form(changeset))}+ end+ end++ def handle_event("update-plan", %{"enterprise_plan" => params}, socket) do+ params =+ params+ |> update_features_to_list()+ |> sanitize_params()++ changeset = EnterprisePlan.changeset(socket.assigns.editing_plan, params)++ case Plausible.Repo.update(changeset) do+ {:ok, _plan} ->+ success(socket, "Plan updated")+ plans = get_plans(socket.assigns.team.id)++ {:noreply,+ assign(socket,+ plans: plans,+ plan_form: to_form(changeset),+ show_plan_form?: false,+ editing_plan: nil+ )}++ {:error, changeset} ->+ failure(socket, "Error updating plan: #{inspect(changeset.errors)}"){:noreply, assign(socket, plan_form: to_form(changeset))}endenddiff --git a/test/plausible_web/live/customer_support/teams_test.exs b/test/plausible_web/live/customer_support/teams_test.exsindex f32f9466e0..c77e61432b 100644--- a/test/plausible_web/live/customer_support/teams_test.exs+++ b/test/plausible_web/live/customer_support/teams_test.exs@@ -232,6 +232,290 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest doassert text(html) =~ "Plan saved"end+ test "displays existing custom plan with edit button", %{conn: conn, user: user} do+ team = team_of(user)++ plan =+ insert(:enterprise_plan,+ team: team,+ paddle_plan_id: "existing-plan-123",+ billing_interval: :yearly,+ monthly_pageview_limit: 5_000_000,+ site_limit: 200,+ team_member_limit: 25,+ hourly_api_request_limit: 2000,+ features: [Plausible.Billing.Feature.StatsAPI, Plausible.Billing.Feature.Funnels]+ )++ {:ok, lv, _html} = live(conn, open_team(team.id, tab: "billing"))+ html = render(lv)++ assert text(html) =~ "existing-plan-123"+ assert text(html) =~ "yearly"+ assert text(html) =~ "5,000,000"+ assert text(html) =~ "200"+ assert text(html) =~ "25"+ assert text(html) =~ "2,000"++ assert element_exists?(html, ~s|button[phx-click="edit-plan"][phx-value-id="#{plan.id}"]|)+ end++ test "edit plan loads existing values into form", %{conn: conn, user: user} do+ team = team_of(user)++ plan =+ insert(:enterprise_plan,+ team: team,+ paddle_plan_id: "edit-test-plan",+ billing_interval: :monthly,+ monthly_pageview_limit: 10_000_000,+ site_limit: 300,+ team_member_limit: 50,+ hourly_api_request_limit: 5000,+ features: [Plausible.Billing.Feature.StatsAPI, Plausible.Billing.Feature.Funnels]+ )++ {:ok, lv, _html} = live(conn, open_team(team.id, tab: :billing))++ lv+ |> element(~s|button[phx-click="edit-plan"][phx-value-id="#{plan.id}"]|)+ |> render_click()++ html = render(lv)++ assert element_exists?(html, ~s|form#save-plan[phx-submit="update-plan"]|)++ assert text_of_attr(html, ~s|input[name="enterprise_plan[paddle_plan_id]"]|, "value") ==+ "edit-test-plan"++ assert text_of_element(+ html,+ ~s|select[name="enterprise_plan[billing_interval]"] option[selected="selected"]|+ ) ==+ "monthly"++ assert text_of_attr(+ html,+ ~s|input[name="enterprise_plan[monthly_pageview_limit]"]|,+ "value"+ ) == "10000000"++ assert text_of_attr(html, ~s|input[name="enterprise_plan[site_limit]"]|, "value") == "300"++ assert text_of_attr(html, ~s|input[name="enterprise_plan[team_member_limit]"]|, "value") ==+ "50"++ assert text_of_attr(+ html,+ ~s|input[name="enterprise_plan[hourly_api_request_limit]"]|,+ "value"+ ) == "5000"++ assert element_exists?(+ html,+ ~s|input[name="enterprise_plan[features[]][stats_api]"][checked="checked"]|+ )++ assert element_exists?(+ html,+ ~s|input[name="enterprise_plan[features[]][funnels]"][checked="checked"]|+ )++ refute element_exists?(+ html,+ ~s|input[name="enterprise_plan[features[]][revenue_goals]"][checked="checked"]|+ )++ assert text(html) =~ "Update Plan"+ refute text(html) =~ "Save Custom Plan"+ end++ test "successfully updates existing plan", %{conn: conn, user: user} do+ team = team_of(user)++ plan =+ insert(:enterprise_plan,+ team: team,+ paddle_plan_id: "original-plan",+ billing_interval: :monthly,+ monthly_pageview_limit: 1_000_000,+ site_limit: 100,+ team_member_limit: 10,+ hourly_api_request_limit: 1000,+ features: [Plausible.Billing.Feature.StatsAPI]+ )++ {:ok, lv, _html} = live(conn, open_team(team.id, tab: :billing))++ lv+ |> element(~s|button[phx-click="edit-plan"][phx-value-id="#{plan.id}"]|)+ |> render_click()++ lv+ |> element(~s|form#save-plan|)+ |> render_change(%{+ "enterprise_plan" => %{+ "paddle_plan_id" => "updated-plan-456",+ "billing_interval" => "yearly",+ "monthly_pageview_limit" => "15000000",+ "site_limit" => "500",+ "team_member_limit" => "100",+ "hourly_api_request_limit" => "8000",+ "features[]" => %{+ "stats_api" => "true",+ "funnels" => "true",+ "props" => "false",+ "revenue_goals" => "false",+ "site_segments" => "false",+ "shared_links" => "false",+ "sites_api" => "false",+ "sso" => "false"+ }+ }+ })++ lv |> element("form#save-plan") |> render_submit()++ html = render(lv)+ assert text(html) =~ "Plan updated"++ updated_plan = Plausible.Repo.reload!(plan)+ assert updated_plan.paddle_plan_id == "updated-plan-456"+ assert updated_plan.billing_interval == :yearly+ assert updated_plan.monthly_pageview_limit == 15_000_000+ assert updated_plan.site_limit == 500+ assert updated_plan.team_member_limit == 100+ assert updated_plan.hourly_api_request_limit == 8000++ feature_names = Enum.map(updated_plan.features, & &1.name())+ assert :stats_api in feature_names+ assert :funnels in feature_names+ refute :shared_links in feature_names++ refute element_exists?(html, ~s|form#save-plan|)++ assert text(html) =~ "updated-plan-456"+ assert text(html) =~ "yearly"+ assert text(html) =~ "15,000,000"+ assert text(html) =~ "500"+ assert text(html) =~ "100"+ assert text(html) =~ "8,000"+ end++ test "handles validation errors when updating plan", %{conn: conn, user: user} do+ team = team_of(user)++ plan =+ insert(:enterprise_plan,+ team: team,+ paddle_plan_id: "valid-plan",+ billing_interval: :monthly,+ monthly_pageview_limit: 1_000_000,+ site_limit: 100,+ team_member_limit: 10,+ hourly_api_request_limit: 1000+ )++ {:ok, lv, _html} = live(conn, open_team(team.id, tab: :billing))++ lv+ |> element(~s|button[phx-click="edit-plan"][phx-value-id="#{plan.id}"]|)+ |> render_click()++ lv+ |> element(~s|form#save-plan|)+ |> render_submit(%{+ "enterprise_plan" => %{+ "paddle_plan_id" => "",+ "billing_interval" => "monthly",+ "monthly_pageview_limit" => "1000000",+ "site_limit" => "100",+ "team_member_limit" => "10",+ "hourly_api_request_limit" => "1000"+ }+ })++ html = render(lv)++ assert text(html) =~ "Error updating plan"+ assert element_exists?(html, ~s|form#save-plan|)++ unchanged_plan = Plausible.Repo.reload!(plan)+ assert unchanged_plan.paddle_plan_id == "valid-plan"+ end++ test "cancel edit returns to plan list", %{conn: conn, user: user} do+ team = team_of(user)++ plan = insert(:enterprise_plan, team: team)++ {:ok, lv, _html} = live(conn, open_team(team.id, tab: :billing))++ lv+ |> element(~s|button[phx-click="edit-plan"][phx-value-id="#{plan.id}"]|)+ |> render_click()++ html = render(lv)+ assert element_exists?(html, ~s|form#save-plan|)++ lv |> element(~s|button[phx-click="hide-plan-form"]|) |> render_click()++ html = render(lv)+ refute element_exists?(html, ~s|form#save-plan|)+ assert element_exists?(html, ~s|button#new-custom-plan|)+ end++ test "multiple plans can be displayed and edited independently", %{conn: conn, user: user} do+ team = team_of(user)++ plan1 =+ insert(:enterprise_plan,+ team: team,+ paddle_plan_id: "plan-1",+ monthly_pageview_limit: 1_000_000+ )++ plan2 =+ insert(:enterprise_plan,+ team: team,+ paddle_plan_id: "plan-2",+ monthly_pageview_limit: 5_000_000+ )++ {:ok, lv, _html} = live(conn, open_team(team.id, tab: :billing))+ html = render(lv)++ assert text(html) =~ "plan-1"+ assert text(html) =~ "plan-2"+ assert text(html) =~ "1,000,000"+ assert text(html) =~ "5,000,000"++ assert element_exists?(+ html,+ ~s|button[phx-click="edit-plan"][phx-value-id="#{plan1.id}"]|+ )++ assert element_exists?(+ html,+ ~s|button[phx-click="edit-plan"][phx-value-id="#{plan2.id}"]|+ )++ lv+ |> element(~s|button[phx-click="edit-plan"][phx-value-id="#{plan2.id}"]|)+ |> render_click()++ html = render(lv)++ assert text_of_attr(html, ~s|input[name="enterprise_plan[paddle_plan_id]"]|, "value") ==+ "plan-2"++ assert text_of_attr(+ html,+ ~s|input[name="enterprise_plan[monthly_pageview_limit]"]|,+ "value"+ ) == "5000000"+ end+defp open_custom_plan(conn, team) do{:ok, lv, _html} = live(conn, open_team(team.id, tab: :billing))render(lv)