diff --git a/lib/plausible/teams/invitations.ex b/lib/plausible/teams/invitations.ex index 40e2d639c4ad8..116b6713679a4 100644 --- a/lib/plausible/teams/invitations.ex +++ b/lib/plausible/teams/invitations.ex @@ -9,6 +9,51 @@ defmodule Plausible.Teams.Invitations do alias Plausible.Repo alias Plausible.Teams + def find_by_id_unified(invitation_or_transfer_id) do + with {:error, :invitation_not_found} <- + find_invitation_by_id_unified(invitation_or_transfer_id) do + find_transfer_by_id_unified(invitation_or_transfer_id) + end + end + + defp find_invitation_by_id_unified(id) do + invitation = + Teams.GuestInvitation + |> Repo.get_by(invitation_id: id) + |> Repo.preload([:site, team_invitation: :inviter]) + + case invitation do + nil -> + {:error, :invitation_not_found} + + guest_invitation -> + {:ok, + %{ + role: guest_invitation.role, + email: guest_invitation.team_invitation.email + }} + end + end + + defp find_transfer_by_id_unified(id) do + transfer = + Teams.SiteTransfer + |> Repo.get_by(transfer_id: id) + |> Repo.preload([:site, :initiator]) + + case transfer do + nil -> + {:error, :invitation_not_found} + + transfer -> + {:ok, + %{ + role: :owner, + email: transfer.email + }} + end + end + def find_for_user(invitation_or_transfer_id, user) do with {:error, :invitation_not_found} <- find_invitation_for_user(invitation_or_transfer_id, user) do diff --git a/lib/plausible_web/live/register_form.ex b/lib/plausible_web/live/register_form.ex index 04e39a7033252..4ba66a73f0285 100644 --- a/lib/plausible_web/live/register_form.ex +++ b/lib/plausible_web/live/register_form.ex @@ -7,12 +7,16 @@ defmodule PlausibleWeb.Live.RegisterForm do alias Plausible.Auth alias Plausible.Repo + alias Plausible.Teams def mount(params, _session, socket) do socket = assign_new(socket, :invitation, fn -> if invitation_id = params["invitation_id"] do - Repo.get_by(Auth.Invitation, invitation_id: invitation_id) + case find_by_id_unified(invitation_id) do + {:error, :invitation_not_found} -> nil + {:ok, unified} -> unified + end end end) @@ -340,4 +344,49 @@ defmodule PlausibleWeb.Live.RegisterForm do )} end end + + defp find_by_id_unified(invitation_or_transfer_id) do + with {:error, :invitation_not_found} <- + find_invitation_by_id_unified(invitation_or_transfer_id) do + find_transfer_by_id_unified(invitation_or_transfer_id) + end + end + + defp find_invitation_by_id_unified(id) do + invitation = + Teams.GuestInvitation + |> Repo.get_by(invitation_id: id) + |> Repo.preload([:site, team_invitation: :inviter]) + + case invitation do + nil -> + {:error, :invitation_not_found} + + guest_invitation -> + {:ok, + %{ + role: guest_invitation.role, + email: guest_invitation.team_invitation.email + }} + end + end + + defp find_transfer_by_id_unified(id) do + transfer = + Teams.SiteTransfer + |> Repo.get_by(transfer_id: id) + |> Repo.preload([:site, :initiator]) + + case transfer do + nil -> + {:error, :invitation_not_found} + + transfer -> + {:ok, + %{ + role: :owner, + email: transfer.email + }} + end + end end diff --git a/test/plausible_web/live/register_form_test.exs b/test/plausible_web/live/register_form_test.exs index c946140e1733d..b4b05037ba79e 100644 --- a/test/plausible_web/live/register_form_test.exs +++ b/test/plausible_web/live/register_form_test.exs @@ -5,6 +5,8 @@ defmodule PlausibleWeb.Live.RegisterFormTest do import Phoenix.LiveViewTest import Plausible.Test.Support.HTML + use Plausible.Teams.Test + alias Plausible.Auth.User alias Plausible.Repo @@ -138,16 +140,11 @@ defmodule PlausibleWeb.Live.RegisterFormTest do describe "/register/invitation/:invitation_id" do setup do - inviter = insert(:user) - site = insert(:site, members: [inviter]) + inviter = new_user() + site = new_site(owner: inviter) invitation = - insert(:invitation, - site_id: site.id, - inviter: inviter, - email: "user@email.co", - role: :admin - ) + invite_guest(site, "user@email.co", role: :editor, inviter: inviter) {:ok, %{site: site, invitation: invitation, inviter: inviter}} end @@ -183,13 +180,14 @@ defmodule PlausibleWeb.Live.RegisterFormTest do assert text_of_attr(password_input, "value") == "very-long-and-very-secret-123" assert text_of_attr(password_confirmation_input, "value") == "very-long-and-very-secret-123" - assert %{ - name: "Mary Sue", - email: "user@email.co", - password_hash: password_hash, - # leaves trial_expiry_date null when invitation role is not :owner - trial_expiry_date: nil - } = Repo.get_by(User, email: "user@email.co") + assert user = + %{ + name: "Mary Sue", + email: "user@email.co", + password_hash: password_hash + } = Repo.get_by(User, email: "user@email.co") + + assert team_of(user) == nil assert String.length(password_hash) > 0 end @@ -201,15 +199,9 @@ defmodule PlausibleWeb.Live.RegisterFormTest do } do mock_captcha_success() - invitation = - insert(:invitation, - site_id: site.id, - inviter: inviter, - email: "owner_user@email.co", - role: :owner - ) + invitation = invite_transfer(site, "owner_user@email.co", inviter: inviter) - lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}") + lv = get_liveview(conn, "/register/invitation/#{invitation.transfer_id}") type_into_input(lv, "user[name]", "Mary Sue") type_into_input(lv, "user[password]", "very-long-and-very-secret-123") @@ -217,12 +209,9 @@ defmodule PlausibleWeb.Live.RegisterFormTest do _html = lv |> element("form") |> render_submit() - assert %{ - email: "owner_user@email.co", - trial_expiry_date: trial_expiry_date - } = Repo.get_by(User, email: "owner_user@email.co") + assert user = Repo.get_by(User, email: "owner_user@email.co") - assert trial_expiry_date != nil + assert team_of(user).trial_expiry_date != nil end test "always uses original email from the invitation", %{conn: conn, invitation: invitation} do