zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

domain.ex (4439B)


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