satisfaction.ex (2602B)
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.Satisfaction do 19 @moduledoc """ 20 Represents many-to-many relationships between intents and commitments 21 or events that partially or full satisfy one or more intents. 22 """ 23 24 use Zenflows.DB.Schema 25 26 alias Ecto.Changeset 27 alias Zenflows.DB.{Schema, Validate} 28 alias Zenflows.VF.{ 29 Commitment, 30 EconomicEvent, 31 Intent, 32 Measure, 33 Unit, 34 } 35 36 @type t() :: %__MODULE__{ 37 satisfied_by_event: nil | EconomicEvent.t(), 38 satisfied_by_commitment: nil | Commitment.t(), 39 satisfies: Intent.t(), 40 resource_quantity: nil | Measure.t(), 41 effort_quantity: nil | Measure.t(), 42 note: nil | String.t(), 43 } 44 45 schema "vf_satisfaction" do 46 belongs_to :satisfied_by_event, EconomicEvent 47 belongs_to :satisfied_by_commitment, Commitment 48 belongs_to :satisfies, Intent 49 field :resource_quantity, :map, virtual: true 50 belongs_to :resource_quantity_has_unit, Unit 51 field :resource_quantity_has_numerical_value, :decimal 52 field :effort_quantity, :map, virtual: true 53 belongs_to :effort_quantity_has_unit, Unit 54 field :effort_quantity_has_numerical_value, :decimal 55 field :note, :string 56 timestamps() 57 end 58 59 @reqr [:satisfies_id] 60 @cast @reqr ++ ~w[ 61 satisfied_by_event_id satisfied_by_commitment_id 62 resource_quantity effort_quantity note 63 ]a 64 65 @doc false 66 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 67 def changeset(schema \\ %__MODULE__{}, params) do 68 schema 69 |> Changeset.cast(params, @cast) 70 |> Changeset.validate_required(@reqr) 71 |> Validate.note(:note) 72 |> Measure.cast(:resource_quantity) 73 |> Measure.cast(:effort_quantity) 74 |> Validate.exist_xor(~w[satisfied_by_event_id satisfied_by_commitment_id]a) 75 |> Changeset.assoc_constraint(:satisfied_by_event) 76 |> Changeset.assoc_constraint(:satisfied_by_commitment) 77 |> Changeset.assoc_constraint(:satisfies) 78 end 79 end