Skip to content
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
28 changes: 19 additions & 9 deletions lib/code_corps/messages/conversation_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ defmodule CodeCorps.Messages.ConversationQuery do


@doc ~S"""
Narrows down a `CodeCorps.Conversation` query to return only those records
considered to have a specific status.
Filters `CodeCorps.Conversation` record queries to return only those
considered to be active.

The status of `active` means that only those records are included which either
- belong to a `CodeCorps.Message` initiated by user
- belong to a `CodeCorps.Message` initiated by admin, with at least a single
reply in the form of a `CodeCorps.ConversationPart`
Active conversations belong either:
- to a `CodeCorps.Message` initiated by user
- to a `CodeCorps.Message` initiated by an admin, with at least a single
conversation part
"""
@spec status_filter(Queryable.t, map) :: Queryable.t
def status_filter(queryable, %{"status" => "active"}) do
@spec active_filter(Queryable.t, map) :: Queryable.t
def active_filter(queryable, %{"active" => true}) do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joshsmith is this how active filter should work? What specifically filters out inactive records?

prefiltered_ids = queryable |> select([c], c.id) |> Repo.all

Conversation
Expand All @@ -53,5 +53,15 @@ defmodule CodeCorps.Messages.ConversationQuery do
|> having([_c, m, _cp], m.initiated_by == "user")
|> or_having([c, m, cp], m.initiated_by == "admin" and count(cp.id) > 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

In response to your question above, this is the query that does it (specifically the having and or_having), and the documentation above expands on that.

end
def status_filter(query, %{}), do: query
def active_filter(query, %{}), do: query

@doc ~S"""
Filters `CodeCorps.Conversation` record queries by their status.
"""
@spec status_filter(Queryable.t, map) :: Queryable.t
def status_filter(queryable, %{"status" => status}) do
queryable
|> where([c], c.status == ^status)
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This still needs a test

def status_filter(query, _), do: query
end
1 change: 1 addition & 0 deletions lib/code_corps/messages/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule CodeCorps.Messages do
def list_conversations(scope, %{} = params) do
scope
|> Messages.ConversationQuery.project_filter(params)
|> Messages.ConversationQuery.active_filter(params)
|> Messages.ConversationQuery.status_filter(params)
|> Messages.ConversationQuery.user_filter(params)
|> Repo.all()
Expand Down
23 changes: 23 additions & 0 deletions test/lib/code_corps/messages/conversation_query_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule CodeCorps.Messages.ConversationQueryTest do
use CodeCorps.DbAccessCase

alias CodeCorps.{
Conversation,
Messages.ConversationQuery,
Repo
}

describe "status_filter/2" do
test "filters by status" do
open_conversation = insert(:conversation, status: "open")
_closed_conversation = insert(:conversation, status: "closed")

[result] =
Conversation
|> ConversationQuery.status_filter(%{"status" => "open"})
|> Repo.all()

assert result.id == open_conversation.id
end
end
end
8 changes: 4 additions & 4 deletions test/lib/code_corps/messages/messages_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ defmodule CodeCorps.MessagesTest do

result_ids =
Conversation
|> Messages.list_conversations(%{"status" => "active"})
|> Messages.list_conversations(%{"active" => true})
|> get_and_sort_ids()

refute conversation_started_by_admin_without_reply.id in result_ids
Expand All @@ -125,7 +125,7 @@ defmodule CodeCorps.MessagesTest do

result_ids =
Conversation
|> Messages.list_conversations(%{"status" => "any"})
|> Messages.list_conversations(%{"status" => "open"})
|> get_and_sort_ids()

assert conversation_started_by_admin_without_reply.id in result_ids
Expand Down Expand Up @@ -192,7 +192,7 @@ defmodule CodeCorps.MessagesTest do
conversation: other_conversation_started_by_admin_with_reply
)

params = %{"status" => "active", "project_id" => project_1.id}
params = %{"active" => true, "project_id" => project_1.id}
result_ids =
Conversation
|> Messages.list_conversations(params)
Expand All @@ -208,7 +208,7 @@ defmodule CodeCorps.MessagesTest do
# project
refute other_conversation_started_by_admin_with_reply.id in result_ids

params = %{"status" => "active", "project_id" => project_2.id}
params = %{"active" => true, "project_id" => project_2.id}
result_ids =
Conversation
|> Messages.list_conversations(params)
Expand Down