Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: normalize profile mentions #361

Merged
merged 7 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -16,7 +16,7 @@
alias Safira.Repo
alias Safira.Roulette.AttendeePrize
alias Safira.Roulette.Prize
alias Safira.Store

Check warning on line 19 in lib/safira/roulette/roulette.ex

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

unused alias Store

Check warning on line 19 in lib/safira/roulette/roulette.ex

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

unused alias Store

@roulette_cost Application.compile_env!(:safira, :roulette_cost)
@roulette_tokens_min Application.compile_env!(:safira, :roulette_tokens_min)
Expand Down Expand Up @@ -501,7 +501,7 @@
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
Loading