feat: filter by category/location on click
- add filtering by storage location
This commit is contained in:
@@ -224,6 +224,27 @@ defmodule ComponentsElixir.Inventory do
|
||||
|
||||
def get_category_and_descendant_ids(_), do: []
|
||||
|
||||
@doc """
|
||||
Gets all storage location IDs that are descendants of the given storage location ID, including the location itself.
|
||||
This is used for filtering components by storage location and all its sub-locations.
|
||||
Returns an empty list if the storage location doesn't exist.
|
||||
|
||||
Note: This implementation loads all storage locations into memory for traversal, which is efficient
|
||||
for typical storage location tree sizes (hundreds of locations). For very large storage location trees,
|
||||
a recursive CTE query could be used instead.
|
||||
"""
|
||||
def get_storage_location_and_descendant_ids(storage_location_id) when is_integer(storage_location_id) do
|
||||
storage_locations = list_storage_locations()
|
||||
|
||||
# Verify the storage location exists before getting descendants
|
||||
case Enum.find(storage_locations, &(&1.id == storage_location_id)) do
|
||||
nil -> []
|
||||
_storage_location -> ComponentsElixir.Inventory.Hierarchical.descendant_ids(storage_locations, storage_location_id, &(&1.parent_id))
|
||||
end
|
||||
end
|
||||
|
||||
def get_storage_location_and_descendant_ids(_), do: []
|
||||
|
||||
## Components
|
||||
|
||||
@doc """
|
||||
@@ -245,7 +266,9 @@ defmodule ComponentsElixir.Inventory do
|
||||
where(query, [c], c.category_id in ^category_ids)
|
||||
|
||||
{:storage_location_id, storage_location_id}, query when not is_nil(storage_location_id) ->
|
||||
where(query, [c], c.storage_location_id == ^storage_location_id)
|
||||
# Get the storage location and all its descendant storage location IDs
|
||||
storage_location_ids = get_storage_location_and_descendant_ids(storage_location_id)
|
||||
where(query, [c], c.storage_location_id in ^storage_location_ids)
|
||||
|
||||
{:search, search_term}, query when is_binary(search_term) and search_term != "" ->
|
||||
search_pattern = "%#{search_term}%"
|
||||
|
||||
Reference in New Issue
Block a user