domain.ex (4592B)
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.Domain do 19 @moduledoc "Domain logic of Proposals." 20 21 alias Ecto.{Changeset, Multi} 22 alias Zenflows.DB.{Page, Repo, Schema} 23 alias Zenflows.VF.{Proposal, Proposal.Query} 24 25 @spec one(Ecto.Repo.t(), Schema.id() | map() | Keyword.t()) 26 :: {:ok, Proposal.t()} | {:error, String.t()} 27 def one(repo \\ Repo, _) 28 def one(repo, id) when is_binary(id), do: one(repo, id: id) 29 def one(repo, clauses) do 30 case repo.get_by(Proposal, clauses) do 31 nil -> {:error, "not found"} 32 found -> {:ok, found} 33 end 34 end 35 36 @spec one!(Ecto.Repo.t(), Schema.id() | map() | Keyword.t()) :: Proposal.t() 37 def one!(repo \\ Repo, id_or_clauses) do 38 {:ok, value} = one(repo, id_or_clauses) 39 value 40 end 41 42 @spec all(Page.t()) :: {:ok, [Proposal.t()]} | {:error, Changeset.t()} 43 def all(page \\ Page.new()) do 44 with {:ok, q} <- Query.all(page) do 45 {:ok, Page.all(q, page)} 46 end 47 end 48 49 @spec all!(Page.t()) :: [Proposal.t()] 50 def all!(page \\ Page.new()) do 51 {:ok, value} = all(page) 52 value 53 end 54 55 @spec status(Proposal.t() | Schema.id()) 56 :: {:ok, :pending | :accepted | :refused} | {:error, String.t()} 57 def status(%Proposal{id: id}), do: status(id) 58 def status(id) do 59 Query.status(id) 60 |> Repo.one() 61 |> case do 62 nil -> {:error, "not found"} 63 {true, _, 0} -> 64 {:ok, :refused} 65 {_, intents, satisfactions} when intents == satisfactions -> 66 {:ok, :accepted} 67 _ -> 68 {:ok, :pending} 69 end 70 end 71 72 @spec create(Schema.params()) :: {:ok, Proposal.t()} | {:error, Changeset.t()} 73 def create(params) do 74 key = multi_key() 75 Multi.new() 76 |> multi_insert(params) 77 |> Repo.transaction() 78 |> case do 79 {:ok, %{^key => value}} -> {:ok, value} 80 {:error, _, reason, _} -> {:error, reason} 81 end 82 end 83 84 @spec create!(Schema.params()) :: Proposal.t() 85 def create!(params) do 86 {:ok, value} = create(params) 87 value 88 end 89 90 @spec update(Schema.id(), Schema.params()) 91 :: {:ok, Proposal.t()} | {:error, String.t() | Changeset.t()} 92 def update(id, params) do 93 key = multi_key() 94 Multi.new() 95 |> multi_update(id, params) 96 |> Repo.transaction() 97 |> case do 98 {:ok, %{^key => value}} -> {:ok, value} 99 {:error, _, reason, _} -> {:error, reason} 100 end 101 end 102 103 @spec update!(Schema.id(), Schema.params()) :: Proposal.t() 104 def update!(id, params) do 105 {:ok, value} = update(id, params) 106 value 107 end 108 109 @spec delete(Schema.id()) 110 :: {:ok, Proposal.t()} | {:error, String.t() | Changeset.t()} 111 def delete(id) do 112 key = multi_key() 113 Multi.new() 114 |> multi_delete(id) 115 |> Repo.transaction() 116 |> case do 117 {:ok, %{^key => value}} -> {:ok, value} 118 {:error, _, reason, _} -> {:error, reason} 119 end 120 end 121 122 @spec delete!(Schema.id()) :: Proposal.t() 123 def delete!(id) do 124 {:ok, value} = delete(id) 125 value 126 end 127 128 @spec preload(Proposal.t(), :eligible_location | :publishes 129 | :primary_intents | :reciprocal_intents) 130 :: Proposal.t() 131 def preload(prop, x) when x in ~w[ 132 eligible_location publishes primary_intents reciprocal_intents 133 ]a do 134 Repo.preload(prop, x) 135 end 136 137 @spec multi_key() :: atom() 138 def multi_key(), do: :proposal 139 140 @spec multi_one(Multi.t(), term(), Schema.id()) :: Multi.t() 141 def multi_one(m, key \\ multi_key(), id) do 142 Multi.run(m, key, fn repo, _ -> one(repo, id) end) 143 end 144 145 @spec multi_insert(Multi.t(), term(), Schema.params()) :: Multi.t() 146 def multi_insert(m, key \\ multi_key(), params) do 147 Multi.insert(m, key, Proposal.changeset(params)) 148 end 149 150 @spec multi_update(Multi.t(), term(), Schema.id(), Schema.params()) :: Multi.t() 151 def multi_update(m, key \\ multi_key(), id, params) do 152 m 153 |> multi_one("#{key}.one", id) 154 |> Multi.update(key, 155 &Proposal.changeset(Map.fetch!(&1, "#{key}.one"), params)) 156 end 157 158 @spec multi_delete(Multi.t(), term(), Schema.id()) :: Multi.t() 159 def multi_delete(m, key \\ multi_key(), id) do 160 m 161 |> multi_one("#{key}.one", id) 162 |> Multi.delete(key, &Map.fetch!(&1, "#{key}.one")) 163 end 164 end