Skip to content
Snippets Groups Projects
Unverified Commit 431c02ad authored by Preslav Rachev's avatar Preslav Rachev Committed by GitHub
Browse files

#332: Refactor stats controller (#337)

* #332: Refactor stats controller

* Simplify the conditional logic
parent 8d9667a9
Branches
No related tags found
No related merge requests found
......@@ -5,18 +5,13 @@ defmodule PlausibleWeb.StatsController do
alias Plausible.Stats.Query
plug PlausibleWeb.AuthorizeStatsPlug when action in [:stats, :csv_export]
plug PlausibleWeb.UpgradeBillingPlug when action in [:stats]
def base_domain() do
PlausibleWeb.Endpoint.host()
end
def stats(conn, _params) do
site = conn.assigns[:site]
user = conn.assigns[:current_user]
if user && Plausible.Billing.needs_to_upgrade?(conn.assigns[:current_user]) do
redirect(conn, to: "/settings")
else
def stats(%{assigns: %{site: site}} = conn, _params) do
if Stats.has_pageviews?(site) do
demo = site.domain == base_domain()
offer_email_report = get_session(conn, site.domain <> "_offer_email_report")
......@@ -38,7 +33,6 @@ defmodule PlausibleWeb.StatsController do
|> render("waiting_first_pageview.html", site: site)
end
end
end
def csv_export(conn, %{"domain" => domain}) do
site = conn.assigns[:site]
......
defmodule PlausibleWeb.UpgradeBillingPlug do
import Phoenix.Controller
use Plausible.Repo
def init(options) do
options
end
def call(conn, _opts) do
user = conn.assigns[:current_user]
if user && Plausible.Billing.needs_to_upgrade?(conn.assigns[:current_user]) do
conn
|> redirect(to: "/settings")
else
conn
end
end
end
......@@ -11,6 +11,13 @@ defmodule PlausibleWeb.StatsControllerTest do
assert html_response(conn, 200) =~ "stats-react-container"
end
test "public site - shows waiting for first pageview", %{conn: conn} do
insert(:site, domain: "some-other-public-site.io", public: true)
conn = get(conn, "/some-other-public-site.io")
assert html_response(conn, 200) =~ "Need to see the snippet again?"
end
test "can not view stats of a private website", %{conn: conn} do
conn = get(conn, "/test-site.com")
assert html_response(conn, 404) =~ "There&#39;s nothing here"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment