Skip to content

Commit

Permalink
refactor: simplify auth controller (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm authored Jan 30, 2024
1 parent 25a4fe2 commit 3cccaa8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 80 deletions.
49 changes: 15 additions & 34 deletions lib/safira_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule SafiraWeb.AuthController do
alias Safira.Accounts
alias Safira.Auth
alias Safira.Guardian
alias Safira.Roulette
alias Safira.Store

action_fallback SafiraWeb.FallbackController

Expand All @@ -20,51 +22,30 @@ defmodule SafiraWeb.AuthController do
user = Guardian.Plug.current_resource(conn)
user_preload = Accounts.get_user_preload!(user.id)

user =
user_preload =
cond do
not is_nil(user_preload.attendee) ->
user
|> Map.put(:id, user_preload.id)
attendee = Accounts.get_attendee_with_badge_count_by_id!(user_preload.attendee.id)

attendee =
attendee
|> Map.put(:redeemables, Store.get_attendee_redeemables(attendee))
|> Map.put(:prizes, Roulette.get_attendee_prize(attendee))

user_preload
|> Map.put(:attendee, attendee)
|> Map.put(:type, "attendee")

not is_nil(user_preload.company) ->
user
|> Map.put(:id, user_preload.id)
user_preload
|> Map.put(:type, "company")

not is_nil(user_preload.staff) ->
user
|> Map.put(:id, user_preload.id)
user_preload
|> Map.put(:type, "staff")
end

render(conn, :data, user: user)
end

def attendee(conn, _params) do
user = Guardian.Plug.current_resource(conn)
user_preload = Accounts.get_user_preload!(user.id)

case is_nil(user_preload.attendee) do
true ->
{:error, :unauthorized}

false ->
render(conn, :attendee, user: user_preload)
end
end

def company(conn, _params) do
user = Guardian.Plug.current_resource(conn)
user_preload = Accounts.get_user_preload!(user.id)

case is_nil(user_preload.company) do
true ->
{:error, :unauthorized}

false ->
render(conn, :company, user: user_preload)
end
render(conn, :data, user: user_preload)
end

def is_registered(conn, %{"id" => id}) do
Expand Down
53 changes: 24 additions & 29 deletions lib/safira_web/controllers/auth_json.ex
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
defmodule SafiraWeb.AuthJSON do
@moduledoc false

alias Safira.Avatar
alias SafiraWeb.AttendeeView
alias SafiraWeb.CompanyView

def data(%{user: user}) do
%{
id: user.id,
email: user.email,
type: user.type
}
def signup_response(%{
jwt: jwt,
discord_association_code: discord_association_code
}) do
%{jwt: jwt, discord_association_code: discord_association_code}
end

def is_registered(%{is_registered: value}) do
%{is_registered: value}
def data(%{user: %{type: "attendee", attendee: attendee}} = user) do
user_data(user)
|> Map.merge(AttendeeView.render("attendee.json", attendee: attendee))
end

def attendee(%{user: user}) do
%{
id: user.attendee.id,
nickname: user.attendee.nickname,
name: user.attendee.name,
avatar: Avatar.url({user.attendee.avatar, user.attendee}, :original),
email: user.email
}
def data(%{user: %{type: "company", company: company}} = user) do
user_data(user)
|> Map.merge(CompanyView.render("company.json", company: company))
end

def company(%{user: user}) do
def data(%{user: %{type: "staff"}} = user) do
user_data(user)
end

defp user_data(%{user: user}) do
%{
id: user.company.id,
name: user.company.name,
id: user.id,
email: user.email,
sponsorship: user.company.sponsorship,
badge_id: user.company.badge_id
type: user.type
}
end

def jwt(%{jwt: jwt}) do
%{jwt: jwt}
def is_registered(%{is_registered: value}) do
%{is_registered: value}
end

def signup_response(%{
jwt: jwt,
discord_association_code: discord_association_code
}) do
%{jwt: jwt, discord_association_code: discord_association_code}
def jwt(%{jwt: jwt}) do
%{jwt: jwt}
end
end
2 changes: 0 additions & 2 deletions lib/safira_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ defmodule SafiraWeb.Router do
pipe_through :jwt_authenticated

get "/user", AuthController, :user
get "/attendee", AuthController, :attendee
get "/company", AuthController, :company
get "/leaderboard", LeaderboardController, :index
get "/leaderboard/:date", LeaderboardController, :daily
get "/roulette/latestwins", RouletteController, :latest_wins
Expand Down
42 changes: 27 additions & 15 deletions test/safira_web/controllers/auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,60 @@ defmodule SafiraWeb.AuthControllerTest do
end

describe "me" do
test "attendee", %{user: user} do
test "when user is an attendee", %{user: user} do
attendee = insert(:attendee, user: user)

%{conn: conn, user: _user} = api_authenticate(user)

conn =
conn
|> get(Routes.auth_path(conn, :attendee))
|> get(Routes.auth_path(conn, :user))

expected_attendee = %{
"avatar" => nil,
expected_user = %{
"email" => user.email,
"id" => attendee.id,
"type" => "attendee",
"avatar" => nil,
"badge_count" => 0,
"badges" => [],
"course" => nil,
"cv" => nil,
"entries" => 0,
"name" => attendee.name,
"nickname" => attendee.nickname
"nickname" => attendee.nickname,
"prizes" => [],
"redeemables" => [],
"token_balance" => 0
}

assert json_response(conn, 200) == expected_attendee
assert json_response(conn, 200) == expected_user
end

test "company", %{user: user} do
test "when user is a company", %{user: user} do
company = insert(:company, user: user)

%{conn: conn, user: _user} = api_authenticate(user)

conn =
conn
|> get(Routes.auth_path(conn, :company))
|> get(Routes.auth_path(conn, :user))

expected_company = %{
"badge_id" => company.badge.id,
expected_user = %{
"badge_id" => company.badge_id,
"channel_id" => company.channel_id,
"email" => user.email,
"has_cv_access" => company.has_cv_access,
"id" => company.id,
"name" => company.name,
"sponsorship" => company.sponsorship
"sponsorship" => company.sponsorship,
"type" => "company"
}

assert json_response(conn, 200) == expected_company
assert json_response(conn, 200) == expected_user
end

test "user", %{user: user} do
insert(:company, user: user)
test "when user is a staff", %{user: user} do
insert(:staff, user: user)

%{conn: conn, user: _user} = api_authenticate(user)

Expand All @@ -60,7 +72,7 @@ defmodule SafiraWeb.AuthControllerTest do
expected_user = %{
"email" => user.email,
"id" => user.id,
"type" => "company"
"type" => "staff"
}

assert json_response(conn, 200) == expected_user
Expand Down

0 comments on commit 3cccaa8

Please sign in to comment.