Skip to content

Commit

Permalink
feat: normalize profile mentions (#361)
Browse files Browse the repository at this point in the history
* feat: normalize profile mentions

* fix: formatting and linting

* chore: implement suggestions

* fix: tests

* fix: remove duplicated function

* fix: drop ! from get_attendee_with_badge_count_by_username function

* fix: function
  • Loading branch information
MarioRodrigues10 authored Jan 16, 2024
1 parent 709f30e commit 2d60e44
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
29 changes: 25 additions & 4 deletions lib/safira/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ defmodule Safira.Accounts do
|> Repo.preload(:prizes)
end

def get_attendee_with_badge_count!(id) do
def get_attendee(id) do
Repo.get(Attendee, id)
|> Repo.preload(:badges)
|> Repo.preload(:user)
|> Repo.preload(:prizes)
|> Repo.preload(:course)
end

def get_attendee_with_badge_count_by_id!(id) do
case get_attendee(id) do
nil ->
nil
Expand All @@ -102,12 +110,25 @@ defmodule Safira.Accounts do
end
end

def get_attendee(id) do
Repo.get(Attendee, id)
def get_attendee_with_badge_count_by_username(username) do
case get_attendee_by_username!(username) do
%Attendee{} = attendee ->
badges =
attendee.badges
|> Enum.filter(&(&1.type != 0))

Map.put(attendee, :badge_count, length(badges))

_ ->
nil
end
end

def get_attendee_by_username!(username) do
Repo.get_by!(Attendee, nickname: username)
|> Repo.preload(:badges)
|> Repo.preload(:user)
|> Repo.preload(:prizes)
|> Repo.preload(:course)
end

def get_attendee_by_discord_association_code(discord_association_code) do
Expand Down
2 changes: 1 addition & 1 deletion lib/safira/roulette/roulette.ex
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ defmodule Safira.Roulette do
preload: [prize: p, attendee: a]

Repo.all(query)
|> Enum.map(fn ap -> {ap.attendee.name, ap.prize, ap.updated_at} end)
|> Enum.map(fn ap -> {ap.attendee.name, ap.attendee.nickname, ap.prize, ap.updated_at} end)
end

def get_attendee_not_redeemed(attendee) do
Expand Down
29 changes: 27 additions & 2 deletions lib/safira_web/controllers/attendee_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,33 @@ defmodule SafiraWeb.AttendeeController do
render(conn, "index.json", attendees: attendees)
end

def show(conn, %{"id" => id}) do
attendee = Accounts.get_attendee_with_badge_count!(id)
def show(conn, _) when is_map_key(conn.query_params, "id") do
id = conn.query_params["id"]
attendee = Accounts.get_attendee_with_badge_count_by_id!(id)

cond do
is_nil(attendee) ->
{:error, :not_found}

is_nil(attendee.user_id) ->
{:error, :not_registered}

Accounts.is_staff(conn) ->
render(conn, "staff_show.json", attendee: attendee)

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

render(conn, "show.json", attendee: attendee)
end
end

def show(conn, _) when is_map_key(conn.query_params, "username") do
username = conn.query_params["username"]
attendee = Accounts.get_attendee_with_badge_count_by_username(username)

cond do
is_nil(attendee) ->
Expand Down
5 changes: 3 additions & 2 deletions lib/safira_web/views/roulette_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ defmodule SafiraWeb.RouletteView do
def render("latest_prize_show.json", %{roulette: latest_prize}) do
%{
attendee_name: elem(latest_prize, 0),
prize: render_one(elem(latest_prize, 1), SafiraWeb.PrizeView, "prize_show.json"),
date: elem(latest_prize, 2)
attendee_nickname: elem(latest_prize, 1),
prize: render_one(elem(latest_prize, 2), SafiraWeb.PrizeView, "prize_show.json"),
date: elem(latest_prize, 3)
}
end
end
6 changes: 3 additions & 3 deletions test/safira/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ defmodule Safira.AccountsTest do
badges = insert_list(3, :badge)
attendee = insert(:attendee, badges: badges)

attendee_with_badge_count = Accounts.get_attendee_with_badge_count!(attendee.id)
attendee_with_badge_count = Accounts.get_attendee_with_badge_count_by_id!(attendee.id)

assert attendee_with_badge_count.id == attendee.id
assert attendee_with_badge_count.badge_count == 3
Expand All @@ -245,14 +245,14 @@ defmodule Safira.AccountsTest do
badges = Enum.concat(badges, [badge])
attendee = insert(:attendee, badges: badges)

attendee_with_badge_count = Accounts.get_attendee_with_badge_count!(attendee.id)
attendee_with_badge_count = Accounts.get_attendee_with_badge_count_by_id!(attendee.id)

assert attendee_with_badge_count.id == attendee.id
assert attendee_with_badge_count.badge_count == 2
end

test "attendee does not exist" do
assert Accounts.get_attendee_with_badge_count!(Ecto.UUID.generate()) |> is_nil()
assert Accounts.get_attendee_with_badge_count_by_id!(Ecto.UUID.generate()) |> is_nil()
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/safira_web/controllers/attendee_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ defmodule SafiraWeb.AttendeeControllerTest do

conn =
conn
|> get(Routes.attendee_path(conn, :show, attendee.id))
|> get(Routes.attendee_path(conn, :show, attendee.id), %{"id" => attendee.id})

expected_attendee = %{
"avatar" => nil,
Expand Down
1 change: 1 addition & 0 deletions test/safira_web/controllers/roulette_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ defmodule SafiraWeb.RouletteControllerTest do
assert json_response(conn, 200)["data"] == [
%{
"attendee_name" => attendee.name,
"attendee_nickname" => attendee.nickname,
"date" =>
String.replace(NaiveDateTime.to_string(attendee_prize.updated_at), " ", "T"),
"prize" => %{
Expand Down

0 comments on commit 2d60e44

Please sign in to comment.