Voile.GLAM.CollectionHelper (Voile v0.1.31)

Copy Markdown View Source

Helper functions for working with GLAM collections and curator permissions.

This module provides convenient functions for common GLAM collection operations with built-in permission checking.

Summary

Functions

Get available resource classes for a user to create collections.

Check if user can create a collection with the given resource class.

Get collection count grouped by GLAM type for a user.

Get collection statistics for a user's accessible collections.

Get a single collection with permission check.

List all collections accessible to a user based on their GLAM curator role.

Validate collection creation parameters against user's permissions.

Functions

available_resource_classes(user)

Get available resource classes for a user to create collections.

Returns only resource classes of GLAM types the user can manage. Super admins get all resource classes.

Examples

# Get resource classes user can use
available_resource_classes(user)
# Librarian returns: [%ResourceClass{glam_type: "Library"}, ...]

can_create_with_resource_class?(resource_class_id, user)

Check if user can create a collection with the given resource class.

Returns {:ok, resource_class} if user can create collections of this GLAM type. Returns {:error, :unauthorized} if user can't create this GLAM type. Returns {:error, :not_found} if resource class doesn't exist.

Examples

case can_create_with_resource_class?(resource_class_id, user) do
  {:ok, resource_class} ->
    # Proceed with creation

  {:error, :unauthorized} ->
    # Show error
end

count_by_glam_type(user)

Get collection count grouped by GLAM type for a user.

Returns a map like: %{"Library" => 10, "Archive" => 5}

Examples

count_by_glam_type(user)
# => %{"Library" => 10, "Archive" => 5}

get_collection_stats(user)

Get collection statistics for a user's accessible collections.

Returns a map with counts by status, GLAM type, etc.

Examples

get_collection_stats(user)
# => %{
#   total: 50,
#   by_status: %{"published" => 30, "draft" => 20},
#   by_glam_type: %{"Library" => 40, "Archive" => 10}
# }

get_collection_with_permission(collection_id, user)

Get a single collection with permission check.

Returns {:ok, collection} if user can manage the collection. Returns {:error, :not_found} if collection doesn't exist. Returns {:error, :unauthorized} if user can't manage the collection.

Examples

case get_collection_with_permission(collection_id, user) do
  {:ok, collection} ->
    # User can manage this collection

  {:error, :unauthorized} ->
    # Show error message

  {:error, :not_found} ->
    # Collection doesn't exist
end

list_accessible_collections(user, opts \\ [])

List all collections accessible to a user based on their GLAM curator role.

Super admins see all collections. Curators only see collections of their GLAM type.

Options

  • :preload - List of associations to preload (default: [:resource_class])
  • :order_by - Field to order by (default: [desc: :inserted_at])
  • :status - Filter by status (e.g., "published", "draft")
  • :glam_type - Filter by specific GLAM type

Examples

# Get all accessible collections
list_accessible_collections(user)

# Get published Library collections only
list_accessible_collections(user, status: "published", glam_type: "Library")

validate_collection_creation(params, user)

Validate collection creation parameters against user's permissions.

Returns :ok if user can create a collection with the given params. Returns {:error, reason} otherwise.

Examples

case validate_collection_creation(params, user) do
  :ok ->
    create_collection(params)

  {:error, reason} ->
    show_error(reason)
end