From 28e20faec41eddd369183e180f31aeeaa0938e22 Mon Sep 17 00:00:00 2001 From: Rui Lopes Date: Sun, 4 Feb 2024 18:32:22 +0000 Subject: [PATCH] feat: counts for day field in badge table --- lib/mix/tasks/gen.badges.ex | 6 ++++-- lib/safira/contest/badge.ex | 2 ++ lib/safira/contest/contest.ex | 3 ++- .../20240204181008_add_counts_for_day_to_badges.exs | 9 +++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 priv/repo/migrations/20240204181008_add_counts_for_day_to_badges.exs diff --git a/lib/mix/tasks/gen.badges.ex b/lib/mix/tasks/gen.badges.ex index 650e511b..9b0afa82 100644 --- a/lib/mix/tasks/gen.badges.ex +++ b/lib/mix/tasks/gen.badges.ex @@ -66,7 +66,8 @@ defmodule Mix.Tasks.Gen.Badges do end_badge, image_path, type, - tokens + tokens, + counts_for_day ] -> {:ok, begin_datetime, _} = DateTime.from_iso8601(begin_time) {:ok, end_datetime, _} = DateTime.from_iso8601(end_time) @@ -82,7 +83,8 @@ defmodule Mix.Tasks.Gen.Badges do begin_badge: begin_badge_datetime, end_badge: end_badge_datetime, type: String.to_integer(type), - tokens: String.to_integer(tokens) + tokens: String.to_integer(tokens), + counts_for_day: String.to_integer(counts_for_day) != 0 }, %{ avatar: get_avatar(image_path) diff --git a/lib/safira/contest/badge.ex b/lib/safira/contest/badge.ex index 7dc7fb2a..43f1b91b 100644 --- a/lib/safira/contest/badge.ex +++ b/lib/safira/contest/badge.ex @@ -28,6 +28,8 @@ defmodule Safira.Contest.Badge do field(:type, :integer) field(:tokens, :integer) + field(:counts_for_day, :boolean, default: true) + has_many(:referrals, Referral) many_to_many(:attendees, Attendee, join_through: Redeem) diff --git a/lib/safira/contest/contest.ex b/lib/safira/contest/contest.ex index c3119bde..717a4893 100644 --- a/lib/safira/contest/contest.ex +++ b/lib/safira/contest/contest.ex @@ -241,7 +241,8 @@ defmodule Safira.Contest do join: t in DailyToken, on: a.id == t.attendee_id, where: - not is_nil(a.user_id) and fragment("?::date", r.inserted_at) == ^date and b.type != ^0 and + not is_nil(a.user_id) and fragment("?::date", r.inserted_at) == ^date and + b.counts_for_day and fragment("?::date", t.day) == ^date, select: %{attendee: a, token_count: t.quantity}, preload: [badges: b, daily_tokens: t] diff --git a/priv/repo/migrations/20240204181008_add_counts_for_day_to_badges.exs b/priv/repo/migrations/20240204181008_add_counts_for_day_to_badges.exs new file mode 100644 index 00000000..15f22e0e --- /dev/null +++ b/priv/repo/migrations/20240204181008_add_counts_for_day_to_badges.exs @@ -0,0 +1,9 @@ +defmodule Safira.Repo.Migrations.AddCountsForDayToBadges do + use Ecto.Migration + + def change do + alter table(:badges) do + add :counts_for_day, :boolean, default: true + end + end +end