Skip to content

Commit

Permalink
feat: counts for day field in badge table (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm authored Feb 4, 2024
1 parent 47a4c96 commit 4f900d0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/mix/tasks/gen.badges.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions lib/safira/contest/badge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -45,7 +47,8 @@ defmodule Safira.Contest.Badge do
:begin_badge,
:end_badge,
:type,
:tokens
:tokens,
:counts_for_day
])
|> cast_attachments(attrs, [:avatar])
|> validate_required([
Expand All @@ -56,7 +59,8 @@ defmodule Safira.Contest.Badge do
:begin_badge,
:end_badge,
:type,
:tokens
:tokens,
:counts_for_day
])
|> validate_length(:name, min: 1, max: 255)
|> validate_length(:description, min: 1, max: 1000)
Expand All @@ -74,7 +78,8 @@ defmodule Safira.Contest.Badge do
:name,
:description,
:type,
:tokens
:tokens,
:counts_for_day
])
|> cast_attachments(attrs, [:avatar])
|> validate_required([
Expand All @@ -85,7 +90,8 @@ defmodule Safira.Contest.Badge do
:name,
:description,
:type,
:tokens
:tokens,
:counts_for_day
])
|> validate_length(:name, min: 1, max: 255)
|> validate_length(:description, min: 1, max: 1000)
Expand Down
3 changes: 2 additions & 1 deletion lib/safira/contest/contest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4f900d0

Please sign in to comment.