Commit e4804c3a authored by Antonis Kalou's avatar Antonis Kalou

Cleanup ValueFlows.Observation.Resource

parent 08ccdf4e
......@@ -292,6 +292,19 @@ defmodule ValueFlows.Observation.EconomicResource.Queries do
select(q, [resource: c], {field(c, ^key), count(c.id)})
end
def filter(q, {:preload, :all}) do
preload(q, [
:accounting_quantity,
:onhand_quantity,
:unit_of_effort,
:primary_accountable,
:current_location,
:contained_in,
:conforms_to,
:image
])
end
def filter(q, {:preload, :primary_accountable}) do
preload(q, :primary_accountable)
end
......
......@@ -70,7 +70,8 @@ defmodule ValueFlows.Observation.EconomicResource do
end
@required ~w(name is_public)a
@cast @required ++ ~w(note tracking_identifier current_location_id is_disabled image_id)a
@cast @required ++ ~w(note tracking_identifier current_location_id is_disabled image_id)a ++
~w(primary_accountable_id contained_in_id unit_of_effort_id conforms_to_id current_location_id)a
def create_changeset(
%User{} = creator,
......@@ -83,61 +84,13 @@ defmodule ValueFlows.Observation.EconomicResource do
creator_id: creator.id,
is_public: true
)
|> common_changeset()
|> common_changeset(attrs)
end
def update_changeset(%EconomicResource{} = resource, attrs) do
resource
|> Changeset.cast(attrs, @cast)
|> common_changeset()
end
def change_primary_accountable(changeset, %{id: _} = primary_accountable) do
Changeset.change(changeset,
primary_accountable: primary_accountable,
primary_accountable_id: primary_accountable.id
)
end
def change_state_action(changeset, %Action{} = state) do
Changeset.change(changeset,
state_id: state.id
)
end
def change_stage_process_spec(changeset, %ProcessSpecification{} = stage) do
Changeset.change(changeset,
stage: stage,
stage: stage.id
)
end
def change_current_location(changeset, %Geolocation{} = location) do
Changeset.change(changeset,
current_location: location,
current_location_id: location.id
)
end
def change_conforms_to_resource_spec(changeset, %ResourceSpecification{} = conforms_to) do
Changeset.change(changeset,
conforms_to: conforms_to,
conforms_to_id: conforms_to.id
)
end
def change_contained_in_resource(changeset, %EconomicResource{} = contained_in) do
Changeset.change(changeset,
contained_in: contained_in,
contained_in_id: contained_in.id
)
end
def change_unit_of_effort(changeset, %Unit{} = unit_of_effort) do
Changeset.change(changeset,
unit_of_effort: unit_of_effort,
unit_of_effort_id: unit_of_effort.id
)
|> common_changeset(attrs)
end
def change_measures(changeset, %{} = attrs) do
......@@ -152,8 +105,9 @@ defmodule ValueFlows.Observation.EconomicResource do
[:onhand_quantity, :accounting_quantity]
end
defp common_changeset(changeset) do
defp common_changeset(changeset, attrs) do
changeset
|> change_measures(attrs)
|> change_public()
|> change_disabled()
end
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
alias __MODULE__
import CommonsPub.Common, only: [maybe_put: 3]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
alias CommonsPub.Contexts
......@@ -9,15 +10,12 @@ defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
alias CommonsPub.Meta.Pointers
alias Geolocation.Geolocations
# alias Measurement.Measure
alias Measurement.Unit.Units
alias ValueFlows.Knowledge.ResourceSpecification.ResourceSpecifications
alias ValueFlows.Knowledge.ProcessSpecification.ProcessSpecifications
alias ValueFlows.Observation.EconomicResource
alias ValueFlows.Observation.EconomicResource.Queries
alias ValueFlows.Observation.EconomicEvent.EconomicEvents
# alias ValueFlows.Observation.Process.Processes
# alias ValueFlows.Knowledge.Action
alias ValueFlows.Knowledge.Action.Actions
def cursor(), do: &[&1.id]
......@@ -108,18 +106,8 @@ defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
end
def preload_all(resource) do
Repo.preload(resource, [
:accounting_quantity,
:onhand_quantity,
:unit_of_effort,
# :state,
:primary_accountable,
:current_location,
:contained_in,
:conforms_to,
:image
])
|> preload_state()
{:ok, resource} = one(id: resource.id, preload: :all)
preload_state(resource)
end
def preload_state(resource) do
......@@ -130,15 +118,10 @@ defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
# @spec create(User.t(), attrs :: map) :: {:ok, EconomicResource.t()} | {:error, Changeset.t()}
def create(%User{} = creator, attrs) when is_map(attrs) do
do_create(creator, attrs, fn ->
EconomicResource.create_changeset(creator, attrs)
end)
end
def do_create(creator, attrs, changeset_fn) do
Repo.transact_with(fn ->
with {:ok, cs} <- prepare_changeset(attrs, changeset_fn),
{:ok, resource} <- Repo.insert(cs),
attrs = prepare_attrs(attrs)
with {:ok, resource} <- Repo.insert(EconomicResource.create_changeset(creator, attrs)),
{:ok, resource} <- ValueFlows.Util.try_tag_thing(creator, resource, attrs),
act_attrs = %{verb: "created", is_local: true},
# FIXME
......@@ -156,104 +139,13 @@ defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
# TODO: take the user who is performing the update
# @spec update(%EconomicResource{}, attrs :: map) :: {:ok, EconomicResource.t()} | {:error, Changeset.t()}
def update(%EconomicResource{} = resource, attrs) do
do_update(resource, attrs, &EconomicResource.update_changeset(&1, attrs))
end
defp do_update(resource, attrs, changeset_fn) do
Repo.transact_with(fn ->
resource = preload_all(resource)
attrs = prepare_attrs(attrs)
with {:ok, cs} <- prepare_changeset(attrs, changeset_fn, resource),
{:ok, resource} <- Repo.update(cs),
with {:ok, resource} <- Repo.update(EconomicResource.update_changeset(resource, attrs)),
{:ok, resource} <- ValueFlows.Util.try_tag_thing(nil, resource, attrs),
:ok <- publish(resource, :updated) do
{:ok, resource}
end
end)
end
defp prepare_changeset(attrs, changeset_fn, resource) do
resource
|> changeset_fn.()
|> changeset_relations(attrs)
end
defp prepare_changeset(attrs, changeset_fn) do
changeset_fn.()
|> changeset_relations(attrs)
end
defp changeset_relations(cs, attrs) do
attrs = parse_measurement_attrs(attrs)
ValueFlows.Util.handle_changeset_errors(cs, attrs, [
&EconomicResource.change_measures/2,
&change_primary_accountable/2,
&change_state_action/2,
&change_current_location/2,
&change_conforms_to_resource_spec/2,
&change_contained_in_resource/2,
&change_unit_of_effort/2
])
end
defp change_primary_accountable(changeset, %{primary_accountable: id}) do
with {:ok, pointer} <- Pointers.one(id: id) do
# primary_accountable = Pointers.follow!(pointer)
EconomicResource.change_primary_accountable(changeset, pointer)
end
end
defp change_primary_accountable(changeset, _attrs), do: changeset
defp change_state_action(changeset, %{state: state_id}) do
with {:ok, state} <- Actions.action(state_id) do
EconomicResource.change_state_action(changeset, state)
end
end
defp change_state_action(changeset, _attrs), do: changeset
defp change_current_location(changeset, %{current_location: id}) do
with {:ok, location} <- Geolocations.one([:default, id: id]) do
EconomicResource.change_current_location(changeset, location)
end
end
defp change_current_location(changeset, _attrs), do: changeset
defp change_conforms_to_resource_spec(changeset, %{conforms_to: id}) do
with {:ok, resource} <- ResourceSpecifications.one([:default, id: id]) do
EconomicResource.change_conforms_to_resource_spec(changeset, resource)
end
end
defp change_conforms_to_resource_spec(changeset, _attrs), do: changeset
defp change_contained_in_resource(changeset, %{contained_in: id}) do
with {:ok, resource} <- EconomicResources.one([:default, id: id]) do
EconomicResource.change_contained_in_resource(changeset, resource)
end
end
defp change_contained_in_resource(changeset, _attrs), do: changeset
defp change_unit_of_effort(changeset, %{unit_of_effort: id}) do
with {:ok, resource} <- Units.one([:default, id: id]) do
EconomicResource.change_unit_of_effort(changeset, resource)
end
end
defp change_unit_of_effort(changeset, _attrs), do: changeset
defp parse_measurement_attrs(attrs) do
Enum.reduce(attrs, %{}, fn {k, v}, acc ->
if is_map(v) and Map.has_key?(v, :has_unit) do
v = CommonsPub.Common.map_key_replace(v, :has_unit, :unit_id)
# I have no idea why the numerical value isn't auto converted
Map.put(acc, k, v)
else
Map.put(acc, k, v)
{:ok, preload_all(resource)}
end
end)
end
......@@ -337,4 +229,30 @@ defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
:ok
end
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:primary_accountable_id, Map.get(attrs, :primary_accountable))
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
|> maybe_put(:current_location_id, Map.get(attrs, :current_location))
|> maybe_put(:conforms_to_id, Map.get(attrs, :conforms_to))
|> maybe_put(:contained_in_id, Map.get(attrs, :contained_in))
|> maybe_put(:unit_of_effort_id, Map.get(attrs, :unit_of_effort))
|> maybe_put(:state_id, Map.get(attrs, :state))
|> parse_measurement_attrs()
end
defp parse_measurement_attrs(attrs) do
Enum.reduce(attrs, %{}, fn {k, v}, acc ->
if is_map(v) and Map.has_key?(v, :has_unit) do
v = CommonsPub.Common.map_key_replace(v, :has_unit, :unit_id)
# I have no idea why the numerical value isn't auto converted
Map.put(acc, k, v)
else
Map.put(acc, k, v)
end
end)
end
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