Skip to content

Commit

Permalink
Expose Relevant Item / Item Groups on Location
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Mar 8, 2022
1 parent e1cdc61 commit e7f2230
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
24 changes: 24 additions & 0 deletions lib/athena/inventory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,30 @@ defmodule Athena.Inventory do
def list_items(%ItemGroup{} = item_group), do: item_group |> Ecto.assoc(:items) |> Repo.all()
def list_items(%Event{} = event), do: event |> Ecto.assoc(:items) |> Repo.all()

def list_relevant_items_query(location)

def list_relevant_items_query(%Location{id: location_id}) do
from(item in Item,
join: item_group in assoc(item, :item_group),
as: :item_group,
join: movement in assoc(item, :movements),
as: :movement,
where:
movement.source_location_id == ^location_id or
movement.destination_location_id == ^location_id,
group_by: item.id,
order_by: item.name
)
end

def list_relevant_item_groups_query(%Location{} = location) do
from([item, item_group: item_group] in list_relevant_items_query(location),
group_by: item_group.id,
select: item_group,
order_by: item_group.name
)
end

def list_relevant_items(%Location{id: location_id}, %ItemGroup{id: item_group_id}),
do:
Repo.all(
Expand Down
6 changes: 0 additions & 6 deletions lib/athena/inventory/location.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ defmodule Athena.Inventory.Location do
use Athena, :model

alias Athena.Inventory.Event
alias Athena.Inventory.Item
alias Athena.Inventory.ItemGroup
alias Athena.Inventory.Movement

@type t :: %__MODULE__{
name: String.t(),
event: association(Event.t()),
item_groups: association([ItemGroup.t()]),
items: association([Item.t()]),
movements_in: association([Movement.t()]),
movements_out: association([Movement.t()]),
inserted_at: DateTime.t(),
Expand All @@ -25,8 +21,6 @@ defmodule Athena.Inventory.Location do
field :name, :string

belongs_to :event, Event
has_many :item_groups, through: [:event, :item_groups]
has_many :items, through: [:event, :item_groups, :items]
has_many :movements_in, Movement, foreign_key: :destination_location_id
has_many :movements_out, Movement, foreign_key: :source_location_id

Expand Down
8 changes: 8 additions & 0 deletions lib/athena_web/schema/location.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ defmodule AthenaWeb.Schema.Location do
resolve &Resolver.stock/3
end

connection field :items, node_type: :item do
resolve &Resolver.items/3
end

connection field :item_groups, node_type: :item_group do
resolve &Resolver.item_groups/3
end

field :inserted_at, non_null(:datetime)
field :updated_at, non_null(:datetime)

Expand Down
16 changes: 16 additions & 0 deletions lib/athena_web/schema/location/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,20 @@ defmodule AthenaWeb.Schema.Location.Resolver do
nil
)
end

@spec items(parent :: Location.t(), args :: map(), resolution :: Absinthe.Resolution.t()) ::
{:ok, term()} | {:error, term()}
def items(%Location{} = location, args, _resolution) do
location
|> Inventory.list_relevant_items_query()
|> connection_from_query(args, &Repo.all/1, nil)
end

@spec item_groups(parent :: Location.t(), args :: map(), resolution :: Absinthe.Resolution.t()) ::
{:ok, term()} | {:error, term()}
def item_groups(%Location{} = location, args, _resolution) do
location
|> Inventory.list_relevant_item_groups_query()
|> connection_from_query(args, &Repo.all/1, nil)
end
end
22 changes: 22 additions & 0 deletions test/athena_web/schema/query/node/location_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ defmodule AthenaWeb.Schema.Query.Node.LocationTest do
}
}
}
itemGroups(first: 10) {
edges {
node {
id
}
}
}
items(first: 10) {
edges {
node {
id
}
}
}
stock(first: 10) {
edges {
node {
Expand Down Expand Up @@ -61,7 +75,9 @@ defmodule AthenaWeb.Schema.Query.Node.LocationTest do
event = event()
location = location(event, name: "Gallusplatz")
item_group = item_group(event, name: "Bier")
unrelated_item_group = item_group(event, name: "Cookies")
item = item(item_group, name: "Lager")
_unrelated_item = item(unrelated_item_group, name: "Chocolate Chip")

supply =
movement(item, amount: 1, source_location_id: nil, destination_location_id: location.id)
Expand Down Expand Up @@ -92,6 +108,12 @@ defmodule AthenaWeb.Schema.Query.Node.LocationTest do
"movementsOut" => %{
"edges" => [%{"node" => %{"id" => ^consumption_node_id}}]
},
"itemGroups" => %{
"edges" => [%{"node" => %{"id" => ^item_group_node_id}}]
},
"items" => %{
"edges" => [%{"node" => %{"id" => ^item_node_id}}]
},
"stock" => %{
"edges" => [
%{
Expand Down

0 comments on commit e7f2230

Please sign in to comment.