Skip to content

Commit

Permalink
feat: spotlight badge cron-job
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm committed Feb 5, 2024
1 parent 7180d16 commit a630690
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/safira/contest/contest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ 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)
end

Redeem.changeset(%Redeem{}, attrs, user_type)
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
40 changes: 40 additions & 0 deletions lib/safira/jobs/spotlight_badge.ex
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
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

0 comments on commit a630690

Please sign in to comment.