Skip to content

Commit bb8f624

Browse files
committed
Add test to check that reopened conversations get a converstationpart 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
1 parent 11ed661 commit bb8f624

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

lib/code_corps/messages/messages.ex

+21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ defmodule CodeCorps.Messages do
1212
Repo
1313
}
1414
alias Ecto.{Changeset, Queryable}
15+
16+
@reopened "reopened"
17+
@closed "closed"
1518

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

58+
@doc ~S"""
59+
Updates a `CodeCorps.Conversation` record
60+
"""
61+
def update_conversation(conversation, %{status: @reopened} = params) do
62+
{:ok, now} = Timex.format(Timex.now, "{ISO:Extended}")
63+
add_part(%{"conversation_id" => conversation.id, "body" => "Reopened on " <> now , "author_id"
64+
=> conversation.user_id, "part_type" => "reopened"})
65+
conversation |> Conversation.update_changeset(params) |> Repo.update
66+
end
67+
68+
def update_conversation(conversation, %{status: @closed} = params) do
69+
{:ok, now} = Timex.format(Timex.now, "{ISO:Extended}")
70+
add_part(%{"conversation_id" => conversation.id, "body" => "Closed on " <> now, "author_id"
71+
=> conversation.user_id, "part_type" => "closed"})
72+
conversation |> Conversation.update_changeset(params) |> Repo.update
73+
end
74+
5575
def update_conversation(conversation, params) do
5676
conversation |> Conversation.update_changeset(params) |> Repo.update
5777
end
78+
5879

5980
@doc ~S"""
6081
Gets a `CodeCorps.ConversationPart` record

lib/code_corps/model/conversation.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ defmodule CodeCorps.Conversation do
3434
end
3535

3636
defp statuses do
37-
~w{ open closed }
37+
~w{ open closed reopened }
3838
end
3939
end

test/lib/code_corps/emails/receipt_email_test.exs

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ defmodule CodeCorps.Emails.ReceiptEmailTest do
5757
project_url: "http://localhost:4200/#{project.organization.slug}/#{project.slug}",
5858
project_current_donation_goal_description: "Test goal",
5959
subject: "Your monthly donation to Code Corps",
60-
name: "Jimmy"
6160
}
6261
assert high_five_image_url
6362
end

test/lib/code_corps/messages/messages_test.exs

+31
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,37 @@ defmodule CodeCorps.MessagesTest do
242242
assert result.id == conversation.id
243243
end
244244
end
245+
246+
describe "update_conversation/2" do
247+
248+
test "creates a conversation_part of part_type reopened when a conversation is reopened" do
249+
conversation = insert(:conversation)
250+
251+
assert Repo.aggregate(ConversationPart, :count, :id) == 0
252+
conversation = Messages.get_conversation(conversation.id)
253+
{_ok, _updated} = Messages.update_conversation(conversation,%{status: "reopened"})
254+
255+
assert Repo.aggregate(ConversationPart, :count, :id) == 1
256+
257+
conversation_part = Repo.get_by(ConversationPart, part_type: "reopened")
258+
assert conversation_part.author_id == conversation.user_id
259+
assert conversation_part.conversation_id == conversation.id
260+
end
261+
262+
test "creates a conversation_part of part_type closed when a conversation is closed" do
263+
conversation = insert(:conversation)
264+
265+
assert Repo.aggregate(ConversationPart, :count, :id) == 0
266+
conversation = Messages.get_conversation(conversation.id)
267+
{_ok, _updated} = Messages.update_conversation(conversation,%{status: "closed"})
268+
269+
assert Repo.aggregate(ConversationPart, :count, :id) == 1
270+
271+
conversation_part = Repo.get_by(ConversationPart, part_type: "closed")
272+
assert conversation_part.author_id == conversation.user_id
273+
assert conversation_part.conversation_id == conversation.id
274+
end
275+
end
245276

246277
describe "get_part/1" do
247278
test "gets a single part" do

0 commit comments

Comments
 (0)