PhoenixKitCatalogue.Catalogue.ItemSupplierInfos (PhoenixKitCatalogue v0.11.0)

Copy Markdown View Source

Context for managing the phoenix_kit_cat_item_supplier_info junction table.

Each row links a catalogue item to a supplier (local or CRM-sourced) and carries a snapshot of the supplier's name at write time so the record stays readable even after renaming or deletion.

At most one row per item may have is_primary: true — enforced by a partial unique index and by set_primary/1, which uses a transaction to clear any existing primary before promoting the target row.

Summary

Functions

Creates a supplier-info row.

Deletes a supplier-info row.

Fetches a supplier-info row by UUID. Returns nil if not found.

Lists all supplier-info rows for an item, ordered by position then inserted_at.

Returns the primary supplier-info row for an item, or nil if none is marked primary.

Promotes a supplier-info row to primary for its item.

Updates a supplier-info row.

Functions

create(attrs, opts \\ [])

Creates a supplier-info row.

When the item has no primary supplier yet, the newly linked row is auto-promoted to primary (an item with suppliers but no primary is a valid state only when the user explicitly demotes it).

delete(info, opts \\ [])

Deletes a supplier-info row.

get(uuid)

Fetches a supplier-info row by UUID. Returns nil if not found.

list_for_item(item_uuid)

Lists all supplier-info rows for an item, ordered by position then inserted_at.

primary_for_item(item_uuid)

@spec primary_for_item(Ecto.UUID.t()) ::
  PhoenixKitCatalogue.Schemas.ItemSupplierInfo.t() | nil

Returns the primary supplier-info row for an item, or nil if none is marked primary.

set_primary(info, opts \\ [])

Promotes a supplier-info row to primary for its item.

Runs in a transaction: clears is_primary on all sibling rows first, then sets is_primary: true on the target. Respects the partial unique index — concurrent callers produce a constraint violation rather than double-marking.

update(info, attrs, opts \\ [])

Updates a supplier-info row.