Skip to content

Commit

Permalink
add IO.inspect debug to trace request for #31
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Mar 31, 2020
1 parent c654cf6 commit 2dc40e7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/app/ctx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ defmodule App.Ctx do
def upsert_sent(attrs) do
# transform attrs into Map with Atoms as Keys:
attrs = for {k, v} <- attrs, into: %{}, do: {String.to_atom(k), v}
# IO.inspect(attrs, label: "attrs")
IO.inspect(attrs, label: "attrs upsert_sent/1:141")
# Step 1: Check if the Person exists by email address:
person_id = case Map.has_key?(attrs, :email) do
true ->
Expand Down
15 changes: 10 additions & 5 deletions lib/app_web/controllers/sent_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule AppWeb.SentController do

def create(conn, params) do
attrs = Map.merge(Map.get(params, "sent"), %{"status" => "Pending"})
IO.inspect(attrs, label: "attrs create/2:19")
send_email(attrs)

conn
Expand All @@ -24,22 +25,26 @@ defmodule AppWeb.SentController do
end

def send_email(attrs) do
IO.inspect(attrs, label: "attrs send_email/1:28")
sent = Ctx.upsert_sent(attrs)
payload = Map.merge(attrs, %{"id" => sent.id})
IO.inspect(payload, label: "payload send_email/1:31")
# see: https://github.com/dwyl/elixir-invoke-lambda-example
lambda = System.get_env("AWS_LAMBDA_FUNCTION")
ExAws.Lambda.invoke(lambda, payload, "no_context")
{:ok, res} = ExAws.Lambda.invoke(lambda, payload, "no_context")
|> ExAws.request(region: System.get_env("AWS_REGION"))

sent
IO.inspect(res, label: "res send_email/1:36")
res
end

def send_email_check_auth_header(conn, params) do
case check_jwt_auth_header(conn) do
{:error, _} ->
unauthorized(conn, params)
{:ok, claims} ->
IO.inspect(claims, label: "claims send_email_check_auth_header/2:45")
claims = Map.merge(claims, %{"status" => "Pending"})

sent = send_email(claims)
data = Map.merge(claims, %{"id" => sent.id})
conn
Expand Down Expand Up @@ -136,12 +141,12 @@ defmodule AppWeb.SentController do
{:ok, _claims} ->

# warm up the lambda function so emails are sent instantly!
payload = %{"ping" => :os.system_time(:millisecond)}
payload = %{"ping" => :os.system_time(:millisecond), "key": "ping"}
# see: https://github.com/dwyl/elixir-invoke-lambda-example
lambda = System.get_env("AWS_LAMBDA_FUNCTION")
res = ExAws.Lambda.invoke(lambda, payload, "no_context")
|> ExAws.request(region: System.get_env("AWS_REGION"))

IO.inspect(res, label: "lambda response ping/2:149")
time = :os.system_time(:millisecond) - Map.get(payload, "ping")
conn
|> put_resp_header("content-type", "application/json;")
Expand Down
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ defmodule App.MixProject do

# Test Code Coverage:
{:excoveralls, "~> 0.12.2", only: :test},

]
end

Expand Down
2 changes: 1 addition & 1 deletion test/app/ctx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ defmodule App.CtxTest do
second = %{
"message_id" => "4562017092006798-f0456694-ac24-487b-9467-b79b8ce798f2",
"status" => "Sent",
"email" => "[email protected]",
"email" => "[email protected]"
}
sent2 = Ctx.upsert_sent(second)
assert sent.person_id == sent2.person_id
Expand Down
13 changes: 6 additions & 7 deletions test/app_web/controllers/sent_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ defmodule AppWeb.SentControllerTest do
describe "create sent" do
test "redirects to dashboard when data is valid", %{conn: conn} do
params = %{
"sent" => %{
"email" => "[email protected]",
"name" => "Success",
"template" => "welcome"
}
"email" => "[email protected]",
"name" => "Success",
"template" => "welcome"
}
conn = post(conn, Routes.sent_path(conn, :create), sent: params)
assert html_response(conn, 302) =~ "redirected"
Expand Down Expand Up @@ -136,17 +134,18 @@ defmodule AppWeb.SentControllerTest do
test "send an email via /api/send", %{conn: conn} do
payload = %{
"email" => "[email protected]",
"name" => "Super Successful",
"name" => "Super Successful /api/send test",
"template" => "welcome"
}
jwt = App.Token.generate_and_sign!(payload)
IO.inspect(jwt)
# IO.inspect(jwt)
conn = conn
|> put_req_header("authorization", "#{jwt}")
|> post("/api/send")

assert conn.status == 200
json = Jason.decode!(conn.resp_body)
IO.inspect(json, label: "json test:150")
assert Map.get(json, "id") > 0
assert Map.get(json, "email") == Map.get(payload, "email")

Expand Down

0 comments on commit 2dc40e7

Please sign in to comment.