Smart-catalogue rules — one row per (item, referenced_catalogue) pair.
Items in a smart catalogue (kind: "smart") reference other catalogues
with a value + unit. The rule row stores the user's intent; consumers
evaluate the math, and CatalogueRule.effective/2 resolves null
value / unit to the parent item's default_value / default_unit.
Provides both the bulk replace-all flow (put_catalogue_rules/3,
preferred when editing the full set in a form) and surgical single-rule
CRUD (create_* / update_* / delete_*) for CLI / external use.
Public surface is re-exported from PhoenixKitCatalogue.Catalogue.
Summary
Functions
Returns the count of rules referencing a given catalogue (non-deleted
items only). Cheaper than list_items_referencing_catalogue/1 when
you just need a badge number.
Returns the rules for an item as %{referenced_catalogue_uuid => rule}.
Returns a changeset for tracking a single rule's changes.
Inserts a single rule. Prefer put_catalogue_rules/3 for managing an
item's full set of rules; this function exists for surgical edits.
Deletes a single rule.
Fetches a single rule by {item_uuid, referenced_catalogue_uuid}.
Returns nil if not found. Does not preload the referenced catalogue.
Lists the rules attached to an item, ordered by position then by the
referenced catalogue's name. The :referenced_catalogue association
is preloaded so UIs can render the catalogue name + status without a
second query.
Lists non-deleted smart items that reference a given catalogue.
Atomic replace-all for an item's rules. See moduledoc for context.
Updates a single rule's value/unit/position.
Functions
@spec catalogue_reference_count(Ecto.UUID.t()) :: non_neg_integer()
Returns the count of rules referencing a given catalogue (non-deleted
items only). Cheaper than list_items_referencing_catalogue/1 when
you just need a badge number.
@spec catalogue_rule_map(PhoenixKitCatalogue.Schemas.Item.t() | Ecto.UUID.t()) :: %{ required(Ecto.UUID.t()) => PhoenixKitCatalogue.Schemas.CatalogueRule.t() }
Returns the rules for an item as %{referenced_catalogue_uuid => rule}.
Convenient for picker UIs that render every available catalogue row and need O(1) lookup for "is this one checked?". Preloads the referenced catalogue on each rule.
@spec change_catalogue_rule(PhoenixKitCatalogue.Schemas.CatalogueRule.t(), map()) :: Ecto.Changeset.t(PhoenixKitCatalogue.Schemas.CatalogueRule.t())
Returns a changeset for tracking a single rule's changes.
@spec create_catalogue_rule( map(), keyword() ) :: {:ok, PhoenixKitCatalogue.Schemas.CatalogueRule.t()} | {:error, Ecto.Changeset.t(PhoenixKitCatalogue.Schemas.CatalogueRule.t())}
Inserts a single rule. Prefer put_catalogue_rules/3 for managing an
item's full set of rules; this function exists for surgical edits.
@spec delete_catalogue_rule( PhoenixKitCatalogue.Schemas.CatalogueRule.t(), keyword() ) :: {:ok, PhoenixKitCatalogue.Schemas.CatalogueRule.t()} | {:error, Ecto.Changeset.t(PhoenixKitCatalogue.Schemas.CatalogueRule.t())}
Deletes a single rule.
@spec get_catalogue_rule(Ecto.UUID.t(), Ecto.UUID.t()) :: PhoenixKitCatalogue.Schemas.CatalogueRule.t() | nil
Fetches a single rule by {item_uuid, referenced_catalogue_uuid}.
Returns nil if not found. Does not preload the referenced catalogue.
@spec list_catalogue_rules(PhoenixKitCatalogue.Schemas.Item.t() | Ecto.UUID.t()) :: [ PhoenixKitCatalogue.Schemas.CatalogueRule.t() ]
Lists the rules attached to an item, ordered by position then by the
referenced catalogue's name. The :referenced_catalogue association
is preloaded so UIs can render the catalogue name + status without a
second query.
Smart items are the only ones that should have rules; a standard item
simply returns [] unless someone put rules on it manually.
@spec list_items_referencing_catalogue(Ecto.UUID.t()) :: [ PhoenixKitCatalogue.Schemas.Item.t() ]
Lists non-deleted smart items that reference a given catalogue.
Useful for warning-before-delete flows: "This catalogue is referenced by 3 smart items — deleting it cascades to those rules."
Preloads the parent catalogue so the UI can render "Services / Delivery".
@spec put_catalogue_rules(PhoenixKitCatalogue.Schemas.Item.t(), [map()], keyword()) :: {:ok, [PhoenixKitCatalogue.Schemas.CatalogueRule.t()]} | {:error, {:duplicate_referenced_catalogue, Ecto.UUID.t() | nil}} | {:error, Ecto.Changeset.t(PhoenixKitCatalogue.Schemas.CatalogueRule.t())}
Atomic replace-all for an item's rules. See moduledoc for context.
@spec update_catalogue_rule( PhoenixKitCatalogue.Schemas.CatalogueRule.t(), map(), keyword() ) :: {:ok, PhoenixKitCatalogue.Schemas.CatalogueRule.t()} | {:error, Ecto.Changeset.t(PhoenixKitCatalogue.Schemas.CatalogueRule.t())}
Updates a single rule's value/unit/position.