- Escape HEEx template braces in GraphQL/API docs with raw(~S[...]) - Fix test assertions for updated marketing copy and UI text - Extract helper functions in GraphQL resolvers to reduce nesting depth - Create shared ErrorHelpers module for API controllers - Fix ETS race condition in brute force whitelist cache for async tests - Fix property test generators to use ASCII instead of printable unicode - Add alert_severity helper to site_live/show - Update accounts fixtures for explicit user confirmation
41 lines
1.2 KiB
Elixir
41 lines
1.2 KiB
Elixir
defmodule ToweropsWeb.InvitationController do
|
|
use ToweropsWeb, :controller
|
|
|
|
alias Towerops.Organizations
|
|
|
|
def show(conn, %{"token" => token}) do
|
|
case Organizations.get_invitation_by_token(token) do
|
|
nil ->
|
|
conn
|
|
|> put_flash(:error, "This invitation is invalid or has expired.")
|
|
|> redirect(to: ~p"/")
|
|
|
|
invitation ->
|
|
accept_or_redirect(conn, invitation, token)
|
|
end
|
|
end
|
|
|
|
defp accept_or_redirect(conn, invitation, token) do
|
|
case conn.assigns[:current_scope] do
|
|
%{user: user} when not is_nil(user) ->
|
|
accept_invitation(conn, invitation, user)
|
|
|
|
_ ->
|
|
redirect(conn, to: ~p"/users/register?invitation_token=#{token}")
|
|
end
|
|
end
|
|
|
|
defp accept_invitation(conn, invitation, user) do
|
|
case Organizations.accept_invitation(invitation, user.id) do
|
|
{:ok, _membership} ->
|
|
conn
|
|
|> put_flash(:info, "You've joined #{invitation.organization.name}!")
|
|
|> redirect(to: ~p"/orgs/#{invitation.organization.slug}/settings?tab=members")
|
|
|
|
{:error, _changeset} ->
|
|
conn
|
|
|> put_flash(:error, "Could not accept invitation. You may already be a member.")
|
|
|> redirect(to: ~p"/dashboard")
|
|
end
|
|
end
|
|
end
|