Commit 08ccdf4e authored by Antonis Kalou's avatar Antonis Kalou

Cleanup ValueFlows.Observation.Process

parent e45cb329
......@@ -235,7 +235,6 @@ defmodule ValueFlows.Observation.Process.GraphQL do
})
end
# FIXME: duplication!
def create_process(%{process: process_attrs}, info) do
Repo.transact_with(fn ->
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
......@@ -248,21 +247,13 @@ defmodule ValueFlows.Observation.Process.GraphQL do
end)
end
def update_process(%{process: changes}, info) do
Repo.transact_with(fn ->
do_update(changes, info, fn process, changes ->
Processes.update(process, changes)
end)
end)
end
defp do_update(%{id: id} = changes, info, update_fn) do
def update_process(%{process: %{id: id} = changes}, info) do
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, process} <- process(%{id: id}, info),
:ok <- ensure_update_permission(user, process),
{:ok, uploads} <- UploadResolver.upload(user, changes, info),
changes = Map.merge(changes, uploads),
{:ok, process} <- update_fn.(process, changes) do
{:ok, process} <- Processes.update(process, changes) do
{:ok, %{process: process}}
end
end
......
......@@ -77,24 +77,7 @@ defmodule ValueFlows.Observation.Process do
end
@required ~w(name is_public)a
@cast @required ++ ~w(note has_beginning has_end finished is_disabled)a
def create_changeset(
%User{} = creator,
%{id: _} = context,
attrs
) do
%Process{}
|> Changeset.cast(attrs, @cast)
|> Changeset.validate_required(@required)
|> Changeset.change(
creator_id: creator.id,
context_id: context.id,
is_public: true
)
|> common_changeset()
end
@cast @required ++ ~w(note has_beginning has_end finished is_disabled context_id based_on_id)a
def create_changeset(
%User{} = creator,
......@@ -122,7 +105,6 @@ defmodule ValueFlows.Observation.Process do
|> change_disabled()
end
def context_module, do: ValueFlows.Observation.Process.Processes
def queries_module, do: ValueFlows.Observation.Process.Queries
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Observation.Process.Processes do
import CommonsPub.Common, only: [maybe_put: 3]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
alias CommonsPub.Contexts
alias CommonsPub.Feeds.FeedActivities
alias CommonsPub.Users.User
# alias CommonsPub.Meta.Pointers
# alias Geolocation.Geolocations
# alias Measurement.Measure
alias ValueFlows.Observation.Process
alias ValueFlows.Observation.Process.Queries
alias ValueFlows.Observation.EconomicEvent.EconomicEvents
......@@ -108,35 +107,27 @@ defmodule ValueFlows.Observation.Process.Processes do
{:ok, nil}
end
## mutations
def create(%User{} = creator, %{id: _id} = context, attrs)
when is_map(attrs) do
do_create(creator, attrs, fn ->
Process.create_changeset(creator, context, attrs)
end)
def preload_all(%Process{} = process) do
# shouldn't fail
{:ok, process} = one(id: process.id, preload: :all)
process
end
## mutations
# @spec create(User.t(), attrs :: map) :: {:ok, Process.t()} | {:error, Changeset.t()}
def create(%User{} = creator, attrs) when is_map(attrs) do
do_create(creator, attrs, fn ->
Process.create_changeset(creator, attrs)
end)
end
def do_create(creator, attrs, changeset_fn) do
Repo.transact_with(fn ->
cs = changeset_fn.()
attrs = prepare_attrs(attrs)
with {:ok, process} <- Repo.insert(cs),
with {:ok, process} <- Repo.insert(Process.create_changeset(creator, attrs)),
{:ok, process} <- ValueFlows.Util.try_tag_thing(creator, process, attrs),
act_attrs = %{verb: "created", is_local: true},
# FIXME
{:ok, activity} <- Activities.create(creator, process, act_attrs),
:ok <- publish(creator, process, activity, :created) do
process = %{process | creator: creator}
index(process)
{:ok, process}
{:ok, preload_all(process)}
end
end)
end
......@@ -189,24 +180,13 @@ defmodule ValueFlows.Observation.Process.Processes do
# TODO: take the user who is performing the update
# @spec update(%Process{}, attrs :: map) :: {:ok, Process.t()} | {:error, Changeset.t()}
def update(%Process{} = process, attrs) do
do_update(process, attrs, &Process.update_changeset(&1, attrs))
end
def do_update(process, attrs, changeset_fn) do
Repo.transact_with(fn ->
process =
Repo.preload(process, [
:based_on
])
cs =
process
|> changeset_fn.()
attrs = prepare_attrs(attrs)
with {:ok, process} <- Repo.update(cs),
with {:ok, process} <- Repo.update(Process.update_changeset(process, attrs)),
{:ok, process} <- ValueFlows.Util.try_tag_thing(nil, process, attrs),
:ok <- publish(process, :updated) do
{:ok, process}
{:ok, preload_all(process)}
end
end)
end
......@@ -242,4 +222,12 @@ defmodule ValueFlows.Observation.Process.Processes do
:ok
end
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:based_on_id, Map.get(attrs, :based_on))
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
end
end
......@@ -241,20 +241,12 @@ defmodule ValueFlows.Observation.Process.Queries do
select(q, [process: c], {field(c, ^key), count(c.id)})
end
def filter(q, {:preload, :provider}) do
preload(q, [pointer: p], provider: p)
end
def filter(q, {:preload, :receiver}) do
preload(q, [pointer: p], receiver: p)
end
def filter(q, {:preload, :at_location}) do
q
|> join_to(:geolocation)
|> preload(:at_location)
# preload(q, [geolocation: g], at_location: g)
def filter(q, {:preload, :all}) do
preload(q, [
:based_on,
:creator,
:context
])
end
# pagination
......
......@@ -132,17 +132,18 @@ defmodule ValueFlows.Observation.Process.ProcessesTest do
test "can create a process" do
user = fake_user!()
assert {:ok, spec} = Processes.create(user, process())
assert_process(spec)
assert {:ok, process} = Processes.create(user, process())
assert_process(process)
end
test "can create a process with context" do
user = fake_user!()
parent = fake_user!()
assert {:ok, spec} = Processes.create(user, parent, process())
assert_process(spec)
assert spec.context_id == parent.id
attrs = %{in_scope_of: [parent.id]}
assert {:ok, process} = Processes.create(user, process(attrs))
assert_process(process)
assert process.context.id == parent.id
end
test "can create a process with tags" do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment