Skip to content

Commit

Permalink
refactor: improve get current user endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm committed Jan 30, 2024
1 parent 996fef6 commit 9045d10
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
3 changes: 3 additions & 0 deletions lib/safira/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ defmodule Safira.Accounts do
def get_attendee!(id) do
Repo.get!(Attendee, id)
|> Repo.preload(:badges)
|> Repo.preload(:redeemables)
|> Repo.preload(:user)
|> Repo.preload(:prizes)
|> Repo.preload(:course)
end

def get_attendee(id) do
Repo.get(Attendee, id)
|> Repo.preload(:badges)
|> Repo.preload(:redeemables)
|> Repo.preload(:user)
|> Repo.preload(:prizes)
|> Repo.preload(:course)
Expand Down
14 changes: 6 additions & 8 deletions lib/safira_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,23 @@ 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)
user_preload
|> Map.put(:attendee, Accounts.get_attendee!(user_preload.attendee.id))
|> 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)
render(conn, :data, user: user_preload)
end

def attendee(conn, _params) 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

0 comments on commit 9045d10

Please sign in to comment.