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

Add Close and Reopen conversation parts when conversation is closed or reopened #1378

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions lib/code_corps/emails/receipt_email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ defmodule CodeCorps.Emails.ReceiptEmail do
import Bamboo.PostmarkHelper

alias CodeCorps.Emails.BaseEmail
alias CodeCorps.{DonationGoal, Project, Repo, StripeConnectCharge, StripeConnectSubscription, WebClient}
alias CodeCorps.{DonationGoal, Project, Repo, StripeConnectCharge, StripeConnectSubscription, WebClient, User}

@spec create(StripeConnectCharge.t, Stripe.Invoice.t) :: Bamboo.Email.t
@spec get_name(User.t) :: String.t
def get_name(%User{ first_name: nil }), do: "there"

@spec get_name(User.t) :: String.t
def get_name(%User{ first_name: name}), do: name

@spec create(StripeConnectCharge.t, Stripe.Invoice.t) :: Bamboo.Email.t
def create(%StripeConnectCharge{} = charge, %Stripe.Invoice{} = invoice) do
with %StripeConnectCharge{} = charge <- Repo.preload(charge, :user),
%Project{} = project <- get_project(invoice.subscription),
Expand Down Expand Up @@ -55,7 +61,8 @@ defmodule CodeCorps.Emails.ReceiptEmail do
project_current_donation_goal_description: current_donation_goal.description,
project_title: project.title,
project_url: project |> url(),
subject: project |> build_subject_line()
subject: project |> build_subject_line(),
name: get_name(charge.user)
}
end

Expand Down
21 changes: 21 additions & 0 deletions lib/code_corps/messages/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ defmodule CodeCorps.Messages do
Repo
}
alias Ecto.{Changeset, Queryable}

@reopened "reopened"
@closed "closed"

@doc ~S"""
Lists pre-scoped `CodeCorps.Message` records filtered by parameters.
Expand Down Expand Up @@ -52,9 +55,27 @@ defmodule CodeCorps.Messages do
Conversation |> Repo.get(id)
end

@doc ~S"""
Updates a `CodeCorps.Conversation` record
"""
def update_conversation(conversation, %{status: @reopened} = params) do
{:ok, now} = Timex.format(Timex.now, "{ISO:Extended}")
add_part(%{"conversation_id" => conversation.id, "body" => "Reopened on " <> now , "author_id"
=> conversation.user_id, "part_type" => "reopened"})
conversation |> Conversation.update_changeset(params) |> Repo.update
end

def update_conversation(conversation, %{status: @closed} = params) do
{:ok, now} = Timex.format(Timex.now, "{ISO:Extended}")
add_part(%{"conversation_id" => conversation.id, "body" => "Closed on " <> now, "author_id"
=> conversation.user_id, "part_type" => "closed"})
conversation |> Conversation.update_changeset(params) |> Repo.update
end

def update_conversation(conversation, params) do
conversation |> Conversation.update_changeset(params) |> Repo.update
end


@doc ~S"""
Gets a `CodeCorps.ConversationPart` record
Expand Down
2 changes: 1 addition & 1 deletion lib/code_corps/model/conversation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ defmodule CodeCorps.Conversation do
end

defp statuses do
~w{ open closed }
~w{ open closed reopened }
end
end
8 changes: 7 additions & 1 deletion test/lib/code_corps/emails/receipt_email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ defmodule CodeCorps.Emails.ReceiptEmailTest do

alias CodeCorps.Emails.ReceiptEmail


test "get name returns there on nil name" do
user = %CodeCorps.User{}
assert ReceiptEmail.get_name(user) == "there"
end

test "receipt email works" do
invoice_fixture = CodeCorps.StripeTesting.Helpers.load_fixture("invoice")

Expand Down Expand Up @@ -50,7 +56,7 @@ defmodule CodeCorps.Emails.ReceiptEmailTest do
project_title: "Code Corps",
project_url: "http://localhost:4200/#{project.organization.slug}/#{project.slug}",
project_current_donation_goal_description: "Test goal",
subject: "Your monthly donation to Code Corps"
subject: "Your monthly donation to Code Corps",
}
assert high_five_image_url
end
Expand Down
31 changes: 31 additions & 0 deletions test/lib/code_corps/messages/messages_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,37 @@ defmodule CodeCorps.MessagesTest do
assert result.id == conversation.id
end
end

describe "update_conversation/2" do

test "creates a conversation_part of part_type reopened when a conversation is reopened" do
conversation = insert(:conversation)

assert Repo.aggregate(ConversationPart, :count, :id) == 0
conversation = Messages.get_conversation(conversation.id)
{_ok, _updated} = Messages.update_conversation(conversation,%{status: "reopened"})

assert Repo.aggregate(ConversationPart, :count, :id) == 1

conversation_part = Repo.get_by(ConversationPart, part_type: "reopened")
assert conversation_part.author_id == conversation.user_id
assert conversation_part.conversation_id == conversation.id
end

test "creates a conversation_part of part_type closed when a conversation is closed" do
conversation = insert(:conversation)

assert Repo.aggregate(ConversationPart, :count, :id) == 0
conversation = Messages.get_conversation(conversation.id)
{_ok, _updated} = Messages.update_conversation(conversation,%{status: "closed"})

assert Repo.aggregate(ConversationPart, :count, :id) == 1

conversation_part = Repo.get_by(ConversationPart, part_type: "closed")
assert conversation_part.author_id == conversation.user_id
assert conversation_part.conversation_id == conversation.id
end
end

describe "get_part/1" do
test "gets a single part" do
Expand Down