resolv.ex (2432B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>. 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 defmodule Zenflows.VF.Person.Resolv do 19 @moduledoc false 20 21 alias Ecto.Changeset 22 alias Zenflows.DB.Validate 23 alias Zenflows.GQL.Connection 24 alias Zenflows.VF.Person.Domain 25 26 def person(params, _) do 27 Domain.one(params) 28 end 29 30 def people(params, _) do 31 with {:ok, page} <- Connection.parse(params), 32 {:ok, schemas} <- Domain.all(page) do 33 {:ok, Connection.from_list(schemas, page)} 34 end 35 end 36 37 def person_exists(params, _) do 38 with {:ok, parsed} <- parse_person_exists(params) do 39 {:ok, parsed |> Map.to_list() |> Domain.exists?()} 40 end 41 end 42 43 def person_check(params, _) do 44 Domain.one(params) 45 end 46 47 def person_pubkey(%{id: id}, _) do 48 Domain.pubkey(id) 49 end 50 51 def create_person(%{person: params}, _) do 52 with {:ok, per} <- Domain.create(params) do 53 {:ok, %{agent: per}} 54 end 55 end 56 57 def update_person(%{person: %{id: id} = params}, _) do 58 with {:ok, per} <- Domain.update(id, params) do 59 {:ok, %{agent: per}} 60 end 61 end 62 63 def delete_person(%{id: id}, _) do 64 with {:ok, _} <- Domain.delete(id) do 65 {:ok, true} 66 end 67 end 68 69 def claim_person(%{id: id}, _) do 70 Domain.claim(id) 71 end 72 73 def images(per, _, _) do 74 per = Domain.preload(per, :images) 75 {:ok, per.images} 76 end 77 78 def primary_location(per, _, _) do 79 per = Domain.preload(per, :primary_location) 80 {:ok, per.primary_location} 81 end 82 83 @spec parse_person_exists(map()) :: {:ok, map()} | {:error, Changeset.t()} 84 def parse_person_exists(params) do 85 {%{}, %{email: :string, user: :string}} 86 |> Changeset.cast(params, [:email, :user]) 87 |> Validate.exist_xor([:email, :user]) 88 |> Validate.email(:email) 89 |> Validate.name(:user) 90 |> Changeset.apply_action(nil) 91 end 92 end