From 038ea4e8bce9102134c71e7cedf79cf377a9d7fc Mon Sep 17 00:00:00 2001 From: Axel Clark Date: Fri, 5 Apr 2024 22:48:43 -0700 Subject: [PATCH] Add chat message for in season picks * Add chat message for in season picks * See #1299 --- lib/ex338/chats/chat.ex | 1 + lib/ex338/fantasy_leagues.ex | 17 +++++++- lib/ex338/in_season_draft_picks.ex | 29 +++++++++++++ test/ex338/fantasy_leagues_test.exs | 41 +++++++++++++++++++ test/ex338/in_season_draft_picks_test.exs | 37 +++++++++++++++++ .../live/championship_live/show_test.exs | 37 +++++++++++++++++ 6 files changed, 161 insertions(+), 1 deletion(-) diff --git a/lib/ex338/chats/chat.ex b/lib/ex338/chats/chat.ex index 3c23a28b..9bc04ea3 100644 --- a/lib/ex338/chats/chat.ex +++ b/lib/ex338/chats/chat.ex @@ -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 diff --git a/lib/ex338/fantasy_leagues.ex b/lib/ex338/fantasy_leagues.ex index 17710798..81a9a847 100644 --- a/lib/ex338/fantasy_leagues.ex +++ b/lib/ex338/fantasy_leagues.ex @@ -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 @@ -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: @@ -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 diff --git a/lib/ex338/in_season_draft_picks.ex b/lib/ex338/in_season_draft_picks.ex index 2ec6436f..d0727262 100644 --- a/lib/ex338/in_season_draft_picks.ex +++ b/lib/ex338/in_season_draft_picks.ex @@ -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 @@ -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 @@ -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 diff --git a/test/ex338/fantasy_leagues_test.exs b/test/ex338/fantasy_leagues_test.exs index 7c3a3684..d13550c3 100644 --- a/test/ex338/fantasy_leagues_test.exs +++ b/test/ex338/fantasy_leagues_test.exs @@ -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 diff --git a/test/ex338/in_season_draft_picks_test.exs b/test/ex338/in_season_draft_picks_test.exs index 65442581..5f6c8d74 100644 --- a/test/ex338/in_season_draft_picks_test.exs +++ b/test/ex338/in_season_draft_picks_test.exs @@ -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) diff --git a/test/ex338_web/live/championship_live/show_test.exs b/test/ex338_web/live/championship_live/show_test.exs index 96b6a103..58be63d5 100644 --- a/test/ex338_web/live/championship_live/show_test.exs +++ b/test/ex338_web/live/championship_live/show_test.exs @@ -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}") @@ -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