Skip to content

Commit

Permalink
Implement domain change via Sites API
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed Mar 30, 2023
1 parent 3078d3b commit 5ea9b19
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/plausible/site/domain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ defmodule Plausible.Site.Domain do

@spec change(Site.t(), String.t(), Keyword.t()) ::
{:ok, Site.t()} | {:error, Ecto.Changeset.t()}
def change(site = %Site{}, new_domain, opts \\ []) when is_binary(new_domain) do
def change(site = %Site{}, new_domain, opts \\ []) do
changeset = Site.update_changeset(site, %{domain: new_domain}, opts)

changeset =
if Enum.empty?(changeset.changes) do
if Enum.empty?(changeset.changes) and is_nil(changeset.errors[:domain]) do
Ecto.Changeset.add_error(
changeset,
:domain,
"New domain must be different than your current one."
"New domain must be different than the current one"
)
else
changeset
Expand Down
19 changes: 19 additions & 0 deletions lib/plausible_web/controllers/api/external_sites_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
end
end

def update_site(conn, %{"site_id" => site_id} = params) do

This comment has been minimized.

Copy link
@ukutaht

ukutaht Mar 30, 2023

Contributor

Seems like this endpoint is missing tests

This comment has been minimized.

Copy link
@aerosol

aerosol Mar 30, 2023

Author Member

🤦 I must've undone them by mistake. Thanks for catching that

This comment has been minimized.

Copy link
@aerosol

aerosol Mar 31, 2023

Author Member

And found them in vim's persistent undo 😂 b6bbf2b

# for now this only allows to change the domain
site = Sites.get_for_user(conn.assigns[:current_user].id, site_id, [:owner, :admin])

if site && Plausible.v2?() do
case Plausible.Site.Domain.change(site, params["domain"]) do
{:ok, site} ->
json(conn, site)

{:error, changeset} ->
conn
|> put_status(400)
|> json(serialize_errors(changeset))
end
else
H.not_found(conn, "Site could not be found")
end
end

defp expect_param_key(params, key) do
case Map.fetch(params, key) do
:error -> {:missing, key}
Expand Down
5 changes: 3 additions & 2 deletions lib/plausible_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ defmodule PlausibleWeb.Router do
pipe_through [:public_api, PlausibleWeb.AuthorizeSitesApiPlug]

post "/", ExternalSitesController, :create_site
get "/:site_id", ExternalSitesController, :get_site
delete "/:site_id", ExternalSitesController, :delete_site
put "/shared-links", ExternalSitesController, :find_or_create_shared_link
put "/goals", ExternalSitesController, :find_or_create_goal
delete "/goals/:goal_id", ExternalSitesController, :delete_goal
get "/:site_id", ExternalSitesController, :get_site
put "/:site_id", ExternalSitesController, :update_site
delete "/:site_id", ExternalSitesController, :delete_site
end

scope "/api", PlausibleWeb do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
describe "GET /api/v1/sites/:site_id" do
setup :create_new_site

@tag :v2_only
test "get a site by its domain", %{conn: conn, site: site} do
conn = get(conn, "/api/v1/sites/" <> site.domain)

Expand All @@ -499,6 +500,7 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
assert json_response(conn, 200) == %{"domain" => new_domain, "timezone" => site.timezone}
end

@tag :v2_only
test "is 404 when site cannot be found", %{conn: conn} do
conn = get(conn, "/api/v1/sites/foobar.baz")

Expand Down
2 changes: 1 addition & 1 deletion test/plausible_web/controllers/site_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ defmodule PlausibleWeb.SiteControllerTest do
})

resp = html_response(conn, 200)
assert resp =~ "New domain must be different than your current one."
assert resp =~ "New domain must be different than the current one"
end

@tag :v2_only
Expand Down

0 comments on commit 5ea9b19

Please sign in to comment.