PhoenixKitCatalogue.Catalogue.Rules (PhoenixKitCatalogue v0.1.14)

Copy Markdown View Source

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.

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

catalogue_reference_count(catalogue_uuid)

@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.

catalogue_rule_map(item_or_uuid)

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.

change_catalogue_rule(rule, attrs \\ %{})

Returns a changeset for tracking a single rule's changes.

create_catalogue_rule(attrs, opts \\ [])

Inserts a single rule. Prefer put_catalogue_rules/3 for managing an item's full set of rules; this function exists for surgical edits.

delete_catalogue_rule(rule, opts \\ [])

Deletes a single rule.

get_catalogue_rule(item_uuid, referenced_catalogue_uuid)

@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.

list_catalogue_rules(item_uuid)

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.

list_items_referencing_catalogue(catalogue_uuid)

@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".

put_catalogue_rules(item, rules, opts \\ [])

@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.

update_catalogue_rule(rule, attrs, opts \\ [])

Updates a single rule's value/unit/position.