Skip to content

Commit

Permalink
started change for admin users
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek committed Nov 18, 2024
1 parent 10a1148 commit 5147d19
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 121 deletions.
2 changes: 1 addition & 1 deletion assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Hooks.SetIdeaLabelBackgroundColor = {
};

let liveSocket = new LiveSocket("/live", Socket, {
hooks: Hooks, params: { _csrf_token: csrfToken }
hooks: Hooks, params: { _csrf_token: csrfToken, adminSecret: location.hash.substring(1) }
})

// Show progress bar on live navigation and form submits
Expand Down
42 changes: 39 additions & 3 deletions lib/mindwendel/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ defmodule Mindwendel.Accounts do
import Ecto.Query, warn: false
alias Mindwendel.Repo
alias Mindwendel.Accounts.User
alias Mindwendel.Brainstormings
alias Mindwendel.Brainstormings.Brainstorming
alias Mindwendel.Accounts.BrainstormingModeratingUser

require Logger

Expand Down Expand Up @@ -70,6 +72,43 @@ defmodule Mindwendel.Accounts do
|> Repo.update()
end

@doc """
Adds a user as moderating user to a brainstorming.
## Examples
iex> add_moderating_user(brainstorming, user)
%Brainstorming{}
"""
def add_moderating_user(%Brainstorming{} = brainstorming, %User{} = user) do
unless user.id in Enum.map(brainstorming.users, fn e -> e.id end) do
moderating_user_result =
%BrainstormingModeratingUser{brainstorming_id: brainstorming.id, user_id: user.id}
|> BrainstormingModeratingUser.changeset()
|> Repo.insert()

case moderating_user_result do
# reload brainstorming
{:ok, _} -> Brainstormings.get_brainstorming!(brainstorming.id)
{:error, _} -> brainstorming
end
else
brainstorming
end
end

def add_moderating_user(%Brainstorming{} = brainstorming, user_id) when is_binary(user_id) do
case Ecto.UUID.dump(user_id) do
:error -> brainstorming
{:ok, _} -> add_moderating_user(brainstorming, get_or_create_user(user_id))
end
end

def add_moderating_user(%Brainstorming{} = brainstorming, user_id) when is_nil(user_id) do
brainstorming
end

@doc """
Connects user to a brainstorm.
Expand All @@ -80,9 +119,6 @@ defmodule Mindwendel.Accounts do
iex> merge_brainstorming_user(brainstorming, user)
%Brainstorming{}
iex> update_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def merge_brainstorming_user(%Brainstorming{} = brainstorming, %User{} = user) do
# credo:disable-for-next-line
Expand Down
7 changes: 0 additions & 7 deletions lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ defmodule Mindwendel.Brainstormings do
alias Mindwendel.Lanes
alias Mindwendel.Ideas
alias Mindwendel.Brainstormings.Brainstorming
alias Mindwendel.Brainstormings.BrainstormingModeratingUser

require Logger

Expand All @@ -36,12 +35,6 @@ defmodule Mindwendel.Brainstormings do
)
end

def add_moderating_user(%Brainstorming{} = brainstorming, %User{} = user) do
%BrainstormingModeratingUser{brainstorming_id: brainstorming.id, user_id: user.id}
|> BrainstormingModeratingUser.changeset()
|> Repo.insert()
end

@doc """
Returns the list of brainstormings.
Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel/brainstormings/brainstorming.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Mindwendel.Brainstormings.Brainstorming do
alias Mindwendel.Brainstormings.Idea
alias Mindwendel.Brainstormings.IdeaLabel
alias Mindwendel.Brainstormings.Lane
alias Mindwendel.Brainstormings.BrainstormingModeratingUser
alias Mindwendel.Accounts.BrainstormingModeratingUser
alias Mindwendel.Accounts.User
alias Mindwendel.Accounts.BrainstormingUser

Expand Down
23 changes: 0 additions & 23 deletions lib/mindwendel/brainstormings/brainstorming_moderating_user.ex

This file was deleted.

35 changes: 0 additions & 35 deletions lib/mindwendel_web/channels/user_socket.ex

