Skip to content

Commit

Permalink
fix: list_company_attendees function (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 authored Feb 5, 2024
1 parent 18f2149 commit 8c19904
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
32 changes: 19 additions & 13 deletions lib/safira/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,27 @@ 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(Attendee)
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
10 changes: 9 additions & 1 deletion test/safira_web/controllers/cv_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ defmodule SafiraWeb.CVControllerTest do

user_company = create_user_strategy(:user)
user_company_no_access = create_user_strategy(:user)
company = insert(:company, user: user_company, badge: badge, has_cv_access: true)

company =
insert(:company,
user: user_company,
badge: badge,
has_cv_access: true,
sponsorship: "Bronze"
)

company_no_access = insert(:company, user: user_company_no_access, has_cv_access: false)

user_attendee_with_badge = create_user_strategy(:user)
Expand Down

0 comments on commit 8c19904

Please sign in to comment.