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

fix: list_company_attendees function #382

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 24 additions & 13 deletions lib/safira/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,32 @@ defmodule Safira.Accounts do
alias Safira.Contest.Redeem

@doc """
Returns the list of attendees that have a badge from the given company
Returns the list of attendees for a company based on the company's sponsorship level.
"""
def list_company_attendees(company_id) do
badge_id =
company_id
|> get_company!()
|> then(fn x -> x.badge_id end)

Repo.all(
from r in Redeem,
where: r.badge_id == ^badge_id,
join: a in assoc(r, :attendee),
preload: [attendee: a]
)
|> Enum.map(fn x -> x.attendee end)
company = get_company!(company_id)

if company.sponsorship in ["Bronze", "Silver"] do
badge_id =
company_id
|> get_company!()
|> then(fn x -> x.badge_id end)

Repo.all(
from r in Redeem,
where: r.badge_id == ^badge_id,
join: a in assoc(r, :attendee),
preload: [attendee: a]
)
|> Enum.map(fn x -> x.attendee end)
else
Repo.all(
from r in Redeem,
join: a in assoc(r, :attendee),
preload: [attendee: a]
)
|> Enum.map(fn x -> x.attendee end)
MarioRodrigues10 marked this conversation as resolved.
Show resolved Hide resolved
end
end

def is_company(conn) do
Expand Down
6 changes: 3 additions & 3 deletions test/safira/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ defmodule Safira.AccountsTest do
assert Accounts.list_company_attendees(company.id) == [attendee1, attendee2]
end

test "attendee redeemed another company's badge" do
company = insert(:company)
test "attendee redeemed another Bronze company's badge" do
company = insert(:company, sponsorship: "Bronze")
attendee = insert(:attendee)

insert(:redeem, attendee: attendee)
Expand All @@ -526,7 +526,7 @@ defmodule Safira.AccountsTest do
end

test "one attendee redeemed and one not" do
company = insert(:company)
company = insert(:company, sponsorship: "Bronze")
[attendee1, attendee2] = insert_pair(:attendee)

insert(:redeem, attendee: attendee1, badge: company.badge)
Expand Down
2 changes: 1 addition & 1 deletion test/safira_web/controllers/company_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule SafiraWeb.CompanyControllerTest do
setup %{conn: conn} do
user = create_user_strategy(:user)
badge = insert(:badge)
company = insert(:company, user: user, badge: badge)
company = insert(:company, user: user, badge: badge, sponsorship: "Bronze")

{:ok, conn: put_req_header(conn, "accept", "application/json"), user: user, company: company}
end
Expand Down
Loading