Bugfix: Allow breakdown by internally used prop keys for Growth plans (#3562)

* fix bug - allow internally used prop key breakdown for Growth plans

* use case instead of with
This commit is contained in:
RobertJoonas 2023-11-28 09:30:35 +00:00 committed by GitHub
parent be48ca2826
commit ff2c3346d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 12 deletions

View File

@ -96,6 +96,14 @@ defmodule Plausible.Props do
"""
def internal_keys, do: @internal_keys
def ensure_prop_key_accessible(prop_key, user) do
if prop_key in @internal_keys do
:ok
else
Plausible.Billing.Feature.Props.check_availability(user)
end
end
@spec suggest_keys_to_allow(Plausible.Site.t(), non_neg_integer()) :: [String.t()]
@doc """
Queries the events table to fetch the #{@max_props} most frequent prop keys

View File

@ -1185,8 +1185,9 @@ defmodule PlausibleWeb.Api.StatsController do
def custom_prop_values(conn, params) do
site = Plausible.Repo.preload(conn.assigns.site, :owner)
prop_key = Map.fetch!(params, "prop_key")
case Plausible.Billing.Feature.Props.check_availability(site.owner) do
case Plausible.Props.ensure_prop_key_accessible(prop_key, site.owner) do
:ok ->
props = breakdown_custom_prop_values(site, params)
json(conn, props)

View File

@ -177,17 +177,6 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do
}
]
end
test "errors when site owner is on a growth plan", %{conn: conn, site: site, user: user} do
insert(:growth_subscription, user: user)
conn = get(conn, "/api/stats/#{site.domain}/custom-prop-values/prop?period=day")
assert json_response(conn, 402) == %{
"error" =>
"Custom Properties is part of the Plausible Business plan. To get access to this feature, please upgrade your account."
}
end
end
describe "GET /api/stats/:domain/custom-prop-values/:prop_key - with goal filter" do
@ -1058,4 +1047,42 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do
]
end
end
describe "GET /api/stats/:domain/custom-prop-values/:prop_key - for a Growth subscription" do
setup [:create_user, :log_in, :create_new_site]
setup %{user: user, site: site} do
insert(:growth_subscription, user: user)
populate_stats(site, [
build(:pageview,
"meta.key": ["url", "path", "author"],
"meta.value": ["one", "two", "three"]
)
])
:ok
end
test "returns breakdown for internally used prop keys", %{conn: conn, site: site} do
[%{"visitors" => 1, "name" => "one"}] =
conn
|> get("/api/stats/#{site.domain}/custom-prop-values/url?period=day")
|> json_response(200)
[%{"visitors" => 1, "name" => "two"}] =
conn
|> get("/api/stats/#{site.domain}/custom-prop-values/path?period=day")
|> json_response(200)
end
test "returns 402 'upgrade required' for any other prop key", %{conn: conn, site: site} do
conn = get(conn, "/api/stats/#{site.domain}/custom-prop-values/prop?period=day")
assert json_response(conn, 402) == %{
"error" =>
"Custom Properties is part of the Plausible Business plan. To get access to this feature, please upgrade your account."
}
end
end
end