person.ex (3346B)
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 do 19 @moduledoc "A natural person." 20 21 use Zenflows.DB.Schema 22 23 alias Ecto.Changeset 24 alias Zenflows.DB.{Schema, Validate} 25 alias Zenflows.File 26 alias Zenflows.VF.SpatialThing 27 28 @type t() :: %__MODULE__{ 29 type: :per, 30 name: String.t(), 31 images: [File.t()], 32 note: String.t() | nil, 33 primary_location: SpatialThing.t() | nil, 34 user: String.t(), 35 email: String.t(), 36 ecdh_public_key: String.t() | nil, 37 eddsa_public_key: String.t() | nil, 38 ethereum_address: String.t() | nil, 39 reflow_public_key: String.t() | nil, 40 bitcoin_public_key: String.t() | nil, 41 } 42 43 schema "vf_agent" do 44 field :type, Ecto.Enum, values: [:per], default: :per 45 field :name, :string 46 has_many :images, File, foreign_key: :agent_id 47 field :note, :string 48 belongs_to :primary_location, SpatialThing 49 field :user, :string 50 field :email, :string 51 field :ecdh_public_key, :string 52 field :eddsa_public_key, :string 53 field :ethereum_address, :string 54 field :reflow_public_key, :string 55 field :bitcoin_public_key, :string 56 timestamps() 57 end 58 59 @insert_reqr ~w[name user email]a 60 @insert_cast @insert_reqr ++ ~w[ 61 note primary_location_id 62 ecdh_public_key 63 eddsa_public_key 64 ethereum_address 65 reflow_public_key 66 bitcoin_public_key 67 ]a 68 # TODO: Maybe add email to @update_cast as well? 69 # TODO: Maybe add the pubkeys to @update_cast as well? 70 @update_cast ~w[name note primary_location_id user]a 71 72 # insert changeset 73 @doc false 74 @spec changeset(Schema.params()) :: Changeset.t() 75 def changeset(params) do 76 %__MODULE__{} 77 |> Changeset.cast(params, @insert_cast) 78 |> Changeset.validate_required(@insert_reqr) 79 |> Validate.name(:name) 80 |> Validate.name(:user) 81 |> Validate.name(:email) 82 |> Changeset.cast_assoc(:images) 83 |> Validate.note(:note) 84 |> Validate.key(:ecdh_public_key) 85 |> Validate.key(:eddsa_public_key) 86 |> Validate.key(:ethereum_address) 87 |> Validate.key(:reflow_public_key) 88 |> Validate.key(:bitcoin_public_key) 89 |> Validate.email(:email) 90 |> Changeset.unique_constraint(:user) 91 |> Changeset.unique_constraint(:name) 92 |> Changeset.unique_constraint(:email) 93 |> Changeset.assoc_constraint(:primary_location) 94 end 95 96 # update changeset 97 @doc false 98 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 99 def changeset(schema, params) do 100 schema 101 |> Changeset.cast(params, @update_cast) 102 |> Validate.name(:name) 103 |> Validate.name(:user) 104 |> Validate.note(:note) 105 |> Validate.email(:email) 106 |> Changeset.unique_constraint(:user) 107 |> Changeset.unique_constraint(:name) 108 |> Changeset.assoc_constraint(:primary_location) 109 end 110 end