recipe_flow.ex (2959B)
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.RecipeFlow do 19 @moduledoc """ 20 The specification of a resource inflow to, or outflow from, a recipe process. 21 """ 22 23 use Zenflows.DB.Schema 24 25 alias Ecto.Changeset 26 alias Zenflows.DB.{Schema, Validate} 27 alias Zenflows.VF.{ 28 Action, 29 Measure, 30 RecipeExchange, 31 RecipeProcess, 32 RecipeResource, 33 Unit, 34 } 35 36 @type t() :: %__MODULE__{ 37 action: Action.t(), 38 recipe_input_of: RecipeProcess.t() | nil, 39 recipe_output_of: RecipeProcess.t() | nil, 40 recipe_flow_resource: RecipeResource.t() | nil, 41 resource_quantity: Measure.t() | nil, 42 effort_quantity: Measure.t() | nil, 43 recipe_clause_of: RecipeExchange.t() | nil, 44 note: String.t() | nil, 45 } 46 47 schema "vf_recipe_flow" do 48 field :note, :string 49 field :action_id, Action.ID 50 field :action, :map, virtual: true 51 belongs_to :recipe_input_of, RecipeProcess 52 belongs_to :recipe_output_of, RecipeProcess 53 belongs_to :recipe_flow_resource, RecipeResource 54 field :resource_quantity, :map, virtual: true 55 belongs_to :resource_quantity_has_unit, Unit 56 field :resource_quantity_has_numerical_value, :decimal 57 field :effort_quantity, :map, virtual: true 58 belongs_to :effort_quantity_has_unit, Unit 59 field :effort_quantity_has_numerical_value, :decimal 60 belongs_to :recipe_clause_of, RecipeExchange 61 timestamps() 62 end 63 64 @reqr ~w[action_id recipe_flow_resource_id]a 65 @cast @reqr ++ ~w[ 66 recipe_input_of_id recipe_output_of_id 67 resource_quantity effort_quantity 68 recipe_clause_of_id note 69 ]a 70 71 @doc false 72 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 73 def changeset(schema \\ %__MODULE__{}, params) do 74 schema 75 |> Changeset.cast(params, @cast) 76 |> Changeset.validate_required(@reqr) 77 |> Validate.note(:note) 78 |> Measure.cast(:effort_quantity) 79 |> Measure.cast(:resource_quantity) 80 |> Validate.exist_or([ 81 :resource_quantity_has_unit_id, :effort_quantity_has_unit_id, 82 ], method: :both) 83 |> Validate.exist_or([ 84 :resource_quantity_has_numerical_value, :effort_quantity_has_numerical_value 85 ], method: :both) 86 |> Changeset.assoc_constraint(:recipe_input_of) 87 |> Changeset.assoc_constraint(:recipe_output_of) 88 |> Changeset.assoc_constraint(:recipe_flow_resource) 89 end 90 end