Commit 6d6b7686 authored by Antonis Kalou's avatar Antonis Kalou

Cleanup Planning.Intents

parent b6867519
......@@ -119,7 +119,19 @@ defmodule ValueFlows.Hydration do
],
published_in: [
resolve: &ValueFlows.Proposal.ProposedIntentGraphQL.published_in_edge/3
]
],
resource_conforms_to: [
resolve: &ValueFlows.Planning.Intent.GraphQL.fetch_resource_conforms_to_edge/3
],
resource_inventoried_as: [
resolve: &ValueFlows.Planning.Intent.GraphQL.fetch_resource_inventoried_as_edge/3
],
input_of: [
resolve: &ValueFlows.Planning.Intent.GraphQL.fetch_input_of_edge/3
],
output_of: [
resolve: &ValueFlows.Planning.Intent.GraphQL.fetch_output_of_edge/3
],
},
claim: %{
action: [
......
......@@ -5,46 +5,20 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
require Logger
alias CommonsPub.{
# Activities,
# Communities,
GraphQL,
Repo
# User
}
alias CommonsPub.{GraphQL, Repo}
alias CommonsPub.GraphQL.{
ResolveField,
# ResolveFields,
# ResolvePage,
ResolvePages,
ResolveRootPage,
FetchPage
# FetchPages,
# CommonResolver
}
# alias CommonsPub.Resources.Resource
# alias CommonsPub.Common.Enums
# alias CommonsPub.Meta.Pointers
# alias CommonsPub.Communities.Community
# alias CommonsPub.Web.GraphQL.CommunitiesResolver
alias ValueFlows.Observation.EconomicEvent
alias ValueFlows.Observation.EconomicEvent.EconomicEvents
alias ValueFlows.Observation.EconomicEvent.Queries
# alias ValueFlows.Knowledge.Action.Actions
# alias CommonsPub.Web.GraphQL.CommonResolver
alias CommonsPub.Web.GraphQL.UploadResolver
# SDL schema import
# use Absinthe.Schema.Notation
# import_sdl path: "lib/value_flows/graphql/schemas/planning.gql"
# TODO: put in config
# @tags_seperator " "
## resolvers
def simulate(%{id: _id}, _) do
......@@ -80,15 +54,9 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
end
def events_filtered(page_opts, _ \\ nil) do
# IO.inspect(events_filtered: page_opts)
events_filter(page_opts, [])
end
# def events_filtered(page_opts, _) do
# IO.inspect(unhandled_filtering: page_opts)
# all_events(page_opts, nil)
# end
# TODO: support several filters combined, plus pagination on filtered queries
defp events_filter(%{agent: id} = page_opts, filters_acc) do
......@@ -128,8 +96,6 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
} = page_opts,
filters_acc
) do
# IO.inspect(geo_with_point: page_opts)
events_filter_next(
:geolocation,
{
......@@ -149,11 +115,7 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
} = page_opts,
filters_acc
) do
# IO.inspect(geo_with_address: page_opts)
with {:ok, coords} <- Geocoder.call(address) do
# IO.inspect(coords)
events_filter(
Map.merge(
page_opts,
......@@ -184,8 +146,6 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
} = page_opts,
filters_acc
) do
# IO.inspect(geo_without_distance: page_opts)
events_filter(
Map.merge(
page_opts,
......@@ -205,17 +165,12 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
_,
filters_acc
) do
# IO.inspect(filters_query: filters_acc)
# finally, if there's no more known params to acumulate, query with the filters
EconomicEvents.many(filters_acc)
end
defp events_filter_next(param_remove, filter_add, page_opts, filters_acc)
when is_list(param_remove) and is_list(filter_add) do
# IO.inspect(events_filter_next: param_remove)
# IO.inspect(events_filter_add: filter_add)
events_filter(Map.drop(page_opts, param_remove), filters_acc ++ filter_add)
end
......@@ -293,10 +248,8 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
FetchPage.run(%FetchPage{
queries: Queries,
query: EconomicEvent,
# cursor_fn: EconomicEvents.cursor(cursor_type),
page_opts: page_opts,
base_filters: base_filters
# data_filters: data_filters
})
end
......@@ -304,15 +257,11 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
FetchPage.run(%FetchPage{
queries: ValueFlows.Observation.EconomicEvent.Queries,
query: ValueFlows.Observation.EconomicEvent,
# preload: [:provider, :receiver, :tags],
# cursor_fn: EconomicEvents.cursor(:followers),
page_opts: page_opts,
base_filters: [
:default,
# preload: [:provider, :receiver, :tags],
user: GraphQL.current_user(info)
]
# data_filters: [page: [desc: [followers: page_opts]]],
})
end
......@@ -385,8 +334,6 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
{:ok, uploads} <- UploadResolver.upload(user, event_attrs, info),
event_attrs = Map.merge(event_attrs, uploads),
event_attrs = Map.merge(event_attrs, %{is_public: true}),
# {:ok, resource} <- ValueFlows.Observation.EconomicResource.GraphQL.create_resource(params, info),
# event_attrs = Map.merge(event_attrs, %{resource_inventoried_as: resource}),
{:ok, event, new_resource} <- EconomicEvents.create(user, event_attrs, params) do
{:ok, %{economic_event: event, economic_resource: new_resource}}
end
......
......@@ -5,7 +5,6 @@ defmodule ValueFlows.Planning.Intent.GraphQL do
require Logger
alias CommonsPub.{GraphQL, Repo}
alias CommonsPub.GraphQL.{
......@@ -50,7 +49,6 @@ defmodule ValueFlows.Planning.Intent.GraphQL do
end
def intents_filtered(page_opts, _ \\ nil) do
# IO.inspect(intents_filtered: page_opts)
intents_filter(page_opts, [])
end
......@@ -98,8 +96,6 @@ defmodule ValueFlows.Planning.Intent.GraphQL do
} = page_opts,
filters_acc
) do
# IO.inspect(geo_with_point: page_opts)
intents_filter_next(
:geolocation,
{
......@@ -119,10 +115,7 @@ defmodule ValueFlows.Planning.Intent.GraphQL do
} = page_opts,
filters_acc
) do
# IO.inspect(geo_with_address: page_opts)
with {:ok, coords} <- Geocoder.call(address) do
# IO.inspect(coords)
intents_filter(
Map.merge(
......@@ -154,8 +147,6 @@ defmodule ValueFlows.Planning.Intent.GraphQL do
} = page_opts,
filters_acc
) do
# IO.inspect(geo_without_distance: page_opts)
intents_filter(
Map.merge(
page_opts,
......@@ -175,17 +166,12 @@ defmodule ValueFlows.Planning.Intent.GraphQL do
_,
filters_acc
) do
# IO.inspect(filters_query: filters_acc)
# finally, if there's no more known params to acumulate, query with the filters
Intents.many(filters_acc)
end
defp intents_filter_next(param_remove, filter_add, page_opts, filters_acc)
when is_list(param_remove) and is_list(filter_add) do
# IO.inspect(intents_filter_next: param_remove)
# IO.inspect(intents_filter_add: filter_add)
intents_filter(Map.drop(page_opts, param_remove), filters_acc ++ filter_add)
end
......@@ -290,6 +276,46 @@ defmodule ValueFlows.Planning.Intent.GraphQL do
)
end
def fetch_resource_conforms_to_edge(%{resource_conforms_to_id: id} = thing, _, _)
when is_binary(id) do
thing = Repo.preload(thing, :resource_conforms_to)
{:ok, Map.get(thing, :resource_conforms_to)}
end
def fetch_resource_conforms_to_edge(_, _, _) do
{:ok, nil}
end
def fetch_resource_inventoried_as_edge(%{resource_inventoried_as_id: id} = thing, _, _)
when is_binary(id) do
thing = Repo.preload(thing, :resource_inventoried_as)
{:ok, Map.get(thing, :resource_inventoried_as)}
end
def fetch_resource_inventoried_as_edge(_, _, _) do
{:ok, nil}
end
def fetch_input_of_edge(%{input_of_id: id} = thing, _, _)
when is_binary(id) do
thing = Repo.preload(thing, :input_of)
{:ok, Map.get(thing, :input_of)}
end
def fetch_input_of_edge(_, _, _) do
{:ok, nil}
end
def fetch_output_of_edge(%{output_of_id: id} = thing, _, _)
when is_binary(id) do
thing = Repo.preload(thing, :output_of)
{:ok, Map.get(thing, :output_of)}
end
def fetch_output_of_edge(_, _, _) do
{:ok, nil}
end
def list_intents(page_opts, base_filters) do
FetchPage.run(%FetchPage{
queries: Queries,
......
......@@ -31,10 +31,6 @@ defmodule ValueFlows.Planning.Intent.Queries do
Enum.reduce(specs, q, &join_to(&2, &1, jq))
end
def join_to(q, :context, jq) do
join(q, jq, [intent: c], c2 in assoc(c, :context), as: :context)
end
def join_to(q, {:follow, follower_id}, jq) do
join(q, jq, [intent: c], f in Follow,
as: :follow,
......@@ -74,7 +70,6 @@ defmodule ValueFlows.Planning.Intent.Queries do
def filter(q, :default) do
filter(q, [:deleted, preload: :quantities])
# filter q, [:deleted, {:preload, :provider}, {:preload, :receiver}]
end
def filter(q, :offer) do
......@@ -266,6 +261,8 @@ defmodule ValueFlows.Planning.Intent.Queries do
:creator,
:context,
:at_location,
:resource_inventoried_as,
:resource_conforms_to,
])
|> filter({:preload, :quantities})
end
......@@ -274,7 +271,6 @@ defmodule ValueFlows.Planning.Intent.Queries do
q
|> join_to([:available_quantity, :effort_quantity, :resource_quantity])
|> preload([:available_quantity, :effort_quantity, :resource_quantity])
# preload(q, [geolocation: g], at_location: g)
end
# pagination
......
......@@ -10,7 +10,7 @@ defmodule ValueFlows.Planning.Intent.Intents do
alias CommonsPub.Meta.Pointers
alias Geolocation.Geolocations
# alias Measurement.Measure
alias ValueFlows.Knowledge.Action.Actions
alias ValueFlows.Planning.Intent
alias ValueFlows.Planning.Intent.Queries
......@@ -88,7 +88,11 @@ defmodule ValueFlows.Planning.Intent.Intents do
def preload_all(%Intent{} = intent) do
# shouldn't fail
{:ok, intent} = one(id: intent.id, preload: :all)
intent
preload_action(intent)
end
def preload_action(%Intent{} = intent) do
Map.put(intent, :action, Actions.action!(intent.action_id))
end
## mutations
......@@ -159,7 +163,7 @@ defmodule ValueFlows.Planning.Intent.Intents do
# TODO: take the user who is performing the update
# @spec update(%Intent{}, attrs :: map) :: {:ok, Intent.t()} | {:error, Changeset.t()}
def update(%Intent{} = intent, attrs) do
attrs = parse_measurement_attrs(attrs)
attrs = prepare_attrs(attrs)
Repo.transact_with(fn ->
with {:ok, intent} <- Repo.update(Intent.update_changeset(intent, attrs)),
......@@ -217,7 +221,7 @@ defmodule ValueFlows.Planning.Intent.Intents do
|> maybe_put(:input_of_id, Map.get(attrs, :input_of))
|> maybe_put(:output_of_id, Map.get(attrs, :output_of))
|> maybe_put(:resource_conforms_to_id, Map.get(attrs, :resource_conforms_to))
|> maybe_put(:resource_inventoried_as_id, Map.get(attrs, :resource_inventoried_as_id))
|> maybe_put(:resource_inventoried_as_id, Map.get(attrs, :resource_inventoried_as))
|> parse_measurement_attrs()
end
......
......@@ -218,7 +218,7 @@ defmodule ValueFlows.Simulate do
|> Map.put_new_lazy(:is_disabled, &falsehood/0)
end
def intent_input(unit, base \\ %{}) do
def intent_input(base \\ %{}) do
base
|> Map.put_new_lazy("name", &name/0)
|> Map.put_new_lazy("note", &summary/0)
......@@ -230,9 +230,6 @@ defmodule ValueFlows.Simulate do
|> Map.put_new_lazy("has_point_in_time", &future_datetime_iso/0)
|> Map.put_new_lazy("due", &future_datetime_iso/0)
|> Map.put_new_lazy("finished", &bool/0)
|> Map.put_new_lazy("available_quantity", fn -> measure_input(unit) end)
|> Map.put_new_lazy("resource_quantity", fn -> measure_input(unit) end)
|> Map.put_new_lazy("effort_quantity", fn -> measure_input(unit) end)
end
@doc "Shorter version of fake_claim!/4, but instead generates a provider and receiver."
......@@ -245,23 +242,7 @@ defmodule ValueFlows.Simulate do
claim
end
def fake_intent!(user, unit \\ nil, overrides \\ %{})
def fake_intent!(user, unit, overrides) when is_nil(unit) do
{:ok, intent} = Intents.create(user, intent(overrides))
intent
end
def fake_intent!(user, unit, overrides) do
measure_attrs = %{unit_id: unit.id}
measures = %{
resource_quantity: measure(measure_attrs),
effort_quantity: measure(measure_attrs),
available_quantity: measure(measure_attrs)
}
overrides = Map.merge(overrides, measures)
def fake_intent!(user, overrides \\ %{}) do
{:ok, intent} = Intents.create(user, intent(overrides))
intent
end
......
......@@ -22,8 +22,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
describe "intent" do
test "fetches an existing intent by ID (via Graphql/HTTP)" do
user = fake_user!()
unit = fake_unit!(user)
intent = fake_intent!(user, unit)
intent = fake_intent!(user)
q = intent_query()
conn = user_conn(user)
......@@ -39,7 +38,12 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
parent = fake_user!()
intent = fake_intent!(user, unit, %{in_scope_of: [parent.id]})
intent = fake_intent!(user, %{
resource_quantity: measure(%{unit_id: unit.id}),
effort_quantity: measure(%{unit_id: unit.id}),
available_quantity: measure(%{unit_id: unit.id}),
in_scope_of: [parent.id]
})
proposal = fake_proposal!(user)
......@@ -60,8 +64,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "fails for deleted intent" do
user = fake_user!()
unit = fake_unit!(user)
intent = fake_intent!(user, unit)
intent = fake_intent!(user)
assert {:ok, intent} = Intents.soft_delete(intent)
q = intent_query()
......@@ -168,20 +171,21 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
)
conn = user_conn(user)
vars = %{intent: intent_input(unit)}
vars = %{intent: intent_input(%{
"availableQuantity" => measure_input(unit),
})}
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
assert_intent(intent)
assert intent["availableQuantity"]["hasNumericalValue"] ==
vars.intent["available_quantity"]["hasNumericalValue"]
vars.intent["availableQuantity"]["hasNumericalValue"]
end
test "creates a new offer given valid attributes" do
user = fake_user!()
unit = fake_unit!(user)
q = create_offer_mutation(fields: [provider: [:id]])
conn = user_conn(user)
vars = %{intent: intent_input(unit)}
vars = %{intent: intent_input()}
assert intent = grumble_post_key(q, conn, :create_offer, vars)["intent"]
assert_intent(intent)
assert intent["provider"]["id"] == user.id
......@@ -189,10 +193,9 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "create a new need given valid attributes" do
user = fake_user!()
unit = fake_unit!(user)
q = create_need_mutation(fields: [receiver: [:id]])
conn = user_conn(user)
vars = %{intent: intent_input(unit)}
vars = %{intent: intent_input()}
assert intent = grumble_post_key(q, conn, :create_need, vars)["intent"]
assert_intent(intent)
assert intent["receiver"]["id"] == user.id
......@@ -200,12 +203,11 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "creates a new intent given a scope" do
user = fake_user!()
unit = fake_unit!(user)
another_user = fake_user!()
q = create_intent_mutation(fields: [in_scope_of: [:__typename]])
conn = user_conn(user)
vars = %{intent: intent_input(unit, %{"inScopeOf" => [another_user.id]})}
vars = %{intent: intent_input(%{"inScopeOf" => [another_user.id]})}
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
assert_intent(intent)
assert [context] = intent["inScopeOf"]
......@@ -214,12 +216,11 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "creates a new intent with a location" do
user = fake_user!()
unit = fake_unit!(user)
geo = fake_geolocation!(user)
q = create_intent_mutation(fields: [atLocation: [:id]])
q = create_intent_mutation(fields: [at_location: [:id]])
conn = user_conn(user)
vars = %{intent: intent_input(unit, %{"atLocation" => geo.id})}
vars = %{intent: intent_input(%{"atLocation" => geo.id})}
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
assert_intent(intent)
assert intent["atLocation"]["id"] == geo.id
......@@ -227,12 +228,11 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "creates a new intent with an action" do
user = fake_user!()
unit = fake_unit!(user)
action = action()
q = create_intent_mutation(fields: [action: [:id]])
conn = user_conn(user)
vars = %{intent: intent_input(unit, %{"action" => action.id})}
vars = %{intent: intent_input(%{"action" => action.id})}
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
assert_intent(intent)
assert intent["action"]["id"] == action.id
......@@ -245,7 +245,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
q = create_intent_mutation(fields: [provider: [:id]])
conn = user_conn(user)
vars = %{intent: intent_input(unit, %{"provider" => provider.id})}
vars = %{intent: intent_input(%{"provider" => provider.id})}
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
assert_intent(intent)
assert intent["provider"]["id"] == provider.id
......@@ -253,12 +253,11 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "creates a new intent with a receiver" do
user = fake_user!()
unit = fake_unit!(user)
receiver = fake_user!()
q = create_intent_mutation(fields: [receiver: [:id]])
conn = user_conn(user)
vars = %{intent: intent_input(unit, %{"receiver" => receiver.id})}
vars = %{intent: intent_input(%{"receiver" => receiver.id})}
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
assert_intent(intent)
assert intent["receiver"]["id"] == receiver.id
......@@ -266,7 +265,6 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "creates a new intent with a provider and a receiver" do
user = fake_user!()
unit = fake_unit!(user)
provider = fake_user!()
receiver = fake_user!()
......@@ -274,7 +272,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
conn = user_conn(user)
vars = %{
intent: intent_input(unit, %{"receiver" => receiver.id, "provider" => provider.id})
intent: intent_input(%{"receiver" => receiver.id, "provider" => provider.id})
}
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
......@@ -292,7 +290,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
vars = %{
intent:
intent_input(unit, %{"image" => %{"url" => "https://via.placeholder.com/150.png"}})
intent_input(%{"image" => %{"url" => "https://via.placeholder.com/150.png"}})
}
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
......@@ -302,7 +300,6 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "create an intent with tags" do
user = fake_user!()
unit = fake_unit!(user)
tags = some(5, fn -> fake_category!(user).id end)
......@@ -311,7 +308,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
vars = %{
intent:
intent_input(unit, %{
intent_input(%{
"tags" => tags
})
}
......@@ -329,7 +326,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
q = create_intent_mutation(fields: [action: [:id]])
conn = user_conn(user)
vars = %{intent: intent_input(unit, %{"action" => "reading"})}
vars = %{intent: intent_input(%{"action" => "reading"})}
assert [%{"status" => 404}] = grumble_post_errors(q, conn, vars)
end
end
......@@ -338,11 +335,14 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "updates an existing intent" do
user = fake_user!()
unit = fake_unit!(user)
intent = fake_intent!(user, unit)
intent = fake_intent!(user)
q = update_intent_mutation()
conn = user_conn(user)
vars = %{intent: intent_input(unit, %{"id" => intent.id})}
vars = %{intent: intent_input(%{
"id" => intent.id,
"availableQuantity" => measure_input(unit),
})}
assert resp = grumble_post_key(q, conn, :update_intent, vars)["intent"]
assert_intent(resp)
......@@ -355,15 +355,14 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
test "updates an existing intent with a scope" do
user = fake_user!()