proposal.ex (2415B)
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.Proposal do 19 @moduledoc """ 20 Published requests or offers, sometimes with what is expected in return. 21 """ 22 23 use Zenflows.DB.Schema 24 25 alias Ecto.Changeset 26 alias Zenflows.DB.{Schema, Validate} 27 alias Zenflows.VF.{ 28 Intent, 29 ProposedIntent, 30 SpatialThing, 31 } 32 33 @type t() :: %__MODULE__{ 34 name: String.t(), 35 has_beginning: DateTime.t() | nil, 36 has_end: DateTime.t() | nil, 37 unit_based: boolean(), 38 note: String.t() | nil, 39 eligible_location: SpatialThing.t() | nil, 40 publishes: [ProposedIntent.t()], 41 primary_intents: [Intent.t()], 42 reciprocal_intents: [Intent.t()], 43 } 44 45 schema "vf_proposal" do 46 field :name, :string 47 field :has_beginning, :utc_datetime_usec 48 field :has_end, :utc_datetime_usec 49 field :unit_based, :boolean, default: false 50 field :note, :string 51 belongs_to :eligible_location, SpatialThing 52 timestamps() 53 54 has_many :publishes, ProposedIntent, foreign_key: :published_in_id 55 many_to_many :primary_intents, Intent, join_through: ProposedIntent, 56 join_keys: [published_in_id: :id, publishes_id: :id], join_where: [reciprocal: false] 57 many_to_many :reciprocal_intents, Intent, join_through: ProposedIntent, 58 join_keys: [published_in_id: :id, publishes_id: :id], join_where: [reciprocal: true] 59 end 60 61 @cast ~w[name has_beginning has_end unit_based note eligible_location_id]a 62 63 @doc false 64 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 65 def changeset(schema \\ %__MODULE__{}, params) do 66 schema 67 |> Changeset.cast(params, @cast) 68 |> Validate.name(:name) 69 |> Validate.note(:note) 70 |> Changeset.assoc_constraint(:eligible_location) 71 end 72 end