Commit 3cdd5ad9 authored by Mayel's avatar Mayel

Merge branch 'cleanup/pagination' into extension/valueflows

parents a2a3aeda 8655f65e
Pipeline #214853208 failed with stage
in 1 minute and 8 seconds
......@@ -19,7 +19,7 @@ defmodule CommonsPub.Collections.Collection do
table_schema "mn_collection" do
# belongs_to(:actor, Character)
has_one(:character, CommonsPub.Characters.Character, references: :id, foreign_key: :id)
has_one(:character, Character, references: :id, foreign_key: :id)
belongs_to(:creator, User)
......
......@@ -24,8 +24,6 @@ defmodule CommonsPub.Communities do
alias CommonsPub.Users.User
alias CommonsPub.Workers.APPublishWorker
alias CommonsPub.Utils.Web.CommonHelper
### Cursor generators
def cursor(:followers), do: &[&1.follower_count, &1.id]
......
......@@ -112,7 +112,7 @@ type GeolocationQuery {
limit: Int
before: [Cursor]
after: [Cursor]
): [SpatialThingsPage!]
): SpatialThingsPage!
}
type GeolocationMutation {
......
......@@ -36,8 +36,8 @@ defmodule Measurement.Hydration do
unit: [
resolve: &Measurement.Unit.GraphQL.unit/2
],
measures: [
resolve: &Measurement.Measure.GraphQL.measures/2
measures_pages: [
resolve: &Measurement.Measure.GraphQL.measures_pages/2
],
# all_measures: [
# resolve: &Measurement.Measure.GraphQL.all_measures/2
......
......@@ -29,7 +29,7 @@ defmodule Measurement.Measure.GraphQL do
})
end
def measures(page_opts, info) do
def measures_pages(page_opts, info) do
ResolveRootPage.run(%ResolveRootPage{
module: __MODULE__,
fetcher: :fetch_measures,
......@@ -53,10 +53,10 @@ defmodule Measurement.Measure.GraphQL do
FetchPage.run(%FetchPage{
queries: Measurement.Measure.Queries,
query: Measurement.Measure,
# cursor_fn: measures.cursor(:followers),
cursor_fn: & &1.id,
page_opts: page_opts,
base_filters: [user: GraphQL.current_user(info)],
data_filters: [:dafault]
data_filters: [:default]
})
end
......
......@@ -85,6 +85,7 @@ defmodule Measurement.Measure.Queries do
## by field values
def filter(q, {:id, id}) when is_binary(id) do
where(q, [measure: c], c.id == ^id)
end
......@@ -120,30 +121,6 @@ defmodule Measurement.Measure.Queries do
select(q, [measure: c], {field(c, ^key), count(c.id)})
end
# pagination
def filter(q, {:limit, limit}) do
limit(q, ^limit)
end
def filter(q, {:paginate_id, %{after: a, limit: limit}}) do
limit = limit + 2
q
|> where([measure: c], c.id >= ^a)
|> limit(^limit)
end
def filter(q, {:paginate_id, %{before: b, limit: limit}}) do
q
|> where([measure: c], c.id <= ^b)
|> filter(limit: limit + 2)
end
def filter(q, {:paginate_id, %{limit: limit}}) do
filter(q, limit: limit + 1)
end
# defp page(q, %{limit: limit}, _), do: filter(q, limit: limit + 1)
def inc_quantity(id, amount) do
......
......@@ -140,11 +140,11 @@ type MeasuresPage {
type MeasurementQuery {
unit(id: ID): Unit
units(start: ID, limit: Int): [Unit!]
unitsPages(limit: Int, before: [Cursor], after: [Cursor]): [UnitsPage!]
unitsPages(limit: Int, before: [Cursor], after: [Cursor]): UnitsPage!
measure(id: ID): Measure
measures(start: ID, limit: Int): [Measure!]
measuresPages(limit: Int, before: [Cursor], after: [Cursor]): [MeasuresPage!]
measuresPages(limit: Int, before: [Cursor], after: [Cursor]): MeasuresPage!
}
type MeasurementMutation {
......
......@@ -3,7 +3,6 @@ defmodule CommonsPub.Tag.Simulate do
@moduledoc false
import CommonsPub.Utils.Simulation
import CommonsPub.Utils.Trendy
alias CommonsPub.Tag.Categories
......
......@@ -63,14 +63,26 @@ defmodule ValueFlows.Claim do
receiver_id: receiver.id,
is_public: true
)
|> change_measures(attrs)
|> common_changeset()
|> common_changeset(attrs)
end
def update_changeset(%__MODULE__{} = claim, attrs) do
claim
|> Changeset.cast(attrs, @cast)
|> common_changeset(attrs)
end
def validate_required(changeset) do
Changeset.validate_required(changeset, @required)
end
defp common_changeset(changeset, attrs) do
changeset
|> change_measures(attrs)
|> change_public()
|> change_disabled()
end
def change_measures(changeset, %{} = attrs) do
measures = Map.take(attrs, measure_fields())
......@@ -79,11 +91,5 @@ defmodule ValueFlows.Claim do
end)
end
defp common_changeset(changeset) do
changeset
|> change_public()
|> change_disabled()
end
def measure_fields, do: [:resource_quantity, :effort_quantity]
end
......@@ -15,19 +15,11 @@ defmodule ValueFlows.Claim.Claims do
def many(filters \\ []), do: {:ok, Repo.all(Queries.query(Claim, filters))}
def preload_all(%Claim{} = claim) do
Repo.preload(claim, [
:creator,
:provider,
:receiver,
:context,
:resource_conforms_to,
:resource_quantity,
:effort_quantity,
:triggered_by,
])
# shouldn't fail
{:ok, claim} = one(id: claim.id, preload: :all)
claim
end
# TODO: change attributes and then pass to changeset, use preload for rest
def create(%User{} = creator, %{id: _} = provider, %{id: _} = receiver, %{} = attrs) do
Repo.transact_with(fn ->
attrs = prepare_attrs(attrs)
......@@ -42,12 +34,19 @@ defmodule ValueFlows.Claim.Claims do
end)
end
def update(%Claim{} = claim, %{} = _attrs) do
{:ok, claim}
def update(%Claim{} = claim, %{} = attrs) do
Repo.transact_with(fn ->
attrs = prepare_attrs(attrs)
claim
|> Claim.update_changeset(attrs)
|> Repo.update()
|> CommonsPub.Common.maybe_ok_error(&preload_all/1)
end)
end
def soft_delete(%Claim{} = claim) do
{:ok, claim}
CommonsPub.Common.Deletion.soft_delete(claim)
end
defp prepare_attrs(attrs) do
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Claim.GraphQL do
# alias CommonsPub.Web.GraphQL.{CommonResolver}
require Logger
# use Absinthe.Schema.Notation
# import_sdl path: "lib/value_flows/graphql/schemas/claim.gql"
alias CommonsPub.{GraphQL, Repo}
alias CommonsPub.Meta.Pointers
alias CommonsPub.GraphQL.{FetchPage, ResolveField, ResolveRootPage}
alias ValueFlows.Claim.Claims
def claim(%{id: id}, info) do
ResolveField.run(%ResolveField{
module: __MODULE__,
fetcher: :fetch_claim,
context: id,
info: info
})
end
def claims(page_opts, info) do
ResolveRootPage.run(%ResolveRootPage{
module: __MODULE__,
fetcher: :fetch_claims,
page_opts: page_opts,
info: info,
cursor_validators: [&(is_integer(&1) and &1 >= 0), &Ecto.ULID.cast/1]
})
end
def fetch_claim(_info, id) do
Claims.one([:default, id: id])
end
def fetch_events(page_opts, info) do
FetchPage.run(%FetchPage{
queries: ValueFlows.Claim.Queries,
query: ValueFlows.Claim,
page_opts: page_opts,
base_filters: [
:default,
user: GraphQL.current_user(info)
]
})
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_triggered_by_edge(%{triggered_by_id: id} = thing, _, _) when is_binary(id) do
thing = Repo.preload(thing, :triggered_by)
{:ok, Map.get(thing, :triggered_by)}
end
def fetch_triggered_by_edge(_, _, _) do
{:ok, nil}
end
def create_claim(%{claim: %{provider: provider_id, receiver: receiver_id} = attrs}, info) do
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, provider} <- Pointers.one(id: provider_id),
{:ok, receiver} <- Pointers.one(id: receiver_id),
{:ok, claim} <- Claims.create(user, provider, receiver, attrs) do
{:ok, %{claim: claim}}
end
end
def update_claim(%{claim: %{id: id} = attrs}, info) do
with {:ok, _user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, claim} <- claim(%{id: id}, info),
{:ok, claim} <- Claims.update(claim, attrs) do
{:ok, %{claim: claim}}
end
end
def delete_claim(%{id: id}, info) do
with {:ok, _user} <- GraphQL.current_user_or_not_logged_in(info),
{:ok, claim} <- claim(%{id: id}, info),
{:ok, _} <- Claims.soft_delete(claim) do
{:ok, true}
end
end
end
......@@ -10,7 +10,7 @@ defmodule ValueFlows.Claim.Migrations do
def up do
create_pointable_table(ValueFlows.Claim) do
add(:note, :text)
add(:agreed_in, :string)
add(:agreed_in, :text)
add(:action_id, :string)
add(:finished, :boolean)
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Claim.Queries do
import CommonsPub.Common.Query, only: [match_admin: 0]
import Ecto.Query
alias CommonsPub.Follows.Follow
alias CommonsPub.Users.User
alias ValueFlows.Claim
def query(Claim) do
......@@ -27,6 +30,13 @@ defmodule ValueFlows.Claim.Queries do
join(q, jq, [claim: c], c2 in assoc(c, :context), as: :context)
end
def join_to(q, {:follow, follower_id}, jq) do
join(q, jq, [claim: c], f in Follow,
as: :follow,
on: c.id == f.context_id and f.creator_id == ^follower_id
)
end
def filter(q, filters) when is_list(filters) do
Enum.reduce(filters, q, &filter(&2, &1))
end
......@@ -34,6 +44,8 @@ defmodule ValueFlows.Claim.Queries do
def filter(q, {:join, {join, qual}}), do: join_to(q, join, qual)
def filter(q, {:join, join}), do: join_to(q, join)
## by status
def filter(q, :default) do
filter(q, [:deleted])
end
......@@ -42,6 +54,31 @@ defmodule ValueFlows.Claim.Queries do
where(q, [claim: c], is_nil(c.deleted_at))
end
def filter(q, :disabled) do
where(q, [claim: c], is_nil(c.disabled_at))
end
def filter(q, :private) do
where(q, [claim: c], not is_nil(c.published_at))
end
## by user
def filter(q, {:creator, match_admin()}), do: q
def filter(q, {:creator, nil}) do
filter(q, ~w(disabled private)a)
end
def filter(q, {:creator, %User{id: id}}) do
q
|> join_to(follow: id)
|> where([claim: c, follow: f], not is_nil(c.published_at) or not is_nil(f.id))
|> filter(~w(disabled)a)
end
## by field values
def filter(q, {:id, id}) when is_binary(id) do
where(q, [claim: c], c.id == ^id)
end
......@@ -49,4 +86,51 @@ defmodule ValueFlows.Claim.Queries do
def filter(q, {:id, ids}) when is_list(ids) do
where(q, [claim: c], c.id in ^ids)
end
def filter(q, {:provider_id, id}) when is_binary(id) do
where(q, [claim: c], c.provider_id == ^id)
end
def filter(q, {:provider_id, ids}) when is_list(ids) do
where(q, [claim: c], c.provider_id in ^ids)
end
def filter(q, {:receiver_id, id}) when is_binary(id) do
where(q, [claim: c], c.receiver_id == ^id)
end
def filter(q, {:receiver_id, ids}) when is_list(ids) do
where(q, [claim: c], c.receiver_id in ^ids)
end
def filter(q, {:context_id, id}) when is_binary(id) do
where(q, [claim: c], c.context_id == ^id)
end
def filter(q, {:context_id, ids}) when is_list(ids) do
where(q, [claim: c], c.context_id in ^ids)
end
def filter(q, {:action_id, ids}) when is_list(ids) do
where(q, [claim: c], c.action_id in ^ids)
end
def filter(q, {:action_id, id}) when is_binary(id) do
where(q, [claim: c], c.action_id == ^id)
end
## preloading
def filter(q, {:preload, :all}) do
preload(q, [
:creator,
:provider,
:receiver,
:resource_conforms_to,
:resource_quantity,
:effort_quantity,
:context,
:triggered_by,
])
end
end
......@@ -119,7 +119,48 @@ 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: [
resolve: &ValueFlows.Knowledge.Action.GraphQL.action_edge/3,
],
provider: [
resolve: &ValueFlows.Util.GraphQL.fetch_provider_edge/3
],
receiver: [
resolve: &ValueFlows.Util.GraphQL.fetch_receiver_edge/3
],
resource_quantity: [
resolve: &ValueFlows.Util.GraphQL.resource_quantity_edge/3
],
effort_quantity: [
resolve: &ValueFlows.Util.GraphQL.effort_quantity_edge/3
],
resource_conforms_to: [
resolve: &ValueFlows.Claim.GraphQL.fetch_resource_conforms_to_edge/3,
],
triggered_by: [
resolve: &ValueFlows.Claim.GraphQL.fetch_triggered_by_edge/3,
],
in_scope_of: [
resolve: &CommonResolver.context_edge/3,
],
creator: [
resolve: &UsersResolver.creator_edge/3
],
},
economic_event: %{
canonical_url: [
......@@ -272,6 +313,14 @@ defmodule ValueFlows.Hydration do
resolve: &ValueFlows.Agent.GraphQL.organizations/2
],
# Claim
claim: [
resolve: &ValueFlows.Claim.GraphQL.claim/2,
],
claims: [
resolve: &ValueFlows.Claim.GraphQL.claims/2,
],
# Knowledge
action: [
resolve: &ValueFlows.Knowledge.Action.GraphQL.action/2
......@@ -366,6 +415,9 @@ defmodule ValueFlows.Hydration do
# start Mutation resolvers
value_flows_mutation: %{
create_claim: [
resolve: &ValueFlows.Claim.GraphQL.create_claim/2,
],
create_intent: [
resolve: &ValueFlows.Planning.Intent.GraphQL.create_intent/2
],
......@@ -387,6 +439,9 @@ defmodule ValueFlows.Hydration do
# create_action: [
# resolve: &ValueFlows.Knowledge.Action.GraphQL.create_action/2
# ],
update_claim: [
resolve: &ValueFlows.Claim.GraphQL.update_claim/2,
],
update_intent: [
resolve: &ValueFlows.Planning.Intent.GraphQL.update_intent/2
],
......@@ -402,6 +457,9 @@ defmodule ValueFlows.Hydration do
update_process: [
resolve: &ValueFlows.Observation.Process.GraphQL.update_process/2
],
delete_claim: [
resolve: &ValueFlows.Claim.GraphQL.delete_claim/2,
],
delete_intent: [
resolve: &ValueFlows.Planning.Intent.GraphQL.delete_intent/2
],
......
......@@ -3,46 +3,20 @@ defmodule ValueFlows.Knowledge.ProcessSpecification.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.Knowledge.ProcessSpecification
alias ValueFlows.Knowledge.ProcessSpecification.ProcessSpecifications
alias ValueFlows.Knowledge.ProcessSpecification.Queries
# alias ValueFlows.Knowledge.Action.Actions
# alias CommonsPub.Web.GraphQL.CommonResolver
alias CommonsPub.Web.GraphQL.UploadResolver
# SDL schema import
# use Absinthe.Schema.Notation