Skip to content

Commit e0de3b3

Browse files
joaop21jpcorreia99
andauthored
Daily badge task (#213)
Co-authored-by: jpcorreia99 <jpcorreia99@gmail.com>
1 parent 4f4d779 commit e0de3b3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

lib/mix/tasks/gift.daily.badge.ex

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule Mix.Tasks.Gift.Daily.Badge do
2+
use Mix.Task
3+
4+
import Ecto.Query, warn: false
5+
6+
alias Safira.Accounts.Attendee
7+
alias Safira.Contest
8+
alias Safira.Contest.Redeem
9+
alias Safira.Contest.Badge
10+
11+
alias Safira.Repo
12+
13+
# Expects a date in yyyy-mm-dd format
14+
def run(args) do
15+
Mix.Task.run("app.start")
16+
17+
cond do
18+
length(args) != 2 ->
19+
Mix.shell().info("Needs to receive badge_id and the date of the day.")
20+
21+
true ->
22+
args |> create()
23+
end
24+
end
25+
26+
def create(args) do
27+
28+
{badge_id, date} = {args |> Enum.at(0), args |> Enum.at(1) |> Date.from_iso8601!()}
29+
30+
Repo.all(
31+
from a in Attendee,
32+
join: r in Redeem, on: a.id == r.attendee_id,
33+
join: b in Badge, on: r.badge_id == b.id,
34+
where: not(is_nil a.user_id) and fragment("?::date", r.inserted_at) == ^date,
35+
preload: [badges: b]
36+
)
37+
|> Enum.map(fn a -> Map.put(a, :badge_count, length(a.badges)) end)
38+
|> Enum.filter(fn a -> a.badge_count > 0 end)
39+
|> Enum.each(fn a ->
40+
Contest.create_redeem(%{
41+
attendee_id: a.id,
42+
manager_id: 1,
43+
badge_id: badge_id
44+
})
45+
end)
46+
47+
end
48+
49+
end

0 commit comments

Comments
 (0)