@@ -33,16 +33,16 @@ defmodule CodeCorps.Messages.ConversationQuery do
3333
3434
3535 @ doc ~S"""
36- Narrows down a `CodeCorps.Conversation` query to return only those records
37- considered to have a specific status .
36+ Filters `CodeCorps.Conversation` record queries to return only those
37+ considered to be active .
3838
39- The status of `active` means that only those records are included which either
40- - belong to a `CodeCorps.Message` initiated by user
41- - belong to a `CodeCorps.Message` initiated by admin, with at least a single
42- reply in the form of a `CodeCorps.ConversationPart`
39+ Active conversations belong either:
40+ - to a `CodeCorps.Message` initiated by user
41+ - to a `CodeCorps.Message` initiated by an admin, with at least a single
42+ conversation part
4343 """
44- @ spec status_filter ( Queryable . t , map ) :: Queryable . t
45- def status_filter ( queryable , % { "status " => "active" } ) do
44+ @ spec active_filter ( Queryable . t , map ) :: Queryable . t
45+ def active_filter ( queryable , % { "active " => true } ) do
4646 prefiltered_ids = queryable |> select ( [ c ] , c . id ) |> Repo . all
4747
4848 Conversation
@@ -53,5 +53,15 @@ defmodule CodeCorps.Messages.ConversationQuery do
5353 |> having ( [ _c , m , _cp ] , m . initiated_by == "user" )
5454 |> or_having ( [ c , m , cp ] , m . initiated_by == "admin" and count ( cp . id ) > 0 )
5555 end
56- def status_filter ( query , % { } ) , do: query
56+ def active_filter ( query , % { } ) , do: query
57+
58+ @ doc ~S"""
59+ Filters `CodeCorps.Conversation` record queries by their status.
60+ """
61+ @ spec status_filter ( Queryable . t , map ) :: Queryable . t
62+ def status_filter ( queryable , % { "status" => status } ) do
63+ queryable
64+ |> where ( [ c ] , c . status == ^ status )
65+ end
66+ def status_filter ( query , _ ) , do: query
5767end
0 commit comments