From 725851bc9ef86bcb43bccfd1c484c9c19f243a5a Mon Sep 17 00:00:00 2001 From: Rui Lopes <76881129+RuiL1904@users.noreply.github.com> Date: Thu, 16 Feb 2023 11:14:21 +0000 Subject: [PATCH] Fix all gold task (#329) --- ...s_with_badge.ex => gift.all_gold.badge.ex} | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) rename lib/mix/tasks/{gift.users_with_badge.ex => gift.all_gold.badge.ex} (69%) diff --git a/lib/mix/tasks/gift.users_with_badge.ex b/lib/mix/tasks/gift.all_gold.badge.ex similarity index 69% rename from lib/mix/tasks/gift.users_with_badge.ex rename to lib/mix/tasks/gift.all_gold.badge.ex index c3cea93a0..b9706da14 100644 --- a/lib/mix/tasks/gift.users_with_badge.ex +++ b/lib/mix/tasks/gift.all_gold.badge.ex @@ -1,24 +1,29 @@ -defmodule Mix.Tasks.Gift.User.Badge do +defmodule Mix.Tasks.Gift.AllGold.Badge do @moduledoc """ - Task to generate a user with a badge + Task to give a badge to a list of users which have all company gold badges and exclusive badges """ use Mix.Task import Ecto.Query, warn: false + + alias Safira.Contest alias Safira.Contest.Redeem alias Safira.Accounts.Company alias Safira.Contest.Badge alias Safira.Accounts.Attendee alias Safira.Repo - def run(args) do + def run(args) when length(args) == 1 do Mix.Task.run("app.start") - create(args) + + args |> List.first() |> String.to_integer() |> create() end - def create(badge_id) do - # give a badge to a list of users which have all company gold badges and exclusive badges + def run(_args) do + Mix.shell().error("You must provide a badge_id") + end + def create(badge_id) do companies = Badge |> where([b], b.type == 4) @@ -38,10 +43,10 @@ defmodule Mix.Tasks.Gift.User.Badge do |> select([b, c, r, a], a.id) |> Repo.all() - # TODO: Transaction + # TODO: Multi transaction attendees |> Enum.each(fn id -> - Repo.insert(%Redeem{attendee_id: id, badge_id: badge_id, manager_id: 1}) + Contest.create_redeem(%{attendee_id: id, badge_id: badge_id, manager_id: 1}, :admin) end) end end