This file was deleted.

6 changes: 3 additions & 3 deletions lib/mindwendel_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ defmodule MindwendelWeb.Endpoint do
signing_salt: "Kxo5qCGx"
]

socket "/socket", MindwendelWeb.UserSocket,
websocket: true,
longpoll: false
# socket "/socket", MindwendelWeb.UserSocket,
# websocket: true,
# longpoll: false

socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]

Expand Down
9 changes: 6 additions & 3 deletions lib/mindwendel_web/live/brainstorming_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ defmodule MindwendelWeb.BrainstormingLive.Show do
if connected?(socket), do: Brainstormings.subscribe(id)

current_user_id = Mindwendel.Services.SessionService.get_current_user_id(session)
brainstorming = Brainstormings.get_brainstorming!(id)

brainstorming =
Brainstormings.get_brainstorming!(id)
|> Accounts.merge_brainstorming_user(current_user_id)
case get_connect_params(socket)["adminSecret"] == "" do
# TODO Check the secret if valid
false -> Accounts.add_moderating_user(brainstorming, current_user_id)
true -> Accounts.merge_brainstorming_user(brainstorming, current_user_id)
end

lanes = Lanes.get_lanes_for_brainstorming_with_labels_filtered(id)
current_user = Mindwendel.Accounts.get_user(current_user_id)
Expand Down
1 change: 1 addition & 0 deletions lib/mindwendel_web/live/live_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule MindwendelWeb.LiveHelpers do
end

def has_moderating_permission(brainstorming, current_user) do
IO.inspect Enum.member?(brainstorming.moderating_users |> Enum.map(& &1.id), current_user.id)
Enum.member?(brainstorming.moderating_users |> Enum.map(& &1.id), current_user.id)
end

Expand Down
33 changes: 33 additions & 0 deletions test/mindwendel/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Mindwendel.AccountsTest do
alias Mindwendel.Accounts
alias Mindwendel.Accounts.User
alias Mindwendel.Accounts.BrainstormingUser
alias Mindwendel.Accounts.BrainstormingModeratingUser
alias Mindwendel.Brainstormings.Brainstorming

import ExUnit.CaptureLog
Expand All @@ -12,6 +13,38 @@ defmodule Mindwendel.AccountsTest do
%{user: Factory.insert!(:user)}
end

describe "#add_moderating_user" do
test "adds a moderating user to the brainstorming", %{
brainstorming: brainstorming,
user: %User{id: user_id} = user
} do
Accounts.add_moderating_user(brainstorming, user)

assert 1 = Repo.one(from(bmu in BrainstormingModeratingUser, select: count(bmu.user_id)))
assert brainstorming_moderatoring_user = Repo.one(BrainstormingModeratingUser)
assert brainstorming_moderatoring_user.user_id == user.id
assert brainstorming_moderatoring_user.brainstorming_id == brainstorming.id

brainstorming = Repo.preload(brainstorming, :moderating_users)
assert [%User{id: ^user_id}] = brainstorming.moderating_users
end

test "responds with an error when brainstorming already contains the moderating user", %{
brainstorming: brainstorming,
user: user
} do
Accounts.add_moderating_user(brainstorming, user)

assert {:error,
%Ecto.Changeset{
valid?: false,
errors: [
brainstorming_id: {_, [{:constraint, :unique}, _]}
]
}} = Accounts.add_moderating_user(brainstorming, user)
end
end

describe "get_or_create_user" do
test "get existing user", %{user: existing_user} do
user = Accounts.get_or_create_user(existing_user.id)
Expand Down
34 changes: 1 addition & 33 deletions test/mindwendel/brainstormings_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mindwendel.BrainstormingsTest do
alias Mindwendel.Brainstormings.IdeaIdeaLabel
use Mindwendel.DataCase, async: true
alias Mindwendel.Brainstormings.BrainstormingModeratingUser
alias Mindwendel.Accounts.BrainstormingModeratingUser
alias Mindwendel.Factory

alias Mindwendel.Brainstormings
Expand Down Expand Up @@ -56,38 +56,6 @@ defmodule Mindwendel.BrainstormingsTest do
end
end

