Commit 4f451133 authored by Antonis Kalou's avatar Antonis Kalou

Remove unused do_create/do_update from ProcessSpecification

parent 4dc23f7f
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Claim.Claims do
import CommonsPub.Common, only: [maybe_put: 3]
alias CommonsPub.Repo
alias CommonsPub.Users.User
......@@ -8,8 +10,6 @@ defmodule ValueFlows.Claim.Claims do
alias CommonsPub.Meta.Pointers
import CommonsPub.Common, only: [maybe_put: 3]
def one(filters), do: Repo.single(Queries.query(Claim, filters))
def many(filters \\ []), do: {:ok, Repo.all(Queries.query(Claim, filters))}
......
......@@ -186,87 +186,31 @@ defmodule ValueFlows.Knowledge.ProcessSpecification.GraphQL do
})
end
def create_process_spec(
%{process_specification: %{in_scope_of: context_ids} = process_spec_attrs},
info
)
when is_list(context_ids) do
# FIXME: support multiple contexts?
context_id = List.first(context_ids)
create_process_spec(
%{process_specification: Map.merge(process_spec_attrs, %{in_scope_of: context_id})},
info
)
end
def create_process_spec(
%{process_specification: %{in_scope_of: context_id} = process_spec_attrs},
info
)
when not is_nil(context_id) do
# FIXME, need to do something like validate_thread_context to validate the provider/receiver agent ID
def create_process_spec(%{process_specification: process_spec_attrs}, info) do
Repo.transact_with(fn ->
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, pointer} <- Pointers.one(id: context_id),
context = Pointers.follow!(pointer),
{:ok, uploads} <- UploadResolver.upload(user, process_spec_attrs, info),
process_spec_attrs = Map.merge(process_spec_attrs, uploads),
process_spec_attrs = Map.merge(process_spec_attrs, %{is_public: true}),
{:ok, process_spec} <-
ProcessSpecifications.create(user, context, process_spec_attrs) do
{:ok, process_spec} <- ProcessSpecifications.create(user, process_spec_attrs) do
{:ok, %{process_specification: process_spec}}
end
end)
end
# FIXME: duplication!
def create_process_spec(%{process_specification: process_spec_attrs}, info) do
def update_process_spec(%{process_specification: %{id: id} = changes}, info) do
Repo.transact_with(fn ->
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, uploads} <- UploadResolver.upload(user, process_spec_attrs, info),
process_spec_attrs = Map.merge(process_spec_attrs, uploads),
process_spec_attrs = Map.merge(process_spec_attrs, %{is_public: true}),
{:ok, process_spec} <- ProcessSpecifications.create(user, process_spec_attrs) do
{:ok, process_spec} <- process_spec(%{id: id}, info),
:ok <- ensure_update_permission(user, process_spec),
{:ok, uploads} <- UploadResolver.upload(user, changes, info),
changes = Map.merge(changes, uploads),
{:ok, process_spec} <- ProcessSpecifications.update(process_spec, changes) do
{:ok, %{process_specification: process_spec}}
end
end)
end
def update_process_spec(%{process_specification: %{in_scope_of: context_ids} = changes}, info) do
context_id = List.first(context_ids)
Repo.transact_with(fn ->
do_update(changes, info, fn process_spec, changes ->
with {:ok, pointer} <- Pointers.one(id: context_id) do
context = Pointers.follow!(pointer)
ProcessSpecifications.update(process_spec, context, changes)
end
end)
end)
end
def update_process_spec(%{process_specification: changes}, info) do
Repo.transact_with(fn ->
do_update(changes, info, fn process_spec, changes ->
ProcessSpecifications.update(process_spec, changes)
end)
end)
end
defp do_update(%{id: id} = changes, info, update_fn) do
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, process_spec} <- process_spec(%{id: id}, info),
:ok <- ensure_update_permission(user, process_spec),
{:ok, uploads} <- UploadResolver.upload(user, changes, info),
changes = Map.merge(changes, uploads),
{:ok, process_spec} <- update_fn.(process_spec, changes) do
{:ok, %{process_specification: process_spec}}
end
end
def delete_process_spec(%{id: id}, info) do
Repo.transact_with(fn ->
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
......
......@@ -8,18 +8,14 @@ defmodule ValueFlows.Knowledge.ProcessSpecification do
alias Ecto.Changeset
alias CommonsPub.Users.User
#
# alias CommonsPub.Communities.Community
# alias ValueFlows.Knowledge.Action
alias ValueFlows.Knowledge.ProcessSpecification
# alias Measurement.Measure
@type t :: %__MODULE__{}
pointable_schema do
field(:name, :string)
field(:note, :string)
# belongs_to(:image, Content)
field(:classified_as, {:array, :string}, virtual: true)
......@@ -44,23 +40,7 @@ defmodule ValueFlows.Knowledge.ProcessSpecification do
end
@required ~w(name is_public)a
@cast @required ++ ~w(note classified_as is_disabled)a
def create_changeset(
%User{} = creator,
%{id: _} = context,
attrs
) do
%ProcessSpecification{}
|> 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 classified_as is_disabled context_id)a
def create_changeset(
%User{} = creator,
......@@ -76,17 +56,6 @@ defmodule ValueFlows.Knowledge.ProcessSpecification do
|> common_changeset()
end
def update_changeset(
%ProcessSpecification{} = process_spec,
%{id: _} = context,
attrs
) do
process_spec
|> Changeset.cast(attrs, @cast)
|> Changeset.change(context_id: context.id)
|> common_changeset()
end
def update_changeset(%ProcessSpecification{} = process_spec, attrs) do
process_spec
|> Changeset.cast(attrs, @cast)
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Knowledge.ProcessSpecification.ProcessSpecifications 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 Measurement.Measure
alias ValueFlows.Knowledge.ProcessSpecification
alias ValueFlows.Knowledge.ProcessSpecification.Queries
# alias ValueFlows.Knowledge.Action
# alias ValueFlows.Knowledge.Action.Actions
def cursor(), do: &[&1.id]
def test_cursor(), do: &[&1["id"]]
......@@ -86,26 +84,12 @@ defmodule ValueFlows.Knowledge.ProcessSpecification.ProcessSpecifications do
## mutations
# @spec create(User.t(), Community.t(), attrs :: map) :: {:ok, ProcessSpecification.t()} | {:error, Changeset.t()}
def create(%User{} = creator, %{id: _id} = context, attrs)
when is_map(attrs) do
do_create(creator, attrs, fn ->
ProcessSpecification.create_changeset(creator, context, attrs)
end)
end
# @spec create(User.t(), attrs :: map) :: {:ok, ProcessSpecification.t()} | {:error, Changeset.t()}
@spec create(User.t(), attrs :: map) :: {:ok, ProcessSpecification.t()} | {:error, Changeset.t()}
def create(%User{} = creator, attrs) when is_map(attrs) do
do_create(creator, attrs, fn ->
ProcessSpecification.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, item} <- Repo.insert(cs),
with {:ok, item} <- Repo.insert(ProcessSpecification.create_changeset(creator, attrs)),
{:ok, item} <- ValueFlows.Util.try_tag_thing(creator, item, attrs),
act_attrs = %{verb: "created", is_local: true},
# FIXME
......@@ -166,20 +150,10 @@ defmodule ValueFlows.Knowledge.ProcessSpecification.ProcessSpecifications do
# TODO: take the user who is performing the update
# @spec update(%ProcessSpecification{}, attrs :: map) :: {:ok, ProcessSpecification.t()} | {:error, Changeset.t()}
def update(%ProcessSpecification{} = process_spec, attrs) do
do_update(process_spec, attrs, &ProcessSpecification.update_changeset(&1, attrs))
end
def update(%ProcessSpecification{} = process_spec, %{id: _id} = context, attrs) do
do_update(process_spec, attrs, &ProcessSpecification.update_changeset(&1, context, attrs))
end
def do_update(process_spec, attrs, changeset_fn) do
Repo.transact_with(fn ->
cs =
process_spec
|> changeset_fn.()
attrs = prepare_attrs(attrs)
with {:ok, process_spec} <- Repo.update(cs),
with {:ok, process_spec} <- Repo.update(ProcessSpecification.update_changeset(process_spec, attrs)),
{:ok, process_spec} <- ValueFlows.Util.try_tag_thing(nil, process_spec, attrs),
:ok <- publish(process_spec, :updated) do
{:ok, process_spec}
......@@ -221,4 +195,11 @@ defmodule ValueFlows.Knowledge.ProcessSpecification.ProcessSpecifications do
:ok
end
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
end
end
......@@ -272,18 +272,11 @@ defmodule ValueFlows.Simulate do
extra ++ ~w(id name note created has_beginning has_end unit_based)a
end
def fake_process_specification!(user, context \\ nil, overrides \\ %{})
def fake_process_specification!(user, context, overrides) when is_nil(context) do
def fake_process_specification!(user, overrides \\ %{}) do
{:ok, spec} = ProcessSpecifications.create(user, process_specification(overrides))
spec
end
def fake_process_specification!(user, context, overrides) do
{:ok, spec} = ProcessSpecifications.create(user, context, process_specification(overrides))
spec
end
def fake_economic_event!(user, overrides \\ %{}) do
unit = fake_unit!(user)
fake_economic_event!(user, overrides, unit)
......
......@@ -40,11 +40,12 @@ defmodule ValueFlows.Knowledge.ProcessSpecification.ProcessSpecificationsTest do
test "can create a process specification with context" do
user = fake_user!()
parent = fake_user!()
assert {:ok, spec} = ProcessSpecifications.create(user, parent, process_specification())
attrs = %{in_scope_of: [fake_user!().id]}
assert {:ok, spec} = ProcessSpecifications.create(user, process_specification(attrs))
assert_process_specification(spec)
assert spec.context_id == parent.id
assert spec.context_id == hd(attrs.in_scope_of)
end
test "can create a process_specification with tags" do
......
......@@ -42,7 +42,9 @@ defmodule ValueFlows.Knowledge.ResourceSpecification.ResourceSpecificationsTest
user = fake_user!()
parent = fake_user!()
assert {:ok, spec} = ResourceSpecifications.create(user, parent, resource_specification())
attrs = %{in_scope_of: [parent.id]}
assert {:ok, spec} = ResourceSpecifications.create(user, resource_specification(attrs))
assert_resource_specification(spec)
assert spec.context_id == parent.id
end
......
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