Skip to content

Commit

Permalink
Add test to check that reopened conversations get a converstationpart…
Browse files Browse the repository at this point in the history
… with `part_type: "reopened"`

Add test to for conversation part when updated with a status of reopened

Add tests to check for a conversation_part when a conversation is updated with a status of closed

correct erronous field

add function clauses to handle adding closed and reopned conversation_parts

add reopened flag to possible statuses

add time information to body of closed and reopened event
  • Loading branch information
zacck committed Feb 15, 2018
1 parent 11ed661 commit bb8f624
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
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
1 change: 0 additions & 1 deletion test/lib/code_corps/emails/receipt_email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ defmodule CodeCorps.Emails.ReceiptEmailTest do
project_url: "http://localhost:4200/#{project.organization.slug}/#{project.slug}",
project_current_donation_goal_description: "Test goal",
subject: "Your monthly donation to Code Corps",
name: "Jimmy"
}
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

0 comments on commit bb8f624

Please sign in to comment.