Skip to content

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

Warn user about Stats API when downgrading (#3471)

Vini Brasil3b28a8d418f0
diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex
index cc5c6f37cc..d11f7333e4 100644
--- a/lib/plausible/billing/quota.ex
+++ b/lib/plausible/billing/quota.ex
@@ -6,7 +6,7 @@ defmodule Plausible.Billing.Quota do
import Ecto.Query
alias Plausible.Billing
alias Plausible.Billing.{Plan, Plans, Subscription, EnterprisePlan, Feature}
- alias Plausible.Billing.Feature.{Goals, RevenueGoals, Funnels, Props}
+ alias Plausible.Billing.Feature.{Goals, RevenueGoals, Funnels, Props, StatsAPI}
def usage(user, opts \\ []) do
basic_usage = %{
@@ -177,10 +177,13 @@ defmodule Plausible.Billing.Quota do
on: g.site_id == os.site_id,
where: not is_nil(g.currency)
+ stats_api_usage = from a in Plausible.Auth.ApiKey, where: a.user_id == ^user.id
+
queries = [
{Props, props_usage_query},
{Funnels, funnels_usage_query},
- {RevenueGoals, revenue_goals_usage}
+ {RevenueGoals, revenue_goals_usage},
+ {StatsAPI, stats_api_usage}
]
Enum.reduce(queries, [], fn {feature, query}, acc ->
diff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exs
index c16e6e9c5e..157be54b34 100644
--- a/test/plausible/billing/quota_test.exs
+++ b/test/plausible/billing/quota_test.exs
@@ -426,6 +426,13 @@ defmodule Plausible.Billing.QuotaTest do
assert [RevenueGoals] == Quota.features_usage(user)
end
+ test "returns [StatsAPI] when user has a stats api key" do
+ user = insert(:user)
+ insert(:api_key, user: user)
+
+ assert [StatsAPI] == Quota.features_usage(user)
+ end
+
test "returns multiple features" do
user = insert(:user)
diff --git a/test/plausible_web/live/choose_plan_test.exs b/test/plausible_web/live/choose_plan_test.exs
index 938d857e86..f2c34a1642 100644
--- a/test/plausible_web/live/choose_plan_test.exs
+++ b/test/plausible_web/live/choose_plan_test.exs
@@ -438,14 +438,15 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do
test "warns about losing access to a feature", %{conn: conn, user: user} do
site = insert(:site, members: [user])
- Plausible.Props.allow(site, ["author"])
+ Plausible.Props.allow(site, ["author"])
insert(:goal, currency: :USD, site: site, event_name: "Purchase")
+ insert(:api_key, user: user)
{:ok, _lv, doc} = get_liveview(conn)
assert text_of_attr(find(doc, @growth_checkout_button), "onclick") =~
- "if (!confirm(\"This plan does not support Custom Properties and Revenue Goals, which you are currently using. Please note that by subscribing to this plan you will lose access to these features.\")) {e.preventDefault()}"
+ "if (!confirm(\"This plan does not support Custom Properties, Revenue Goals and Stats API, which you are currently using. Please note that by subscribing to this plan you will lose access to these features.\")) {e.preventDefault()}"
end
end