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
Allow over-limit site ownership transfer for users having grandfathered plans (#5026)
hq194799b6d7a2d
diff --git a/lib/plausible/billing/qouta/quota.ex b/lib/plausible/billing/qouta/quota.exindex 54b12010be..7ad415eb04 100644--- a/lib/plausible/billing/qouta/quota.ex+++ b/lib/plausible/billing/qouta/quota.ex@@ -68,9 +68,16 @@ defmodule Plausible.Billing.Quota doenddefp exceeded_limits(usage, plan, opts) do+ site_limit_exceeded? =+ if opts[:skip_site_limit_check?] do+ false+ else+ not within_limit?(usage.sites, plan.site_limit)+ end+for {limit, exceeded?} <- [{:team_member_limit, not within_limit?(usage.team_members, plan.team_member_limit)},- {:site_limit, not within_limit?(usage.sites, plan.site_limit)},+ {:site_limit, site_limit_exceeded?},{:monthly_pageview_limit,exceeds_monthly_pageview_limit?(usage.monthly_pageviews, plan, opts)}],diff --git a/lib/plausible/teams/billing.ex b/lib/plausible/teams/billing.exindex e168fc1f5d..dc0e939b8b 100644--- a/lib/plausible/teams/billing.ex+++ b/lib/plausible/teams/billing.ex@@ -35,6 +35,13 @@ defmodule Plausible.Teams.Billing do@typep last_30_days_usage() :: %{:last_30_days => usage_cycle()}@typep monthly_pageview_usage() :: cycles_usage() | last_30_days_usage()+ def grandfathered_team?(nil), do: false++ def grandfathered_team?(team) do+ # timestamps were originally rewritten from owner.inserted_at+ Date.before?(team.inserted_at, @limit_sites_since)+ end+def get_subscription(nil), do: nildef get_subscription(%Teams.Team{subscription: %Subscription{} = subscription}),@@ -183,9 +190,7 @@ defmodule Plausible.Teams.Billing doenddef site_limit(team) do- {:ok, user} = Teams.get_owner(team)-- if Timex.before?(user.inserted_at, @limit_sites_since) do+ if grandfathered_team?(team) do:unlimitedelseget_site_limit_from_plan(team)diff --git a/lib/plausible/teams/invitations.ex b/lib/plausible/teams/invitations.exindex 041f47812c..42ce31b705 100644--- a/lib/plausible/teams/invitations.ex+++ b/lib/plausible/teams/invitations.ex@@ -436,7 +436,9 @@ defmodule Plausible.Teams.Invitations doif active_subscription? and plan != :free_10k doteam|> Teams.Billing.quota_usage(pending_ownership_site_ids: [site.id])- |> Billing.Quota.ensure_within_plan_limits(plan)+ |> Billing.Quota.ensure_within_plan_limits(plan,+ skip_site_limit_check?: Teams.Billing.grandfathered_team?(team)+ )else{:error, :no_plan}enddiff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exsindex 1028a62fa6..e8e1253ee4 100644--- a/test/plausible/billing/quota_test.exs+++ b/test/plausible/billing/quota_test.exs@@ -67,6 +67,23 @@ defmodule Plausible.Billing.QuotaTest doassert Plausible.Teams.Billing.site_limit(team) == 10end++ test "grandfathered site limit should be unlimited when accepting transfer invitations" do+ # must be before ~D[2021-05-05]+ owner = new_user(team: [inserted_at: ~N[2021-01-01 00:00:00]])+ # plan with site_limit: 10+ subscribe_to_plan(owner, "857097")+ _site = for _ <- 1..10, do: new_site(owner: owner)++ other_owner = new_user()+ other_site = new_site(owner: other_owner)+ invite_transfer(other_site, owner, inviter: other_owner)++ team = owner |> team_of()++ assert Plausible.Teams.Billing.site_limit(team) == :unlimited+ assert Plausible.Teams.Invitations.ensure_can_take_ownership(other_site, team) == :ok+ endendtest "site_usage/1 returns the amount of sites the user owns" dodiff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exsindex 9f3f2a029f..6a94657d16 100644--- a/test/plausible_web/controllers/site_controller_test.exs+++ b/test/plausible_web/controllers/site_controller_test.exs@@ -324,7 +324,7 @@ defmodule PlausibleWeb.SiteControllerTest dofor _ <- 1..51, do: new_site(owner: user)- Ecto.Changeset.change(user, %{inserted_at: ~N[2021-05-04 00:00:00]})+ Ecto.Changeset.change(team_of(user), %{inserted_at: ~N[2021-05-04 00:00:00]})|> Repo.update()conn =