Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

share emotion with trusted adult #63

Merged
merged 4 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/elm/src/Calendar.elm
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ showCalendar ((Calendar model) as calendar) =
span [id "calendar"]
[ button [onClick Toggle, class "pa2 ba br2 dib mb2 pointer b--black bg-white"]
[ span [class "v-mid"] [text <| formatPosix model.zone model.posix]
, img [class "w2 v-mid", src "/images/calendar.png"] []
, img [class "w2 v-mid ml2", src "/images/calendar.png"] []
]
, div [class "relative"]
[ div [ class "absolute top-0 left-0 bg-white pt3 w-100", classList [("dn", not model.open)]]
Expand Down
Binary file added assets/static/images/success-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:user_id]

config :afc, Afc.Mailer,
adapter: Bamboo.MailgunAdapter,
api_key: Map.fetch!(System.get_env(), "MAILGUN_API_KEY"),
domain: Map.fetch!(System.get_env(), "MAILGUN_DOMAIN")
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ services:
- .:/app
depends_on:
- db
env_file:
- ./.env
db:
image: postgres:10.5
55 changes: 55 additions & 0 deletions lib/afc/email.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule Afc.Email do
import Bamboo.Email

def share_emotion(emotion_log, emotion) do
from = Map.fetch!(System.get_env(), "EMAIL_FROM")
html = emotion_log_to_html(emotion_log, emotion)
txt = emotion_to_text(emotion_log, emotion)

new_email()
|> to(emotion_log.user.trusted_adult.email)
|> from(from)
|> subject("New shared emotion log")
|> html_body(html)
|> text_body(txt)
end

def emotion_log_to_html(emotion_log, emotion) do
logged_at = "#{emotion_log.inserted_at.day}-#{emotion_log.inserted_at.month}-#{emotion_log.inserted_at.year}"

"""
<strong>#{emotion_log.emotion}</strong>
<p>#{logged_at}</p>
<p>reason list:</p>
#{list_reasons(emotion)}
<p>reason text:</p>
<p>#{emotion.reason}</p>
"""
end

def list_reasons(log) do
log
|> Map.from_struct
|> Enum.filter(fn {_, v} -> v == true end)
|> Keyword.keys
|> Enum.map(fn r ->
if r == :else do
"Something else"
end

if r == :"family/home" do
"Family / Home"
end

String.capitalize(to_string r)
end)
|> Enum.join(", ")
end

def emotion_to_text(emotion_log, emotion) do
"""
#{emotion_log.emotion}
#{emotion.reason}
"""
end
end
3 changes: 2 additions & 1 deletion lib/afc/emotion/emotion_log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Afc.Emotion.EmotionLog do
schema "emotion_logs" do
field :emotion, EmotionEnum
field :emotion_id, :id
field :shared, :boolean, default: false
belongs_to :user, User

timestamps()
Expand All @@ -16,7 +17,7 @@ defmodule Afc.Emotion.EmotionLog do
@doc false
def changeset(emotion_log, attrs) do
emotion_log
|> cast(attrs, [:emotion, :emotion_id])
|> cast(attrs, [:emotion, :emotion_id, :shared])
|> validate_required([:emotion, :emotion_id])
end
end
3 changes: 3 additions & 0 deletions lib/afc/mailer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Afc.Mailer do
use Bamboo.Mailer, otp_app: :afc
end
3 changes: 2 additions & 1 deletion lib/afc/user.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
defmodule Afc.User do
use Ecto.Schema
import Ecto.Changeset
alias Afc.{TrustedAdult}

@moduledoc false

schema "users" do
field :pin, :integer
field :username, :string
field :trusted_adult_id, :id
belongs_to :trusted_adult, TrustedAdult
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix here.


timestamps()
end
Expand Down
6 changes: 4 additions & 2 deletions lib/afc_web/controllers/emotion_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ defmodule AfcWeb.EmotionController do
emotion_params =
%{emotion: emotion_str, emotion_id: captured_emotion.id}

%EmotionLog{}
log = %EmotionLog{}
|> EmotionLog.changeset(emotion_params)
|> Changeset.put_assoc(:user, conn.assigns.current_user)
|> Repo.insert!()

redirect(conn, to: emotion_path(conn, :show, "captured"))
date = Timex.format!(Timex.now(), "{0D}-{0M}-{YYYY}")
render conn, "captured.html", log_id: log.id, date: date
# redirect(conn, to: emotion_path(conn, :show, "captured"))

{:error, changeset} ->
assigns = [changeset: changeset, module: emotion_map.module]
Expand Down
7 changes: 6 additions & 1 deletion lib/afc_web/controllers/log_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ defmodule AfcWeb.LogController do
else
Timex.format!(date, "{WDfull} {D} {Mfull}")
end
render conn, "single.html", emotion_log: emotion_log, emotion: emotion, millis: selected_date * 1000, date_title: date_title
render conn, "single.html",
emotion_log: emotion_log,
emotion: emotion,
millis: selected_date * 1000,
date_title: date_title,
date: params["date"]
end
end
_ ->
Expand Down
2 changes: 1 addition & 1 deletion lib/afc_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule AfcWeb.SessionController do

def create(conn, %{"session" => %{"username" => username, "pin1" => pin1, "pin2" => pin2, "pin3" => pin3, "pin4" => pin4}}) do
pin = "#{pin1}#{pin2}#{pin3}#{pin4}"

case Integer.parse(pin) do
{pin, ""} ->
case Auth.login_with_username_and_pin(conn, username, pin) do
Expand Down
25 changes: 25 additions & 0 deletions lib/afc_web/controllers/share_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule AfcWeb.ShareController do
use AfcWeb, :controller
alias Afc.{Email, Emotion, Mailer, Repo, Emotion.EmotionLog}

def create(conn, params) do
id = params["emotion_log_id"]
date = params["date"]
case log = Repo.get(EmotionLog, id) |> Repo.preload([user: :trusted_adult]) do
nil -> redirect conn, to: log_path(conn, :index, %{date: date})
_ ->
emotionSchema = log.emotion |> Emotion.get_emotion_module_name()
case emotion = Repo.get(emotionSchema, log.emotion_id) do
nil -> redirect conn, to: log_path(conn, :index, %{date: date})
_ ->
Email.share_emotion(log, emotion) |> Mailer.deliver_now
changeset = EmotionLog.changeset log, %{shared: true}
Repo.update(changeset)
redirect conn, to: log_path(conn, :index, %{date: date})
end
end
end



end
1 change: 1 addition & 0 deletions lib/afc_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ defmodule AfcWeb.Router do
get "/", PageController, :index
resources "/emotion", EmotionController, only: [:show, :create]
get "/log", LogController, :index
resources "/share", ShareController, only: [:create]
end
end
4 changes: 2 additions & 2 deletions lib/afc_web/templates/component/navbar.html.eex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="flex fixed bottom-0 h3 w-100 tc bg-white bt afc-b--pink">
<div class="flex fixed bottom-0 h3 w-100 tc bg-white bt afc-b--pink z-max">
<a class="dib w-25 pointer link pt2 black" href="/">
<p class="db mv1">Home</p>
</a>
Expand All @@ -11,5 +11,5 @@
<p class="db mv1">Help</p>
</a>

<%= link "Log out", to: session_path(@conn, :delete, @current_user), method: "delete", class: "dib w-25 pointer link pt2 black" %>
<%= link "Log out", to: session_path(@conn, :delete, @current_user), method: "delete", class: "dib w-25 pointer link pt2 black mv1" %>
</div>
9 changes: 5 additions & 4 deletions lib/afc_web/templates/component/trusted_share_btn.html.eex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="tc mt5 mb3 f4">
<button type="button" class="b ba afc-b--red ph3 pv2 br2 bg-white">Share with Trusted Adult</button>
<!-- <%= form_for @conn, "/", fn f -> %>
<%= submit("Share with Trusted Adult", class: "b ba afc-b--red ph3 pv2 br2 bg-white") %>
<% end %> -->
<%= form_for @conn, "/share", fn f -> %>
<input type="hidden" name="emotion_log_id" value="<%= @emotion_log.id %>">
<input type="hidden" name="date" value="<%= @date %>">
<%= submit("Share with Trusted Adult", class: "b ba afc-b--red ph3 pv2 br2 bg-white pointer") %>
<% end %>
</div>
12 changes: 8 additions & 4 deletions lib/afc_web/templates/emotion/captured.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
<p class="tc b f4 mv4">Well done</p>
<p class="tc mb3">Today's log has been completed.</p>
<p class="tc mb5">Do you want to share it with someone?</p>
</div>
<div class="center tc">
<p class="dib w4 ba br2 b pv3 mr3">NO</p>
<p class="dib w4 ba br2 b pv3">YES</p>
<%= form_for @conn, "/share", fn f -> %>
<input type="hidden" name="emotion_log_id" value="<%= @log_id %>">
<input type="hidden" name="date" value="<%= @date %>">
<div class="center tc">
<a href="/" class="no-underline dib w-40 ba br2 b pv3 mr3 bg-white b--black black pointer">NO</a>
<%= submit("YES", class: "dib w-40 ba br2 b pv3 bg-white b--black pointer") %>
</div>
<% end %>
</div>
12 changes: 9 additions & 3 deletions lib/afc_web/templates/log/single.html.eex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= component "day_week_month_bar" %>

<div class="w-90 bg-white center br2 pa3 mb5 mt-day-week-month">
<p id="elm-calendar" class="pa3 ba br2 dib mb4">10th Oct 2019 (placeholder)</p>
<p id="elm-calendar" class="pa3 ba br2 dib mb4"></p>
<p class="tc f3 mb3"><%= @date_title %></p>
<div class="center w-100 tc f4">
<%= emoji_img_tag(static_path(@conn, src_image(@emotion_log.emotion))) %>
Expand All @@ -16,8 +16,14 @@
</div>

</div>

<%= component "trusted_share_btn", [conn: @conn] %>
<%= if not @emotion_log.shared do %>
<%= component "trusted_share_btn", [conn: @conn, emotion_log: @emotion_log, date: @date] %>
<% else %>
<div class="tl pt3 pl3">
<img class="w2 v-mid" src="<%= static_path(@conn, "/images/success-icon.png") %> " alt="success">
<span>Shared with trusted adult</span>
</span>
<% end %>
</div>

<script type="text/javascript">
Expand Down
1 change: 1 addition & 0 deletions lib/afc_web/templates/page/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<div class="flex justify-between pt3 ph1 mb3">
<%= component "emoji_helper", [text: "Something else", page: "else", conn: @conn] %>
<div class="tc w4 ph3"></div>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Afc.Mixfile do
# Type `mix help compile.app` for more information.
def application do
[
mod: {Afc.Application, []},
mod: {Afc.Application, [:bamboo]},
extra_applications: [:logger, :runtime_tools]
]
end
Expand All @@ -35,6 +35,7 @@ defmodule Afc.Mixfile do
# Type `mix help deps` for examples and options.
defp deps do
[
{:bamboo, "~> 1.1"},
{:phoenix, "~> 1.3.4"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.2"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
"autoform": {:git, "https:/dwyl/autoform.git", "0bdde12f2405b62f245b055f81ba5a9fc0c76103", [tag: "0.2.1"]},
"bamboo": {:hex, :bamboo, "1.1.0", "ecbdc851d0127d369957e5ca7bcfb4c7fe7dbfaf4fa0b2e5cc2493dd8a9a600e", [:mix], [{:hackney, ">= 1.13.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, ">= 1.5.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm"},
Expand Down
5 changes: 3 additions & 2 deletions priv/repo/create_dummy_user.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ emotions = [
{Worried, "worried"}
]

trusted_adult_email = Map.fetch!(System.get_env(), "EMAIL_TRUSTED_ADULT")
adult =
case Repo.get_by(TrustedAdult, email: "[email protected]") do
case Repo.get_by(TrustedAdult, email: trusted_adult_email) do
nil ->
Repo.insert!(%TrustedAdult{email: "[email protected]"})
Repo.insert!(%TrustedAdult{email: trusted_adult_email})
adult ->
adult
end
Expand Down
5 changes: 3 additions & 2 deletions priv/repo/create_test_users.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
alias Afc.{Repo, TrustedAdult, User}
trusted_adult_email = Map.fetch!(System.get_env(), "EMAIL_TRUSTED_ADULT")

adult =
case Repo.get_by(TrustedAdult, email: "[email protected]") do
case Repo.get_by(TrustedAdult, email: trusted_adult_email) do
nil ->
Repo.insert!(%TrustedAdult{email: "[email protected]"})
Repo.insert!(%TrustedAdult{email: trusted_adult_email})
adult ->
adult
end
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20181025143750_share_emotion_log.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Afc.Repo.Migrations.ShareEmotionLog do
use Ecto.Migration

def change do
alter table(:emotion_logs) do
add :shared, :boolean, default: false, null: false
end
end
end