PhoenixKitCRM.RoleSettings (PhoenixKitCRM v0.2.2)

Copy Markdown View Source

Context for managing which roles have CRM access enabled.

Summary

Functions

Returns whether the given role has CRM access enabled.

Lists all roles eligible for CRM access.

Lists all roles that have CRM access enabled.

Lists CRM-enabled roles with the count of users assigned to each role.

Enables or disables CRM access for a role.

Functions

enabled?(role_uuid)

@spec enabled?(binary()) :: boolean()

Returns whether the given role has CRM access enabled.

Examples

iex> enabled?("some-uuid")
false

list_eligible_roles()

@spec list_eligible_roles() :: [PhoenixKit.Users.Role.t()]

Lists all roles eligible for CRM access.

Returns all non-system roles (i.e. roles where is_system_role is false).

Examples

iex> list_eligible_roles()
[%Role{name: "Manager"}, %Role{name: "User"}, ...]

list_enabled()

@spec list_enabled() :: [PhoenixKit.Users.Role.t()]

Lists all roles that have CRM access enabled.

Returns joined %PhoenixKit.Users.Role{} structs.

Examples

iex> list_enabled()
[%Role{name: "Manager"}, ...]

list_enabled_with_user_counts()

@spec list_enabled_with_user_counts() :: [
  %{uuid: binary(), name: String.t(), count: integer()}
]

Lists CRM-enabled roles with the count of users assigned to each role.

Single query — left-joins role_assignments so roles with zero users still appear (count = 0). Used by the CRM overview page in place of N+1 count_users_with_role/1 calls.

Examples

iex> list_enabled_with_user_counts()
[%{uuid: "...", name: "Manager", count: 3}, ...]

set_enabled(role_uuid, enabled?)

@spec set_enabled(binary(), boolean()) ::
  {:ok, PhoenixKitCRM.RoleSetting.t()} | {:error, Ecto.Changeset.t()}

Enables or disables CRM access for a role.

Upserts the setting row and triggers a sidebar refresh.

Examples

iex> set_enabled("some-uuid", true)
{:ok, %RoleSetting{}}

iex> set_enabled("some-uuid", false)
{:ok, %RoleSetting{}}