 
           redirect(conn, to: invitation_path(conn, :show, invitation.event.slug, invitation.invitation_token))
         end
-      nil ->
-        conn
-        |> put_flash(:error, "Invalid invitation.")
-        |> redirect(to: page_path(conn, :index))
+      nil -> handle_error(conn, "Invalid invitation.")
     end
   end
 
   # Private methods
   defp load_event(conn) do
     Repo.get_by!(Event, slug: conn.params["event_slug"])
+  end
+
+  defp handle_error(conn, flash_message) do
+    conn
+    |> put_flash(:error, flash_message)
+    |> redirect(to: page_path(conn, :index))
   end
 
   defp create_user(%Invitation{} = invitation) do

        conn
        |> put_flash(:info, "Invitation completed.")
        |> render("show.html", invitation: invitation)
      nil ->
        conn
        |> put_flash(:error, "Invalid invitation.")
        |> redirect(to: page_path(conn, :index))
    end
  end


    end
  end

  defp invitation_has_membership?(invitation, user) do
    membership_res =
      Membership
      |> Membership.for_event(invitation.event)
      |> Query.where(user_id: ^user.id)
      |> Query.where(role_id: ^invitation.role_id)
      |> Repo.one()
