From d983d2e85d89fe3bb0ba4b3556d72367026c8bb4 Mon Sep 17 00:00:00 2001 From: GlennGeelen Date: Mon, 18 Dec 2017 17:04:03 +0100 Subject: [PATCH 1/2] Add sub queries for conversation tags --- lib/intercom_stats/repository/conversations.ex | 10 +++++++--- lib/intercom_stats/repository/tags.ex | 4 +--- lib/intercom_stats_web/resolvers/conversation.ex | 4 ++-- lib/intercom_stats_web/schema.ex | 16 ++++++++++++++-- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/intercom_stats/repository/conversations.ex b/lib/intercom_stats/repository/conversations.ex index defb7c0d..997a3e78 100644 --- a/lib/intercom_stats/repository/conversations.ex +++ b/lib/intercom_stats/repository/conversations.ex @@ -14,12 +14,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..2abf2988 100644 --- a/lib/intercom_stats/repository/tags.ex +++ b/lib/intercom_stats/repository/tags.ex @@ -6,9 +6,7 @@ defmodule IntercomStats.Repository.Tags do 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]) + where: tag.name == ^name) end def list_all_tags(%{}) do 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}) -> From 3aaac2f26d3e6725fcab0f99d991e05dd88f6189 Mon Sep 17 00:00:00 2001 From: GlennGeelen Date: Wed, 20 Dec 2017 10:16:05 +0100 Subject: [PATCH 2/2] Refactor list all --- lib/intercom_stats/repository/conversations.ex | 3 ++- lib/intercom_stats/repository/tags.ex | 7 ++++--- lib/intercom_stats_web/controllers/page_controller.ex | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/intercom_stats/repository/conversations.ex b/lib/intercom_stats/repository/conversations.ex index 997a3e78..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 diff --git a/lib/intercom_stats/repository/tags.ex b/lib/intercom_stats/repository/tags.ex index 2abf2988..712c850d 100644 --- a/lib/intercom_stats/repository/tags.ex +++ b/lib/intercom_stats/repository/tags.ex @@ -5,11 +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) + 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),