Skip to content

Commit

Permalink
feat(models/board_moderator): implement function to remove board mode…
Browse files Browse the repository at this point in the history
…rators from specific board
  • Loading branch information
akinsey committed Jul 24, 2023
1 parent c8dcb4c commit da0da50
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/epochtalk_server/models/board_moderator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,32 @@ defmodule EpochtalkServer.Models.BoardModerator do
users
end)
end

@doc """
Removes list of users from the list of moderators for a specific `Board`
"""
@spec remove_moderators_by_username(
board_id :: non_neg_integer,
usernames :: [String.t()]
) ::
{:ok, removed_moderators :: [User.t()] | []}
def remove_moderators_by_username(board_id, usernames)
when is_integer(board_id) and is_list(usernames) do
Repo.transaction(fn ->
# fetches all users in list with role data attached for return
users = User.by_usernames(usernames)

user_ids = Enum.map(users, & &1.id)

# remove board moderators
query =
from bm in BoardModerator,
where: bm.board_id == ^board_id and bm.user_id in ^user_ids

Repo.delete_all(query)

# return users upon successfully adding new moderators
users
end)
end
end

0 comments on commit da0da50

Please sign in to comment.