describe "#add_moderating_user" do
test "adds a moderating user to the brainstorming", %{
brainstorming: brainstorming,
user: %User{id: user_id} = user
} do
Brainstormings.add_moderating_user(brainstorming, user)

assert 1 = Repo.one(from(bmu in BrainstormingModeratingUser, select: count(bmu.user_id)))
assert brainstorming_moderatoring_user = Repo.one(BrainstormingModeratingUser)
assert brainstorming_moderatoring_user.user_id == user.id
assert brainstorming_moderatoring_user.brainstorming_id == brainstorming.id

brainstorming = Repo.preload(brainstorming, :moderating_users)
assert [%User{id: ^user_id}] = brainstorming.moderating_users
end

test "responds with an error when brainstorming already contains the moderating user", %{
brainstorming: brainstorming,
user: user
} do
Brainstormings.add_moderating_user(brainstorming, user)

assert {:error,
%Ecto.Changeset{
valid?: false,
errors: [
brainstorming_id: {_, [{:constraint, :unique}, _]}
]
}} = Brainstormings.add_moderating_user(brainstorming, user)
end
end

describe "change brainstorming" do
test "shortens the brainstorming name if it is too long", %{brainstorming: brainstorming} do
result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule MindwendelWeb.BrainstormingLive.ShowIdeaCommentTest do
})

moderating_user = Factory.insert!(:user)
Brainstormings.add_moderating_user(brainstorming, moderating_user)
Accounts.add_moderating_user(brainstorming, moderating_user)

new_user = Factory.insert!(:user)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule MindwendelWeb.BrainstormingLive.ShowIdeaDeleteTest do
brainstorming: brainstorming
} do
moderating_user = Factory.insert!(:user)
Brainstormings.add_moderating_user(brainstorming, moderating_user)
Accounts.add_moderating_user(brainstorming, moderating_user)

{:ok, show_live_view, _html} =
conn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule MindwendelWeb.BrainstormingLive.ShowIdeaEditTest do
brainstorming: brainstorming,
user: user
} do
Brainstormings.add_moderating_user(brainstorming, user)
Accounts.add_moderating_user(brainstorming, user)
{:ok, show_live_view, _html} = live(conn, ~p"/brainstormings/#{brainstorming.id}")

assert show_live_view
Expand Down Expand Up @@ -75,7 +75,7 @@ defmodule MindwendelWeb.BrainstormingLive.ShowIdeaEditTest do
idea: idea,
user: user
} do
Brainstormings.add_moderating_user(brainstorming, user)
Accounts.add_moderating_user(brainstorming, user)

{:ok, show_live_view, _html} =
live(conn, ~p"/brainstormings/#{brainstorming.id}")
Expand Down Expand Up @@ -114,7 +114,7 @@ defmodule MindwendelWeb.BrainstormingLive.ShowIdeaEditTest do
idea: idea
} do
moderatoring_user = Factory.insert!(:user)
Brainstormings.add_moderating_user(brainstorming, moderatoring_user)
Accounts.add_moderating_user(brainstorming, moderatoring_user)

{:ok, show_live_view, _html} =
conn
Expand All @@ -141,7 +141,7 @@ defmodule MindwendelWeb.BrainstormingLive.ShowIdeaEditTest do
user: %User{id: user_id}
} do
moderator_user = Factory.insert!(:user)
Brainstormings.add_moderating_user(brainstorming, moderator_user)
Accounts.add_moderating_user(brainstorming, moderator_user)

{:ok, show_live_view, _html} =
conn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule MindwendelWeb.BrainstormingLive.ShowSortByLabelTest do
setup do
moderating_user = Factory.insert!(:user)
brainstorming = Factory.insert!(:brainstorming)
Brainstormings.add_moderating_user(brainstorming, moderating_user)
Accounts.add_moderating_user(brainstorming, moderating_user)
%{brainstorming: brainstorming, moderating_user: moderating_user}
end

Expand Down
Loading

0 comments on commit 5147d19

Please sign in to comment.