Introduce registerPerson mutation
To register a profile from an unactivated user Signed-off-by: Thomas Citharel <tcit@tcit.fr> 👤 Fix Person interface use Signed-off-by: Thomas Citharel <tcit@tcit.fr> Change host function for data property Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -3,7 +3,7 @@ defmodule MobilizonWeb.Resolvers.Person do
|
||||
Handles the person-related GraphQL calls
|
||||
"""
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Actors.{Actor, User}
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
|
||||
@deprecated "Use find_person/3 or find_group/3 instead"
|
||||
@@ -52,6 +52,9 @@ defmodule MobilizonWeb.Resolvers.Person do
|
||||
{:error, "You need to be logged-in to view your list of identities"}
|
||||
end
|
||||
|
||||
@doc """
|
||||
This function is used to create more identities from an existing user
|
||||
"""
|
||||
def create_person(_parent, %{preferred_username: _preferred_username} = args, %{
|
||||
context: %{current_user: user}
|
||||
}) do
|
||||
@@ -59,9 +62,23 @@ defmodule MobilizonWeb.Resolvers.Person do
|
||||
|
||||
with {:ok, %Actor{} = new_person} <- Actors.new_person(args) do
|
||||
{:ok, new_person}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
This function is used to register a person afterwards the user has been created (but not activated)
|
||||
"""
|
||||
def register_person(_parent, args, _resolution) do
|
||||
with {:ok, %User{} = user} <- Actors.get_user_by_email(args.email),
|
||||
{:no_actor, nil} <- {:no_actor, Actors.get_actor_for_user(user)},
|
||||
args <- Map.put(args, :user_id, user.id),
|
||||
{:ok, %Actor{} = new_person} <- Actors.new_person(args) do
|
||||
{:ok, new_person}
|
||||
else
|
||||
{:error, %Ecto.Changeset{} = _e} ->
|
||||
{:error, "Unable to create a profile with this username"}
|
||||
{:error, :user_not_found} ->
|
||||
{:error, "User with email not found"}
|
||||
{:no_actor, _} ->
|
||||
{:error, "You already have a profile for this user"}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,6 +6,7 @@ defmodule MobilizonWeb.Schema.Actors.PersonType do
|
||||
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
|
||||
alias Mobilizon.Events
|
||||
alias MobilizonWeb.Resolvers
|
||||
import MobilizonWeb.Schema.Utils
|
||||
|
||||
@desc """
|
||||
Represents a person identity
|
||||
@@ -69,11 +70,24 @@ defmodule MobilizonWeb.Schema.Actors.PersonType do
|
||||
@desc "Create a new person for user"
|
||||
field :create_person, :person do
|
||||
arg(:preferred_username, non_null(:string))
|
||||
arg(:name, :string, description: "The displayed name for the new profile")
|
||||
|
||||
arg(:description, :string, description: "The summary for the new profile", default_value: "")
|
||||
arg(:name, :string, description: "The displayed name for the new profile", default_value: "")
|
||||
|
||||
resolve(&Resolvers.Person.create_person/3)
|
||||
arg(:summary, :string, description: "The summary for the new profile", default_value: "")
|
||||
|
||||
resolve(handle_errors(&Resolvers.Person.create_person/3))
|
||||
end
|
||||
|
||||
@desc "Register a first profile on registration"
|
||||
field :register_person, :person do
|
||||
arg(:preferred_username, non_null(:string))
|
||||
|
||||
arg(:name, :string, description: "The displayed name for the new profile", default_value: "")
|
||||
|
||||
arg(:summary, :string, description: "The summary for the new profile", default_value: "")
|
||||
arg(:email, non_null(:string), description: "The email from the user previously created")
|
||||
|
||||
resolve(handle_errors(&Resolvers.Person.register_person/3))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,8 +12,8 @@ defmodule MobilizonWeb.Schema.Utils do
|
||||
# {:error, [email: {"has already been taken", []}]}
|
||||
errors =
|
||||
changeset.errors
|
||||
|> Enum.map(fn {_key, {value, context}} ->
|
||||
[message: "#{value}", details: context]
|
||||
|> Enum.map(fn {key, {value, _context}} ->
|
||||
[message: "#{value}", details: key]
|
||||
end)
|
||||
|
||||
{:error, errors}
|
||||
|
||||
Reference in New Issue
Block a user