diff --git a/lib/intercom_stats/repository/conversations.ex b/lib/intercom_stats/repository/conversations.ex index defb7c0d..9c03555a 100644 --- a/lib/intercom_stats/repository/conversations.ex +++ b/lib/intercom_stats/repository/conversations.ex @@ -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 @@ -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 diff --git a/lib/intercom_stats/repository/tags.ex b/lib/intercom_stats/repository/tags.ex index f81f0af0..712c850d 100644 --- a/lib/intercom_stats/repository/tags.ex +++ b/lib/intercom_stats/repository/tags.ex @@ -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 diff --git a/lib/intercom_stats_web/controllers/page_controller.ex b/lib/intercom_stats_web/controllers/page_controller.ex index 4681e1a9..827c5d93 100644 --- a/lib/intercom_stats_web/controllers/page_controller.ex +++ b/lib/intercom_stats_web/controllers/page_controller.ex @@ -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), diff --git a/lib/intercom_stats_web/resolvers/conversation.ex b/lib/intercom_stats_web/resolvers/conversation.ex index 5730ca84..f31fc694 100644 --- a/lib/intercom_stats_web/resolvers/conversation.ex +++ b/lib/intercom_stats_web/resolvers/conversation.ex @@ -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 diff --git a/lib/intercom_stats_web/schema.ex b/lib/intercom_stats_web/schema.ex index 328f26bc..28415ff2 100644 --- a/lib/intercom_stats_web/schema.ex +++ b/lib/intercom_stats_web/schema.ex @@ -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 @@ -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)) + end) + end end @desc "Conversation data" @@ -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}) ->