-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Safira.Jobs.SpotlightBadge do | ||
@moduledoc """ | ||
Job to gift the Spotlight badge to all eligible attendees. | ||
Eligible attendees are those that have redeemed a spotlighted badge. | ||
Receives the badge_id to gift. | ||
""" | ||
import Ecto.Query, warn: false | ||
|
||
alias Safira.Accounts.Attendee | ||
alias Safira.Contest | ||
alias Safira.Contest.Badge | ||
alias Safira.Repo | ||
|
||
@spec run(integer()) :: :ok | ||
def run(badge_id) do | ||
badge = Repo.get(Badge, badge_id) | ||
|
||
list_eligible_attendees(badge_id) | ||
|> Enum.each(&create_redeem(&1, badge)) | ||
end | ||
|
||
defp list_eligible_attendees(badge_id) do | ||
Attendee | ||
|> join(:inner, [a], r in assoc(a, :redeems)) | ||
|> where([a, r], r.spotlighted == true and r.badge_id != ^badge_id) | ||
|> Repo.all() | ||
end | ||
|
||
defp create_redeem(attendee, badge) do | ||
Contest.create_redeem( | ||
%{ | ||
attendee_id: attendee.id, | ||
staff_id: 1, | ||
badge_id: badge.id | ||
}, | ||
:admin | ||
) | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20240205215721_add_spotlighted_to_redeems.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Safira.Repo.Migrations.AddSpotlightedToRedeems do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:redeems) do | ||
add :spotlighted, :boolean, default: false | ||
end | ||
end | ||
end |