commit cf86879ef1049a59db52587e85871e7e377dc573
parent 9035af30d5abd8de2300c8c1d06d0ce2ff76150a
Author: srfsh <dev@srf.sh>
Date: Tue, 15 Nov 2022 01:03:38 +0300
Zenflows.VF.Process.{Domain,Type,Resolv}: introducte previous call
This is one step forward the implementation of trace.
Diffstat:
4 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/src/zenflows/vf/process/domain.ex b/src/zenflows/vf/process/domain.ex
@@ -20,7 +20,11 @@ defmodule Zenflows.VF.Process.Domain do
alias Ecto.{Changeset, Multi}
alias Zenflows.DB.{Page, Repo, Schema}
-alias Zenflows.VF.Process
+alias Zenflows.VF.{
+ EconomicEvent,
+ Process,
+ Process.Query,
+}
@spec one(Ecto.Repo.t(), Schema.id() | map() | Keyword.t())
:: {:ok, Process.t()} | {:error, String.t()}
@@ -50,6 +54,18 @@ def all!(page \\ Page.new()) do
value
end
+@spec previous(Process.t() | Schema.id()) :: [EconomicEvent.t()]
+def previous(_, _ \\ Page.new())
+def previous(%Process{id: id}, page), do: previous(id, page)
+def previous(id, page) do
+ Query.previous(id)
+ |> Page.all(page)
+ |> Enum.sort(&(
+ &1.previous_event_id == nil
+ or &1.id == &2.previous_event_id
+ or &1.id <= &2.id))
+end
+
@spec create(Schema.params()) :: {:ok, Process.t()} | {:error, Changeset.t()}
def create(params) do
key = multi_key()
diff --git a/src/zenflows/vf/process/query.ex b/src/zenflows/vf/process/query.ex
@@ -0,0 +1,31 @@
+# Zenflows is designed to implement the Valueflows vocabulary,
+# written and maintained by srfsh <info@dyne.org>.
+# Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+defmodule Zenflows.VF.Process.Query do
+@moduledoc false
+
+import Ecto.Query
+
+alias Ecto.Queryable
+alias Zenflows.DB.Schema
+alias Zenflows.VF.EconomicEvent
+
+@spec previous(Schema.id()) :: Queryable.t()
+def previous(id) do
+ where(EconomicEvent, input_of_id: ^id)
+end
+end
diff --git a/src/zenflows/vf/process/resolv.ex b/src/zenflows/vf/process/resolv.ex
@@ -66,4 +66,8 @@ def nested_in(proc, _, _) do
proc = Domain.preload(proc, :nested_in)
{:ok, proc.nested_in}
end
+
+def previous(proc, _, _) do
+ {:ok, Domain.previous(proc)}
+end
end
diff --git a/src/zenflows/vf/process/type.ex b/src/zenflows/vf/process/type.ex
@@ -89,6 +89,9 @@ object :process do
@desc @nested_in
field :nested_in, :scenario, resolve: &Resolv.nested_in/3
+
+ field :previous, list_of(non_null(:economic_event)),
+ resolve: &Resolv.previous/3
end
input_object :process_create_params do