mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Send email to opponent when new game is created
This commit is contained in:
parent
4fa3babaa0
commit
2bf71e882f
@ -33,7 +33,9 @@ config :logger, :console, format: "[$level] $message\n"
|
|||||||
# in production as building large stacktraces may be expensive.
|
# in production as building large stacktraces may be expensive.
|
||||||
config :phoenix, :stacktrace_depth, 20
|
config :phoenix, :stacktrace_depth, 20
|
||||||
|
|
||||||
# Configure your database
|
config :chess, Chess.Mailer,
|
||||||
|
adapter: Bamboo.LocalAdapter
|
||||||
|
|
||||||
config :chess, Chess.Repo,
|
config :chess, Chess.Repo,
|
||||||
adapter: Ecto.Adapters.Postgres,
|
adapter: Ecto.Adapters.Postgres,
|
||||||
database: "chess_dev",
|
database: "chess_dev",
|
||||||
|
|||||||
@ -11,6 +11,9 @@ config :chess, :sql_sandbox, true
|
|||||||
# Print only warnings and errors during test
|
# Print only warnings and errors during test
|
||||||
config :logger, level: :warn
|
config :logger, level: :warn
|
||||||
|
|
||||||
|
config :chess, Chess.Mailer,
|
||||||
|
adapter: Bamboo.TestAdapter
|
||||||
|
|
||||||
# Configure your database
|
# Configure your database
|
||||||
config :chess, Chess.Repo,
|
config :chess, Chess.Repo,
|
||||||
adapter: Ecto.Adapters.Postgres,
|
adapter: Ecto.Adapters.Postgres,
|
||||||
|
|||||||
17
lib/chess/emails.ex
Normal file
17
lib/chess/emails.ex
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
defmodule Chess.Emails do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Bamboo.Email
|
||||||
|
|
||||||
|
def new_game_email(conn, game) do
|
||||||
|
new_email()
|
||||||
|
|> to(game.opponent)
|
||||||
|
|> from("games@64squares.club")
|
||||||
|
|> subject("New chess game created!")
|
||||||
|
|> text_body("""
|
||||||
|
#{game.user.name} has invited you to play a game of chess.
|
||||||
|
|
||||||
|
#{ChessWeb.Router.Helpers.game_url(conn, :show, game)}
|
||||||
|
""")
|
||||||
|
end
|
||||||
|
end
|
||||||
3
lib/chess/mailer.ex
Normal file
3
lib/chess/mailer.ex
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
defmodule Chess.Mailer do
|
||||||
|
use Bamboo.Mailer, otp_app: :chess
|
||||||
|
end
|
||||||
@ -3,6 +3,7 @@ defmodule ChessWeb.GameController do
|
|||||||
|
|
||||||
alias Chess.Store.Game
|
alias Chess.Store.Game
|
||||||
alias Chess.Store.User
|
alias Chess.Store.User
|
||||||
|
alias Chess.Mailer
|
||||||
|
|
||||||
import Chess.Auth, only: [current_user: 1]
|
import Chess.Auth, only: [current_user: 1]
|
||||||
|
|
||||||
@ -40,6 +41,14 @@ defmodule ChessWeb.GameController do
|
|||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, game} ->
|
{:ok, game} ->
|
||||||
|
conn
|
||||||
|
|> Chess.Emails.new_game_email(
|
||||||
|
game
|
||||||
|
|> Repo.preload(:user)
|
||||||
|
|> Repo.preload(:opponent)
|
||||||
|
)
|
||||||
|
|> Mailer.deliver_later
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Game created successfully.")
|
|> put_flash(:info, "Game created successfully.")
|
||||||
|> redirect(to: game_path(conn, :show, game))
|
|> redirect(to: game_path(conn, :show, game))
|
||||||
|
|||||||
5
lib/chess_web/formatters/user_formatter.ex
Normal file
5
lib/chess_web/formatters/user_formatter.ex
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
defimpl Bamboo.Formatter, for: Chess.Store.User do
|
||||||
|
def format_email_address(user, _opts) do
|
||||||
|
{user.name, user.email}
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -53,6 +53,10 @@ defmodule ChessWeb.Router do
|
|||||||
resources "/games", ChessWeb.Api.GameController, only: [:show, :update]
|
resources "/games", ChessWeb.Api.GameController, only: [:show, :update]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if Mix.env == :dev do
|
||||||
|
forward "/sent_emails", Bamboo.SentEmailViewerPlug
|
||||||
|
end
|
||||||
|
|
||||||
defp put_user_token(conn, _) do
|
defp put_user_token(conn, _) do
|
||||||
if current_user = Guardian.Plug.current_resource(conn) do
|
if current_user = Guardian.Plug.current_resource(conn) do
|
||||||
token = Token.sign(conn, "game socket", current_user.id)
|
token = Token.sign(conn, "game socket", current_user.id)
|
||||||
|
|||||||
@ -1,12 +1,13 @@
|
|||||||
defmodule ChessWeb.GameControllerTest do
|
defmodule ChessWeb.GameControllerTest do
|
||||||
use ChessWeb.ConnCase
|
use ChessWeb.ConnCase
|
||||||
|
use Bamboo.Test
|
||||||
|
|
||||||
alias Chess.Store.Game
|
alias Chess.Store.Game
|
||||||
alias Chess.Auth.Guardian
|
alias Chess.Auth.Guardian
|
||||||
|
|
||||||
import Chess.Factory
|
import Chess.Factory
|
||||||
|
|
||||||
test "lists all entries on index", %{conn: conn} do
|
test "lists all games on index", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
@ -17,7 +18,7 @@ defmodule ChessWeb.GameControllerTest do
|
|||||||
assert html_response(conn, 200) =~ "Listing games"
|
assert html_response(conn, 200) =~ "Listing games"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "creates resource and redirects when data is valid", %{conn: conn} do
|
test "creates game and redirects when data is valid", %{conn: conn} do
|
||||||
opponent = insert(:user, %{email: "daruk@goron.city"})
|
opponent = insert(:user, %{email: "daruk@goron.city"})
|
||||||
attrs = %{"opponent_id" => opponent.id}
|
attrs = %{"opponent_id" => opponent.id}
|
||||||
|
|
||||||
@ -33,6 +34,21 @@ defmodule ChessWeb.GameControllerTest do
|
|||||||
assert redirected_to(conn) == game_path(conn, :show, game)
|
assert redirected_to(conn) == game_path(conn, :show, game)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "sends and email when game is created", %{conn: conn} do
|
||||||
|
opponent = insert(:user, %{name: "Daruk", email: "daruk@goron.city"})
|
||||||
|
attrs = %{"opponent_id" => opponent.id}
|
||||||
|
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> login(user)
|
||||||
|
|> post(game_path(conn, :create), game: attrs)
|
||||||
|
|
||||||
|
assert_email_delivered_with(
|
||||||
|
to: [{opponent.name, opponent.email}]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
test "shows chosen game", %{conn: conn} do
|
test "shows chosen game", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
opponent = insert(:user, %{email: "revali@rito.village"})
|
opponent = insert(:user, %{email: "revali@rito.village"})
|
||||||
@ -71,7 +87,7 @@ defmodule ChessWeb.GameControllerTest do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "deletes chosen resource", %{conn: conn} do
|
test "deletes game", %{conn: conn} do
|
||||||
game = Repo.insert! %Game{}
|
game = Repo.insert! %Game{}
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
conn = login(conn, user)
|
conn = login(conn, user)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user