From 981593f0359abb4fb890f0d58e96e7231725882a Mon Sep 17 00:00:00 2001 From: Rui Lopes Date: Mon, 5 Feb 2024 18:59:54 +0000 Subject: [PATCH] feat: gambling addiction cron-job --- lib/safira/jobs/gambling_addiction.ex | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/safira/jobs/gambling_addiction.ex diff --git a/lib/safira/jobs/gambling_addiction.ex b/lib/safira/jobs/gambling_addiction.ex new file mode 100644 index 00000000..5aa203ef --- /dev/null +++ b/lib/safira/jobs/gambling_addiction.ex @@ -0,0 +1,42 @@ +defmodule Safira.Jobs.GamblingAddiction do + @moduledoc """ + Job to gift the gambling addiction badge to all eligible attendees. + Eligible attendees are those that spun the wheel and played at the slots at least once. + + Receives the badge_id. + """ + import Ecto.Query, warn: false + + alias Safira.Accounts.{Attendee, Company} + alias Safira.Contest + alias Safira.Contest.{Badge, Redeem} + alias Safira.Repo + alias Safira.Roulette.AttendeePrize + alias Safira.Slots.AttendeePayout + + @spec run(integer()) :: :ok + def run(badge_id) do + list_eligible_attendees() + |> Enum.each(&create_redeem(&1.id, badge_id)) + end + + defp list_eligible_attendees do + Attendee + |> join(:inner, [a], ap in AttendeePrize, on: a.id == ap.attendee_id) + |> join(:inner, [a, ap], sp in AttendeePayout, on: a.id == sp.attendee_id) + |> where([a, ap, sp], not is_nil(ap.id) and not is_nil(sp.id)) + |> select([a], a) + |> Repo.all() + end + + defp create_redeem(attendee_id, badge_id) do + Contest.create_redeem( + %{ + attendee_id: attendee_id, + staff_id: 1, + badge_id: badge_id + }, + :admin + ) + end +end