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
Refactor free billing features logic (#5682)
Adrian Gruntkowskieed90a343e22
diff --git a/extra/lib/plausible_web/live/customer_support/team/components/billing.ex b/extra/lib/plausible_web/live/customer_support/team/components/billing.exindex 6a347ab9b2..41971292fa 100644--- a/extra/lib/plausible_web/live/customer_support/team/components/billing.ex+++ b/extra/lib/plausible_web/live/customer_support/team/components/billing.ex@@ -190,7 +190,7 @@ defmodule PlausibleWeb.CustomerSupport.Team.Components.Billing doPlausible.Billing.Feature.list()|> Enum.sort_by(fn item -> if item.name() == :stats_api, do: 0, else: 1 end)}- :if={not mod.free?()}+ :if={mod not in Teams.Billing.free_features()}x-on:change="featureChangeCallback(event)"type="checkbox"value={mod in (f.source.changes[:features] || f.source.data.features || [])}diff --git a/lib/plausible/billing/feature.ex b/lib/plausible/billing/feature.exindex 86af601758..2117406509 100644--- a/lib/plausible/billing/feature.ex+++ b/lib/plausible/billing/feature.ex@@ -17,9 +17,6 @@ defmodule Plausible.Billing.Feature do* `:toggle_field` - the field in the %Plausible.Site{} schema that togglesthe feature. If `nil` or not set, toggle/2 silently returns `:ok`- * `:free` - if set to `true`, makes the `check_availability/1` function- always return `:ok` (no matter the user's subscription status)-Functions defined by `__using__` can be overridden if needed."""@@ -38,11 +35,6 @@ defmodule Plausible.Billing.Feature do"""@callback toggle_field() :: atom()- @doc """- Returns whether the feature is free to use or not.- """- @callback free?() :: boolean()-@doc """Toggles the feature on and off for a site. Returns`{:error, :upgrade_required}` when toggling a feature the site owner does not@@ -119,9 +111,6 @@ defmodule Plausible.Billing.Feature do@impl truedef toggle_field, do: Keyword.get(unquote(opts), :toggle_field)- @impl true- def free?, do: Keyword.get(unquote(opts), :free, false)-@impl truedef enabled?(%Plausible.Site{} = site) dosite = Plausible.Repo.preload(site, :team)@@ -135,10 +124,10 @@ defmodule Plausible.Billing.Feature do@impl truedef check_availability(team_or_nil) do- cond do- free?() -> :ok- __MODULE__ in Plausible.Teams.Billing.allowed_features_for(team_or_nil) -> :ok- true -> {:error, :upgrade_required}+ if __MODULE__ in Plausible.Teams.Billing.allowed_features_for(team_or_nil) do+ :ok+ else+ {:error, :upgrade_required}endend@@ -188,8 +177,7 @@ defmodule Plausible.Billing.Feature.Goals douse Plausible.Billing.Feature,name: :goals,display_name: "Goals",- toggle_field: :conversions_enabled,- free: true+ toggle_field: :conversions_enabledenddefmodule Plausible.Billing.Feature.Props dodiff --git a/lib/plausible/teams/billing.ex b/lib/plausible/teams/billing.exindex 41847682ff..7bd75713cd 100644--- a/lib/plausible/teams/billing.ex+++ b/lib/plausible/teams/billing.ex@@ -16,11 +16,16 @@ defmodule Plausible.Teams.Billing dorequire Plausible.Billing.Subscription.Status+ # Features that are always available, regardless of team plan+ @free_features [Goals]+@limit_sites_since ~D[2021-05-05]@typep last_30_days_usage() :: %{:last_30_days => Quota.usage_cycle()}@typep monthly_pageview_usage() :: Quota.cycles_usage() | last_30_days_usage()+ def free_features(), do: @free_features+def grandfathered_team?(nil), do: falsedef grandfathered_team?(team) do@@ -621,29 +626,34 @@ defmodule Plausible.Teams.Billing doenddef allowed_features_for(nil) do- [Goals]+ @free_featuresenddef allowed_features_for(team) doteam = Teams.with_subscription(team)- case Plans.get_subscription_plan(team.subscription) do- %EnterprisePlan{features: features} ->- features+ features =+ case Plans.get_subscription_plan(team.subscription) do+ %EnterprisePlan{features: features} ->+ features- %Plan{features: features} ->- features+ %Plan{features: features} ->+ features- :free_10k ->- [Goals, Props, StatsAPI, SharedLinks]+ :free_10k ->+ [Props, StatsAPI, SharedLinks]- nil ->- if Teams.on_trial?(team) do- Feature.list() -- [SitesAPI, SSO]- else- [Goals]- end- end+ nil ->+ if Teams.on_trial?(team) do+ Feature.list() -- [SitesAPI, SSO]+ else+ []+ end+ end++ features+ |> Enum.concat(@free_features)+ |> Enum.uniq()enddefp active_subscription_query(team) dodiff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exsindex af26d96d8e..f566181580 100644--- a/test/plausible/billing/quota_test.exs+++ b/test/plausible/billing/quota_test.exs@@ -590,7 +590,7 @@ defmodule Plausible.Billing.QuotaTest dosubscribe_to_plan(user, "free_10k")team = team_of(user)- assert [Goals, Props, StatsAPI, SharedLinks] ==+ assert [Props, StatsAPI, SharedLinks, Goals] ==Plausible.Teams.Billing.allowed_features_for(team)end@@ -613,7 +613,8 @@ defmodule Plausible.Billing.QuotaTest doassert [Plausible.Billing.Feature.StatsAPI,Plausible.Billing.Feature.Funnels,- Plausible.Billing.Feature.SharedLinks+ Plausible.Billing.Feature.SharedLinks,+ Plausible.Billing.Feature.Goals] ==Plausible.Teams.Billing.allowed_features_for(team)end@@ -662,7 +663,7 @@ defmodule Plausible.Billing.QuotaTest doteam = team_of(user)- assert [Plausible.Billing.Feature.StatsAPI] ==+ assert [Plausible.Billing.Feature.StatsAPI, Plausible.Billing.Feature.Goals] ==Plausible.Teams.Billing.allowed_features_for(team)end@@ -677,7 +678,8 @@ defmodule Plausible.Billing.QuotaTest doassert [Plausible.Billing.Feature.StatsAPI,- Plausible.Billing.Feature.SitesAPI+ Plausible.Billing.Feature.SitesAPI,+ Plausible.Billing.Feature.Goals] ==Plausible.Teams.Billing.allowed_features_for(team)end