feat: add groups administration #372
This commit is contained in:
parent
f05fae3ea3
commit
6faa9847f4
9 changed files with 701 additions and 7 deletions
|
|
@ -244,4 +244,48 @@ defmodule Mv.Membership do
|
|||
|> Ash.Changeset.for_update(:update_single_member_field_visibility, %{})
|
||||
|> Ash.update(domain: __MODULE__)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a group by its slug.
|
||||
|
||||
Uses `Ash.Query.filter` to efficiently find a group by its slug.
|
||||
The unique index on `slug` ensures efficient lookup performance.
|
||||
The slug lookup is case-sensitive (exact match required).
|
||||
|
||||
## Parameters
|
||||
|
||||
- `slug` - The slug to search for (case-sensitive)
|
||||
- `opts` - Options including `:actor` for authorization
|
||||
|
||||
## Returns
|
||||
|
||||
- `{:ok, group}` - Found group (with members and member_count loaded)
|
||||
- `{:ok, nil}` - Group not found
|
||||
- `{:error, error}` - Error reading group
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, group} = Mv.Membership.get_group_by_slug("board-members", actor: actor)
|
||||
iex> group.name
|
||||
"Board Members"
|
||||
|
||||
iex> {:ok, nil} = Mv.Membership.get_group_by_slug("non-existent", actor: actor)
|
||||
{:ok, nil}
|
||||
|
||||
"""
|
||||
def get_group_by_slug(slug, opts \\ []) do
|
||||
actor = Keyword.get(opts, :actor)
|
||||
load_opts = Keyword.get(opts, :load, [:members, :member_count])
|
||||
|
||||
require Ash.Query
|
||||
|
||||
query =
|
||||
Mv.Membership.Group
|
||||
|> Ash.Query.filter(slug == ^slug)
|
||||
|> Ash.Query.load(load_opts)
|
||||
|
||||
opts_with_actor = if actor, do: [actor: actor], else: []
|
||||
|
||||
Ash.read_one(query, opts_with_actor)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue