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: spotlight badge cron-job #392

Merged
merged 5 commits into from
Feb 5, 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
9 changes: 8 additions & 1 deletion lib/safira/contest/contest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ defmodule Safira.Contest do

def create_redeem(attrs \\ %{}, user_type \\ :staff) do
Multi.new()
|> Multi.insert(:redeem, Redeem.changeset(%Redeem{}, attrs, user_type))
|> Multi.insert(:redeem, fn _ ->
if Interaction.is_badge_spotlighted(attrs["badge_id"]) do
attrs = Map.put(attrs, "spotlighted", true)
Redeem.changeset(%Redeem{}, attrs, user_type)
else
Redeem.changeset(%Redeem{}, attrs, user_type)
end
end)
|> Multi.update(:attendee, fn %{redeem: redeem} ->
redeem = Repo.preload(redeem, [:badge, :attendee])

Expand Down
4 changes: 3 additions & 1 deletion lib/safira/contest/redeem.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Safira.Contest.Redeem do
alias Safira.Contest.Badge

schema "redeems" do
field(:spotlighted, :boolean, default: false)

belongs_to(:attendee, Attendee, foreign_key: :attendee_id, type: :binary_id)
belongs_to(:staff, Staff)
belongs_to(:badge, Badge)
Expand All @@ -24,7 +26,7 @@ defmodule Safira.Contest.Redeem do
@doc false
def changeset(redeem, attrs, user_type \\ :staff) do
redeem
|> cast(attrs, [:attendee_id, :staff_id, :badge_id])
|> cast(attrs, [:spotlighted, :attendee_id, :staff_id, :badge_id])
|> foreign_key_constraint(:attendee_id)
|> foreign_key_constraint(:staff_id)
|> validate_required([:attendee_id, :badge_id])
Expand Down
39 changes: 39 additions & 0 deletions lib/safira/jobs/spotlight_badge.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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.Contest
alias Safira.Contest.{Badge, Redeem}
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
Redeem
|> where([r], r.spotlighted == true and r.badge_id != ^badge_id)
|> select([r], r.attendee_id)
|> Repo.all()
end

defp create_redeem(attendee_id, badge) do
Contest.create_redeem(
%{
attendee_id: attendee_id,
staff_id: 1,
badge_id: badge.id
},
:admin
)
end
end
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
Loading