zf

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

commit 3a252a89810b832c919102cff7644a48c1da6974
parent e826f016ddbc735036d64c97ad2aba19223dfe22
Author: srfsh <dev@srf.sh>
Date:   Tue, 13 Dec 2022 20:37:43 +0300

Zenflows.VF.Proposal.{Query,Domain}: add state/1 that indicates in which state the proposal is

Diffstat:
Msrc/zenflows/vf/proposal/domain.ex | 17+++++++++++++++++
Msrc/zenflows/vf/proposal/query.ex | 10++++++++++
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/src/zenflows/vf/proposal/domain.ex b/src/zenflows/vf/proposal/domain.ex @@ -52,6 +52,23 @@ def all!(page \\ Page.new()) do value end +@spec state(Proposal.t() | Schema.id()) + :: {:ok, :pending | :accepted | :refused} | {:error, String.t()} +def state(%Proposal{id: id}), do: state(id) +def state(id) do + Query.state(id) + |> Repo.one() + |> case do + nil -> {:error, "not found"} + {true, _, 0} -> + {:ok, :refused} + {_, intents, satisfactions} when intents != satisfactions -> + {:ok, :pending} + {_, intents, satisfactions} when intents == satisfactions -> + {:ok, :accepted} + end +end + @spec create(Schema.params()) :: {:ok, Proposal.t()} | {:error, Changeset.t()} def create(params) do key = multi_key() diff --git a/src/zenflows/vf/proposal/query.ex b/src/zenflows/vf/proposal/query.ex @@ -171,4 +171,14 @@ defp all_validate(params) do :or_primary_intents_resource_inventoried_as_id]) |> Changeset.apply_action(nil) end + +@spec state(Schema.id()) :: Queryable.t() +def state(id) do + from p in Proposal, + where: p.id == ^id, + join: pi in assoc(p, :publishes), + join: i in assoc(pi, :publishes), + left_join: s in assoc(i, :satisfied_by), + select: {fragment("every(?)", i.finished), count(i.id), count(s.id)} +end end