Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve transfer ownership error message #2651

Merged
merged 6 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- Fix breakdown API pagination when using event metrics plausible/analytics#2562
- Automatically update all visible dashboard reports in the realtime view
- Connect via TLS when using HTTPS scheme in ClickHouse URL plausible/analytics#2570
- Add error message in case a transfer to an invited (but not joined) user is requested plausible/analytics#2651
- Fix bug with [showing property breakdown with a prop filter](https://github.com/plausible/analytics/issues/1789)
- Fix bug when combining goal and prop filters plausible/analytics#2654

Expand Down
13 changes: 13 additions & 0 deletions lib/plausible/helpers/changeset.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Plausible.ChangesetHelpers do
@moduledoc "Helper function for working with Ecto changesets"

def traverse_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
opts
|> Keyword.get(String.to_existing_atom(key), key)
|> to_string()
end)
end)
end
end
14 changes: 2 additions & 12 deletions lib/plausible_web/controllers/api/external_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule PlausibleWeb.Api.ExternalController do
conn
|> put_resp_header("x-plausible-dropped", "#{Enum.count(dropped)}")
|> put_status(400)
|> json(%{errors: traverse_errors(first_invalid_changeset)})
|> json(%{errors: Plausible.ChangesetHelpers.traverse_errors(first_invalid_changeset)})
else
conn
|> put_resp_header("x-plausible-dropped", "#{Enum.count(dropped)}")
Expand All @@ -38,7 +38,7 @@ defmodule PlausibleWeb.Api.ExternalController do
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_status(400)
|> json(%{errors: traverse_errors(changeset)})
|> json(%{errors: Plausible.ChangesetHelpers.traverse_errors(changeset)})
end
end

Expand Down Expand Up @@ -102,14 +102,4 @@ defmodule PlausibleWeb.Api.ExternalController do
end
end)
end

defp traverse_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
opts
|> Keyword.get(String.to_existing_atom(key), key)
|> to_string()
end)
end)
end
end
35 changes: 27 additions & 8 deletions lib/plausible_web/controllers/site/membership_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,41 @@ defmodule PlausibleWeb.Site.MembershipController do
site = Sites.get_for_user!(conn.assigns[:current_user].id, site_domain)
user = Plausible.Auth.find_user_by(email: email)

invitation =
invite_result =
Invitation.new(%{
email: email,
role: :owner,
site_id: site.id,
inviter_id: conn.assigns[:current_user].id
})
|> Repo.insert!()
|> Repo.preload([:site, :inviter])
|> Repo.insert()

PlausibleWeb.Email.ownership_transfer_request(invitation, user)
|> Plausible.Mailer.send()
conn =
case invite_result do
{:ok, invitation} ->
invitation
|> Repo.preload([:site, :inviter])
|> PlausibleWeb.Email.ownership_transfer_request(user)
|> Plausible.Mailer.send()

put_flash(conn, :success, "Site transfer request has been sent to #{email}")

{:error, changeset} ->
errors = Plausible.ChangesetHelpers.traverse_errors(changeset)

message =
case errors do
%{invitation: ["already sent" | _]} -> "Invitation has already been sent"
Copy link
Contributor Author

@ruslandoga ruslandoga Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid failing tests, changing too much in a single PR, and to still show a reasonable error message, I decided to reuse the approach used for duplicate invitations above:

{"already sent", _} ->
"This invitation has been already sent. To send again, remove it from pending invitations first."

_other -> "Site transfer request to #{email} has failed"
ruslandoga marked this conversation as resolved.
Show resolved Hide resolved
end

conn
|> put_flash(:ttl, :timer.seconds(5))
|> put_flash(:error_title, "Transfer error")
|> put_flash(:error, message)
end

conn
|> put_flash(:success, "Site transfer request has been sent to #{email}")
|> redirect(to: Routes.site_path(conn, :settings_people, site.domain))
redirect(conn, to: Routes.site_path(conn, :settings_people, site.domain))
end

@doc """
Expand Down
27 changes: 27 additions & 0 deletions test/plausible_web/controllers/site/membership_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,33 @@ defmodule PlausibleWeb.Site.MembershipControllerTest do

refute Repo.get_by(Plausible.Auth.Invitation, email: "john.doe@example.com")
end

test "fails to transfer ownership to invited user with proper error message", ctx do
%{conn: conn, user: user} = ctx
site = insert(:site, members: [user])
invited = "john.doe@example.com"

# invite a user but don't join

conn =
post(conn, "/sites/#{site.domain}/memberships/invite", %{
email: invited,
role: "admin"
})

conn = get(recycle(conn), redirected_to(conn, 302))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: recycle, nice


assert html_response(conn, 200) =~
"#{invited} has been invited to #{site.domain} as an admin"

# transferring ownership to that domain now fails

conn = post(conn, "/sites/#{site.domain}/transfer-ownership", %{email: invited})
conn = get(recycle(conn), redirected_to(conn, 302))
html = html_response(conn, 200)
assert html =~ "Transfer error"
assert html =~ "Invitation has already been sent"
end
end

describe "PUT /sites/memberships/:id/role/:new_role" do
Expand Down