Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new feature to count messages by date #272

Merged
merged 2 commits into from
Oct 5, 2020
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
14 changes: 14 additions & 0 deletions lib/chat_api/reporting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ defmodule ChatApi.Reporting do
|> Repo.all()
end

def count_sent_messages_by_date() do
Message
|> where([message], not is_nil(message.user_id))
|> count_grouped_by_date()
|> Repo.all()
end

def count_received_messages_by_date() do
Message
|> where([message], not is_nil(message.customer_id))
|> count_grouped_by_date()
|> Repo.all()
end

def count_grouped_by_date(query, field \\ :inserted_at) do
query
|> group_by([r], fragment("date(?)", field(r, ^field)))
Expand Down
79 changes: 79 additions & 0 deletions test/chat_api/reporting_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,84 @@ defmodule ChatApi.ReportingTest do
%{date: ~D[2020-09-03], count: 1}
] = Reporting.conversations_by_date(account.id)
end

test "count_sent_messages_by_date/0 groups by date correctly", %{
account: account,
customer: customer
} do
user_2 = user_fixture(account)
user_3 = user_fixture(account)
conversation = conversation_fixture(account, customer)

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-01 12:00:00],
user_id: user_2.id
})

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-02 12:00:00],
user_id: user_2.id
})

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-03 12:00:00],
user_id: user_3.id
})

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-03 12:00:00],
user_id: user_3.id
})

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-03 12:00:00],
customer_id: customer.id
})

assert [
%{date: ~D[2020-09-01], count: 1},
%{date: ~D[2020-09-02], count: 1},
%{date: ~D[2020-09-03], count: 2}
] = Reporting.count_sent_messages_by_date()
end

test "count_received_messages_by_date/0 groups by date correctly", %{
account: account,
customer: customer
} do
user_2 = user_fixture(account)
conversation = conversation_fixture(account, customer)

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-01 12:00:00],
customer_id: customer.id
})

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-02 12:00:00],
customer_id: customer.id
})

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-03 12:00:00],
customer_id: customer.id
})

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-03 12:00:00],
user_id: user_2.id
})

message_fixture(account, conversation, %{
inserted_at: ~N[2020-09-03 12:00:00],
user_id: user_2.id
})

assert [
%{date: ~D[2020-09-01], count: 1},
%{date: ~D[2020-09-02], count: 1},
%{date: ~D[2020-09-03], count: 1}
] = Reporting.count_received_messages_by_date()
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

hooray for tests! 🎉

end
end