Skip to content
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: 9 additions & 4 deletions lib/intercom_stats/repository/conversations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ defmodule IntercomStats.Repository.Conversations do
alias IntercomStats.Repository.Segments
import Ecto.Query

def list_all_conversations(%{}) do
def list_all_conversations(%{}), do: list_all_conversations
def list_all_conversations() do
Repo.all(Conversation)
end

Expand All @@ -14,12 +15,16 @@ defmodule IntercomStats.Repository.Conversations do
Repo.all(from c in Conversation, where: like(c.company_name, ^company))
end

def list_conversations_by_tags(:or, tags_list) do
def list_conversations_by_tags(tag_id, %{company_name: company_name}) do
company = "%#{company_name}%"
query = from t in "tags",
join: ct in "conversations_tags", on: t.id == ct.tag_id,
join: c in "conversations", on: ct.conversation_id == c.id,
where: t.id in ^tags_list,
select: %Conversation{id: c.id}
where: t.id == ^tag_id and like(c.company_name, ^company),
select: %Conversation{
id: c.id,
time_to_first_response: c.time_to_first_response
}
Repo.all(query)
|> Enum.uniq_by(fn %{id: id} -> id end)
end
Expand Down
9 changes: 4 additions & 5 deletions lib/intercom_stats/repository/tags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ defmodule IntercomStats.Repository.Tags do
import Ecto.Query

def list_all_tags(%{name: name}) do
Repo.all(from tag in Tag,
where: tag.name == ^name,
join: conversations in assoc(tag, :conversations),
preload: [conversations: conversations])
name = "%#{name}%"
Repo.all(from tag in Tag, where: like(tag.name, ^name))
end

def list_all_tags(%{}) do
def list_all_tags(%{}), do: list_all_tags
def list_all_tags() do
Repo.all(Tag)
end
end
2 changes: 1 addition & 1 deletion lib/intercom_stats_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule IntercomStatsWeb.PageController do
end

defp model() do
conversations = Repository.Conversations.list_all_conversations(%{})
conversations = Repository.Conversations.list_all_conversations()

model = %{
average_response_time: get_average_time(:first_response, conversations),
Expand Down
4 changes: 2 additions & 2 deletions lib/intercom_stats_web/resolvers/conversation.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule IntercomStatsWeb.Resolvers.Conversation do
alias IntercomStats.Repository.Conversations

def get_conversations(_, arg, _) do
{:ok, Conversations.list_all_conversations(arg)}
def get_conversations(_, filter, _) do
{:ok, Conversations.list_all_conversations(filter)}
end
end
16 changes: 14 additions & 2 deletions lib/intercom_stats_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule IntercomStatsWeb.Schema do

@desc "Get all conversations"
field :conversations, list_of(:conversation) do
arg :company_name, :string
arg :filter, :conversation_filter
resolve authorize(&Resolvers.Conversation.get_conversations/3)
end

Expand All @@ -35,7 +35,14 @@ defmodule IntercomStatsWeb.Schema do
object :tag do
field :id, :string
field :name, :string
field :conversations, list_of(:conversation), resolve: assoc(:conversations)
field :conversations, list_of(:conversation) do
arg :filter, :conversation_filter
resolve assoc(:conversations, fn conversation_query, %{filter: %{company_name: company_name}}, _context ->
company_name = "%#{company_name}%"
conversation_query
|> where([c], like(c.company_name, ^company_name))
Copy link
Copy Markdown
Contributor

@fatboypunk fatboypunk Dec 20, 2017

Choose a reason for hiding this comment

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

end)
end
end

@desc "Conversation data"
Expand All @@ -56,6 +63,11 @@ defmodule IntercomStatsWeb.Schema do
field :email, :string
end

# Filter objects
input_object :conversation_filter do
field :company_name, :string
end

# Authorization
def authorize(fun) do
fn (source, args, %{context: %{current_user: user} = info}) ->
Expand Down