Skip to content

Commit

Permalink
Return error when inviting an existing collaborator to a project
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Sep 3, 2024
1 parent 396a792 commit 5952e19
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
39 changes: 31 additions & 8 deletions lib/ask_web/controllers/invite_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,27 @@ defmodule AskWeb.InviteController do

recipient_user = Repo.one(from u in Ask.User, where: u.email == ^email)

if recipient_user do
result = if recipient_user do
notify_access_to_user(conn, recipient_user, current_user, email, code, project, level)
else
send_invitation_email(code, level, email, project, conn)
end

render(conn, "invite.json", %{project_id: project.id, code: code, email: email, level: level})
case result do
{:ok, _} ->
render(conn, "invite.json", %{
project_id: project.id,
code: code,
email: email,
level: level
})

{:error, _, error_changeset, _} ->
conn
|> put_status(:unprocessable_entity)
|> put_view(AskWeb.ChangesetView)
|> render("error.json", changeset: error_changeset)
end
end

def update(_, %{"level" => "owner"}) do
Expand Down Expand Up @@ -287,13 +301,22 @@ defmodule AskWeb.InviteController do
AskWeb.Email.notify(level, email, current_user, url, project)
|> Ask.Mailer.deliver()

Ask.Repo.transaction(fn ->
ProjectMembership.changeset(%ProjectMembership{}, changeset) |> Repo.insert()
changeset =
ProjectMembership.changeset(%ProjectMembership{}, changeset)

if invite do
invite |> Repo.delete!()
end
end)
Multi.new()
|> Multi.insert(:insert_project_membership, changeset)
|> delete_invite(invite)
|> Repo.transaction()
end

defp delete_invite(multi, nil) do
multi
end

defp delete_invite(multi, invite) do
multi
|> Multi.delete(:delete_invite, invite)
end

defp send_invitation_email(code, level, email, project, conn) do
Expand Down
26 changes: 25 additions & 1 deletion test/ask_web/controllers/invite_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ defmodule AskWeb.InviteControllerTest do
end
end

test "send invite to existing user", %{conn: conn, user: user} do
test "send invite to new user", %{conn: conn, user: user} do
project = create_project_for_user(user)
code = "ABC1234"
level = "reader"
Expand All @@ -1091,4 +1091,28 @@ defmodule AskWeb.InviteControllerTest do

assert json_response(conn, 200)
end

test "send invite to existing user", %{conn: conn, user: user} do
project = create_project_for_user(user)
code = "ABC1234"
level = "reader"
email = "[email protected]"

user2 = insert(:user, email: email)
insert(:project_membership, user_id: user2.id, project_id: project.id, level: "editor")

conn = get(
conn,
send_invitation_path(conn, :send_invitation, %{
"code" => code,
"level" => level,
"email" => user2.email,
"project_id" => project.id
})
)

assert_email_sent(subject: "#{user.name} has added you as a collaborator on #{project.name}.")

assert json_response(conn, 422)
end
end

0 comments on commit 5952e19

Please sign in to comment.