From 3b28a8d418f05e06723cea9ca70229397ebd01d5 Mon Sep 17 00:00:00 2001 From: Vini Brasil Date: Tue, 31 Oct 2023 14:57:21 -0300 Subject: [PATCH] Warn user about Stats API when downgrading (#3471) * Warn user about Stats API when downgrading * Update test/plausible/billing/quota_test.exs Co-authored-by: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> --------- Co-authored-by: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> --- lib/plausible/billing/quota.ex | 7 +++++-- test/plausible/billing/quota_test.exs | 7 +++++++ test/plausible_web/live/choose_plan_test.exs | 5 +++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex index cc5c6f37c..d11f7333e 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 c16e6e9c5..157be54b3 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 938d857e8..f2c34a164 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