PhoenixKitManufacturing.Machines (PhoenixKitManufacturing v0.2.0)

Copy Markdown View Source

Context module for managing machines and machine types.

Machines and types have a many-to-many relationship via a join table, so a machine can be tagged with several types at once (e.g. both "CNC" and "Milling").

Both machines and types use hard-delete only (simple reference data).

Activity logging

Every mutating function accepts opts \ []. When actor_uuid: is present in opts, the mutation is logged via PhoenixKit.Activity.log/1 under the "manufacturing" module key. Logging failures never crash the primary operation — both PhoenixKit.Activity.log/1 and this module's maybe_log_activity/5 rescue internally, so on a host that has not yet run core's activity migration the mutation still succeeds and the failure degrades to a Logger.warning.

Usage from IEx

alias PhoenixKitManufacturing.Machines

{:ok, cnc} = Machines.create_machine_type(%{name: "CNC"})
{:ok, mill} = Machines.create_machine(%{name: "CNC-01", code: "M-001"})
{:ok, _} = Machines.sync_machine_types(mill.uuid, [cnc.uuid])

Machines.list_machines(type_uuid: cnc.uuid)
Machines.count_machines()

Summary

Functions

Returns an Ecto.Changeset for tracking machine changes.

Returns an Ecto.Changeset for tracking machine type changes.

Returns the total count of machine types.

Returns the total count of machines.

Creates a machine type. Required: :name. Optional: :description, :status, :data.

Hard-deletes a machine. Cascades to type assignments.

Hard-deletes a machine type. Cascades to type assignments (machines keep existing, lose the link).

Fetches a machine by UUID with types preloaded. Returns nil if not found.

Fetches a machine type by UUID. Returns nil if not found.

Fetches a machine type by name (case-sensitive). Returns nil if not found.

Returns true if the machine has the given type assigned.

Returns a list of type UUIDs linked to a machine.

Lists all machine types, ordered by name.

Lists all machines, ordered by name, with their types preloaded.

Logs a module enable/disable toggle. Called from the enable_system / disable_system module lifecycle functions.

Syncs the type assignments for a machine (full replace).

Updates a machine with the given attributes.

Updates a machine type with the given attributes.

Types

list_machines_opts()

@type list_machines_opts() :: [status: String.t(), type_uuid: String.t()]

opts()

@type opts() :: keyword()

status_filter()

@type status_filter() :: [{:status, String.t()}]

Functions

change_machine(machine, attrs \\ %{})

Returns an Ecto.Changeset for tracking machine changes.

change_machine_type(machine_type, attrs \\ %{})

Returns an Ecto.Changeset for tracking machine type changes.

count_machine_types(opts \\ [])

@spec count_machine_types(status_filter()) :: non_neg_integer()

Returns the total count of machine types.

count_machines(opts \\ [])

@spec count_machines(status_filter()) :: non_neg_integer()

Returns the total count of machines.

create_machine(attrs, opts \\ [])

@spec create_machine(map(), opts()) ::
  {:ok, PhoenixKitManufacturing.Schemas.Machine.t()}
  | {:error, Ecto.Changeset.t()}

Creates a machine.

Required: :name. Optional: :code, :manufacturer, :serial_number, :description, :location_note, :status, :data, :metadata.

create_machine_type(attrs, opts \\ [])

@spec create_machine_type(map(), opts()) ::
  {:ok, PhoenixKitManufacturing.Schemas.MachineType.t()}
  | {:error, Ecto.Changeset.t()}

Creates a machine type. Required: :name. Optional: :description, :status, :data.

delete_machine(machine, opts \\ [])

Hard-deletes a machine. Cascades to type assignments.

delete_machine_type(machine_type, opts \\ [])

Hard-deletes a machine type. Cascades to type assignments (machines keep existing, lose the link).

get_machine(uuid)

@spec get_machine(String.t()) :: PhoenixKitManufacturing.Schemas.Machine.t() | nil

Fetches a machine by UUID with types preloaded. Returns nil if not found.

get_machine_type(uuid)

@spec get_machine_type(String.t()) ::
  PhoenixKitManufacturing.Schemas.MachineType.t() | nil

Fetches a machine type by UUID. Returns nil if not found.

get_machine_type_by_name(name)

@spec get_machine_type_by_name(String.t()) ::
  PhoenixKitManufacturing.Schemas.MachineType.t() | nil

Fetches a machine type by name (case-sensitive). Returns nil if not found.

has_type?(machine_uuid, type_uuid)

@spec has_type?(String.t(), String.t()) :: boolean()

Returns true if the machine has the given type assigned.

linked_type_uuids(machine_uuid)

@spec linked_type_uuids(String.t()) :: [String.t()]

Returns a list of type UUIDs linked to a machine.

list_machine_types(opts \\ [])

@spec list_machine_types(status_filter()) :: [
  PhoenixKitManufacturing.Schemas.MachineType.t()
]

Lists all machine types, ordered by name.

Options

  • :status — filter by status (e.g. "active", "inactive").

list_machines(opts \\ [])

Lists all machines, ordered by name, with their types preloaded.

Options

  • :status — filter by status.
  • :type_uuid — filter to only machines that have this type assigned.

log_module_toggle(state, opts \\ [])

@spec log_module_toggle(:enabled | :disabled, opts()) :: :ok

Logs a module enable/disable toggle. Called from the enable_system / disable_system module lifecycle functions.

sync_machine_types(machine_uuid, type_uuids, opts \\ [])

@spec sync_machine_types(String.t(), [String.t()], opts()) ::
  {:ok, :synced | :unchanged} | {:error, :type_assignment_failed}

Syncs the type assignments for a machine (full replace).

Replaces all existing assignments with the given list of type UUIDs, wrapped in a transaction for atomicity. Logs machine.types_synced only when the assignment set actually changed; a no-op sync is silent.

update_machine(machine, attrs, opts \\ [])

Updates a machine with the given attributes.

update_machine_type(machine_type, attrs, opts \\ [])

Updates a machine type with the given attributes.