Skip to content

Commit

Permalink
Add chat message for in season picks
Browse files Browse the repository at this point in the history
* Add chat message for in season picks
* See #1299
  • Loading branch information
axelclark committed Apr 6, 2024
1 parent 87623e0 commit 038ea4e
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/ex338/chats/chat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Ex338.Chats.Chat do
field :room_name, :string

has_many :messages, Message, preload_order: [asc: :inserted_at]
has_one :fantasy_league_draft, Ex338.FantasyLeagues.FantasyLeagueDraft
timestamps()
end

Expand Down
17 changes: 16 additions & 1 deletion lib/ex338/fantasy_leagues.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Ex338.FantasyLeagues do

import Ecto.Query

alias Ex338.Championships.Championship
alias Ex338.DraftPicks
alias Ex338.FantasyLeagues.FantasyLeague
alias Ex338.FantasyLeagues.FantasyLeagueDraft
Expand Down Expand Up @@ -101,7 +102,10 @@ defmodule Ex338.FantasyLeagues do
|> Repo.insert!()
end

def get_draft_by_league_and_championship(fantasy_league, championship) do
def get_draft_by_league_and_championship(
%FantasyLeague{} = fantasy_league,
%Championship{} = championship
) do
query =
from(d in FantasyLeagueDraft,
where:
Expand All @@ -111,4 +115,15 @@ defmodule Ex338.FantasyLeagues do

Repo.one(query)
end

def get_draft_with_chat_by_league_and_championship(fantasy_league_id, championship_id) do
query =
from(d in FantasyLeagueDraft,
where: d.fantasy_league_id == ^fantasy_league_id,
where: d.championship_id == ^championship_id,
where: not is_nil(d.chat_id)
)

Repo.one(query)
end
end
29 changes: 29 additions & 0 deletions lib/ex338/in_season_draft_picks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Ex338.InSeasonDraftPicks do
import Ecto.Query, only: [limit: 2]

alias Ex338.Championships
alias Ex338.Chats
alias Ex338.FantasyLeagues
alias Ex338.FantasyPlayers
alias Ex338.InSeasonDraftPicks
alias Ex338.InSeasonDraftPicks.InSeasonDraftPick
Expand Down Expand Up @@ -57,6 +59,7 @@ defmodule Ex338.InSeasonDraftPicks do
|> InSeasonDraftPicks.Admin.update(params)
|> Repo.transaction()
|> broadcast_change([:in_season_draft_pick, :draft_player])
|> tap(&maybe_create_chat_message/1)
end

def last_picks(fantasy_league_id, sports_league_id, picks) do
Expand Down Expand Up @@ -119,6 +122,32 @@ defmodule Ex338.InSeasonDraftPicks do

defp broadcast_change(error, _), do: error

defp maybe_create_chat_message({:ok, %{update_pick: draft_pick}}) do
fantasy_league_draft =
FantasyLeagues.get_draft_with_chat_by_league_and_championship(
draft_pick.fantasy_league_id,
draft_pick.championship_id
)

# had to reload because otherwise the drafted player is nil and doesn't get preloaded
if fantasy_league_draft do
draft_pick =
draft_pick
|> Repo.reload!()
|> Repo.preload([[draft_pick_asset: :fantasy_team], :drafted_player])

message_params = %{
chat_id: fantasy_league_draft.chat_id,
content:
"#{draft_pick.draft_pick_asset.fantasy_team.team_name} drafted #{draft_pick.drafted_player.player_name} with pick ##{draft_pick.position}"
}

Chats.create_message(message_params)
end
end

defp maybe_create_chat_message(_result), do: nil

## schedule_autodraft

defp calculate_scheduled_at(championship) do
Expand Down
41 changes: 41 additions & 0 deletions test/ex338/fantasy_leagues_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,45 @@ defmodule Ex338.FantasyLeaguesTest do
end
end
end

describe "get_draft_with_chat_by_league_and_championship/2" do
test "returns draft for championship and league when a chat exists" do
chat = insert(:chat)
championship = insert(:championship)
fantasy_league = insert(:fantasy_league)

insert(:fantasy_league_draft,
chat: chat,
championship: championship,
fantasy_league: fantasy_league
)

result =
FantasyLeagues.get_draft_with_chat_by_league_and_championship(
fantasy_league.id,
championship.id
)

assert result.chat_id == chat.id
end

test "returns nil for championship and league when a chat doesn't exist" do
championship = insert(:championship)
fantasy_league = insert(:fantasy_league)
other_fantasy_league = insert(:fantasy_league)

insert(:fantasy_league_draft,
championship: championship,
fantasy_league: other_fantasy_league
)

result =
FantasyLeagues.get_draft_with_chat_by_league_and_championship(
fantasy_league.id,
championship.id
)

assert result == nil
end
end
end
37 changes: 37 additions & 0 deletions test/ex338/in_season_draft_picks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,43 @@ defmodule Ex338.InSeasonDraftPicksTest do
assert drafted_queue.status == :drafted
end

test "creates a message when a draft chat exists" do
league = insert(:fantasy_league)
team = insert(:fantasy_team, team_name: "Brown", fantasy_league: league)

sport = insert(:sports_league, abbrev: "KD")
championship = insert(:championship, sports_league: sport)
pick_player = insert(:fantasy_player, draft_pick: true, sports_league: sport)
pick_asset = insert(:roster_position, fantasy_team: team, fantasy_player: pick_player)
player = insert(:fantasy_player, draft_pick: false, sports_league: sport)
chat = insert(:chat)

insert(:fantasy_league_draft,
fantasy_league: league,
championship: championship,
chat: chat
)

pick =
insert(
:in_season_draft_pick,
position: 1,
draft_pick_asset: pick_asset,
championship: championship,
fantasy_league: league
)

params = %{"drafted_player_id" => player.id}

{:ok, %{update_pick: updated_pick}} =
InSeasonDraftPicks.draft_player(pick, params)

message = Repo.one!(Ex338.Chats.Message)

assert updated_pick.drafted_player_id == player.id
assert message.content == "Brown drafted #{player.player_name} with pick #1"
end

test "does not update and returns errors when invalid" do
league = insert(:fantasy_league)
team = insert(:fantasy_team, team_name: "Brown", fantasy_league: league)
Expand Down
37 changes: 37 additions & 0 deletions test/ex338_web/live/championship_live/show_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,29 @@ defmodule Ex338Web.ChampionshipLive.ShowTest do

another_user = insert(:user)

team_b = insert(:fantasy_team, fantasy_league: league)

pick2 =
insert(:fantasy_player, sports_league: sport, draft_pick: true, player_name: "KD Pick #2")

pick_asset2 = insert(:roster_position, fantasy_team: team_b, fantasy_player: pick2)

horse2 =
insert(:fantasy_player,
sports_league: sport,
draft_pick: false,
player_name: "Another Horse"
)

in_season_draft_pick =
insert(
:in_season_draft_pick,
draft_pick_asset: pick_asset2,
championship: championship,
fantasy_league: league,
position: 2
)

{:ok, view, _html} =
live(conn, ~p"/fantasy_leagues/#{league.id}/championships/#{championship.id}")

Expand Down Expand Up @@ -327,6 +350,20 @@ defmodule Ex338Web.ChampionshipLive.ShowTest do
render(view)

assert has_element?(view, "p", "Wow")

InSeasonDraftPicks.draft_player(in_season_draft_pick, %{
"drafted_player_id" => horse2.id
})

assert render(view) =~ horse2.player_name

draft_flash = "#{team_b.team_name} selected #{horse2.player_name}!"
assert has_element?(view, "div", draft_flash)

draft_chat_message =
"#{team_b.team_name} drafted #{horse2.player_name} with pick ##{in_season_draft_pick.position}"

assert has_element?(view, "p", draft_chat_message)
end
end
end

0 comments on commit 038ea4e

Please sign in to comment.