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.
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
Functions
@spec change_machine(PhoenixKitManufacturing.Schemas.Machine.t(), map()) :: Ecto.Changeset.t()
Returns an Ecto.Changeset for tracking machine changes.
@spec change_machine_type(PhoenixKitManufacturing.Schemas.MachineType.t(), map()) :: Ecto.Changeset.t()
Returns an Ecto.Changeset for tracking machine type changes.
@spec count_machine_types(status_filter()) :: non_neg_integer()
Returns the total count of machine types.
@spec count_machines(status_filter()) :: non_neg_integer()
Returns the total count of machines.
@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.
@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.
@spec delete_machine(PhoenixKitManufacturing.Schemas.Machine.t(), opts()) :: {:ok, PhoenixKitManufacturing.Schemas.Machine.t()} | {:error, Ecto.Changeset.t()}
Hard-deletes a machine. Cascades to type assignments.
@spec delete_machine_type(PhoenixKitManufacturing.Schemas.MachineType.t(), opts()) :: {:ok, PhoenixKitManufacturing.Schemas.MachineType.t()} | {:error, Ecto.Changeset.t()}
Hard-deletes a machine type. Cascades to type assignments (machines keep existing, lose the link).
@spec get_machine(String.t()) :: PhoenixKitManufacturing.Schemas.Machine.t() | nil
Fetches a machine by UUID with types preloaded. Returns nil if not found.
@spec get_machine_type(String.t()) :: PhoenixKitManufacturing.Schemas.MachineType.t() | nil
Fetches a machine type by UUID. Returns nil if not found.
@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.
Returns true if the machine has the given type assigned.
Returns a list of type UUIDs linked to a machine.
@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").
@spec list_machines(list_machines_opts()) :: [ PhoenixKitManufacturing.Schemas.Machine.t() ]
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.
@spec log_module_toggle(:enabled | :disabled, opts()) :: :ok
Logs a module enable/disable toggle. Called from the enable_system /
disable_system module lifecycle functions.
@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.
@spec update_machine(PhoenixKitManufacturing.Schemas.Machine.t(), map(), opts()) :: {:ok, PhoenixKitManufacturing.Schemas.Machine.t()} | {:error, Ecto.Changeset.t()}
Updates a machine with the given attributes.
@spec update_machine_type( PhoenixKitManufacturing.Schemas.MachineType.t(), map(), opts() ) :: {:ok, PhoenixKitManufacturing.Schemas.MachineType.t()} | {:error, Ecto.Changeset.t()}
Updates a machine type with the given attributes.