Skip to content

Commit

Permalink
fix: make final draw filter by minimum badge count (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruioliveira02 authored Feb 14, 2025
1 parent 6699aa9 commit 074071f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 14 additions & 0 deletions lib/safira/contest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,20 @@ defmodule Safira.Contest do
|> Repo.aggregate(:count, :id)
end

def list_final_draw do
br_query =
BadgeRedeem
|> group_by([br], br.attendee_id)
|> having([br], count(br.id) >= 10)
|> select([br, at, u], %{attendee_id: br.attendee_id, count: count(br.id)})

Attendee
|> join(:inner, [at], br in subquery(br_query), on: br.attendee_id == at.id)
|> where([at, br], not at.ineligible)
|> preload(:user)
|> Repo.all()
end

@doc """
Gets the count of badges for a category.
Expand Down
13 changes: 5 additions & 8 deletions lib/safira_web/controllers/download_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule SafiraWeb.DownloadController do

alias Safira.Accounts
alias Safira.Companies
alias Safira.Contest

def generate_credentials(conn, %{"count" => count}) do
{count_int, ""} = Integer.parse(count)
Expand Down Expand Up @@ -59,7 +60,7 @@ defmodule SafiraWeb.DownloadController do
|> put_resp_header("content-disposition", "attachment; filename=\"final_draw.csv\"")
|> send_chunked(200)

Accounts.list_attendees()
Contest.list_final_draw()
|> Stream.flat_map(&final_draw_lines/1)
|> Stream.map(&CSV.dump_to_iodata(&1))
|> Enum.reduce(conn, fn chunk, conn ->
Expand Down Expand Up @@ -169,13 +170,9 @@ defmodule SafiraWeb.DownloadController do
|> to_string()
end

defp final_draw_lines(user) do
if user.attendee.entries < 10 or user.attendee.ineligible do
[]
else
for _ <- 1..user.attendee.entries do
[[user.id, user.name, user.handle]]
end
defp final_draw_lines(attendee) do
for _ <- 1..attendee.entries do
[[attendee.user.id, attendee.user.name, attendee.user.handle]]
end
end

Expand Down

0 comments on commit 074071f

Please sign in to comment.