Skip to content

Commit

Permalink
Matches for group (#72)
Browse files Browse the repository at this point in the history
* Move RetrieveSlackUserInfo to minitest

* Add method to HistoricalMatch for getting profiled members

* Add historical match filter
  • Loading branch information
tuxagon committed Jun 4, 2024
1 parent fc72c3a commit 2cd2e1f
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
!/app/assets/builds/.keep

.irb_history

.DS_Store
2 changes: 1 addition & 1 deletion app/controllers/profile_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def load_current_user_profile(slack_user_id)
@user_profile = SlackUserProfile.find_by(slack_user_id: slack_user_id)
return if @user_profile.present?

Slack::RetrievesSlackUserInfo.new.call(user: slack_user_id)
Slack::RetrieveSlackUserInfo.new.call(user: slack_user_id)

@user_profile = SlackUserProfile.find_by(slack_user_id: slack_user_id)
end
Expand Down
12 changes: 11 additions & 1 deletion app/controllers/recent_matches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ def index
@recent_matches = CollectsRecentMatchesForUser.new.call(user: @current_user)
end

def filter
load_current_user_profile

@historical_matches = HistoricalMatch.where(filter_params).order(matched_on: :desc)
end

private

def load_current_user_profile
@user_profile = SlackUserProfile.find_by(slack_user_id: @current_user.slack_user_id)
return if @user_profile.present?

Slack::RetrievesSlackUserInfo.new.call(user: @current_user.slack_user_id)
Slack::RetrieveSlackUserInfo.new.call(user: @current_user.slack_user_id)

@user_profile = SlackUserProfile.find_by(slack_user_id: @current_user.slack_user_id)
end

def filter_params
params.permit(:grouping)
end
end
14 changes: 14 additions & 0 deletions app/models/historical_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def self.protracted_in(grouping)
SQL
end

def profiled_members
members.map do |member|
user_profile = SlackUserProfile.find_by(slack_user_id: member)

if user_profile.nil?
Slack::RetrieveSlackUserInfo.new.call(user: member)

user_profile = SlackUserProfile.find_by(slack_user_id: member)
end

user_profile
end
end

private

def at_least_two_members
Expand Down
4 changes: 2 additions & 2 deletions app/services/collects_recent_matches_for_user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CollectsRecentMatchesForUser
def initialize
@retrieves_slack_user_info = Slack::RetrievesSlackUserInfo.new
@retrieve_slack_user_info = Slack::RetrieveSlackUserInfo.new
end

def call(user:)
Expand All @@ -26,7 +26,7 @@ def fetch_user_profile(slack_user_id)
user_profile = SlackUserProfile.find_by(slack_user_id: slack_user_id)

if user_profile.nil?
@retrieves_slack_user_info.call(user: slack_user_id)
@retrieve_slack_user_info.call(user: slack_user_id)

user_profile = SlackUserProfile.find_by(slack_user_id: slack_user_id)
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/notify/use_email_to_deliver_notification.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Notify
class UseEmailToDeliverNotification
def initialize
@retrieves_slack_user_info = Slack::RetrievesSlackUserInfo.new
@retrieve_slack_user_info = Slack::RetrieveSlackUserInfo.new
@build_group_mailer_message = Mailer::BuildGroupMailerMessage.new
end

Expand All @@ -26,7 +26,7 @@ def call(notification, group)
private

def convert_to_match_member(member_id)
slack_user = @retrieves_slack_user_info.call(user: member_id)
slack_user = @retrieve_slack_user_info.call(user: member_id)
Mailer::MatchMember.from_slack_user(slack_user)
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Slack
class RetrievesSlackUserInfo
class RetrieveSlackUserInfo
include RateLimitRetryable

def call(user:)
Expand Down
24 changes: 24 additions & 0 deletions app/views/recent_matches/_historical_match.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div id="historical_match_<%= historical_match.id %>" class="my-4 px-4">
<div class="flex flex-col space-y-4 border border-black">
<div class="flex flex-wrap">
<% historical_match.profiled_members.each do |member| %>
<%= link_to profile_path(member.slack_user_id) do %>
<div class="flex flex-row p-2">
<img class="w-8 h-8" src="<%= member.avatar_url %>" />
<span class="leading-8 px-2"><%= member.name %></span>
</div>
<% end %>
<% end %>
</div>
<div class="flex flex-row justify-between">
<div class="text-sm text-slate-500 m-1">
<%= historical_match.grouping.titleize %>
</div>
<div class="text-sm text-slate-500 m-1">
<div>
<span><%= historical_match.matched_on.strftime("%b %d, %Y") %></span>
</div>
</div>
</div>
</div>
</div>
13 changes: 13 additions & 0 deletions app/views/recent_matches/filter.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="w-full">
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Historical matches</h1>
</div>

<div id="historical_matches" class="min-w-full">
<% if @historical_matches.empty? %>
<p class="px-4">No matches made yet</p>
<% else %>
<%= render partial: "historical_match", collection: @historical_matches %>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
root to: "root#index"

get "/matches", to: "recent_matches#index", as: "recent_matches"
get "/matches/filter", to: "recent_matches#filter", as: "filter_recent_matches"
get "/profile/:slack_user_id", to: "profile#show", as: "profile"
resources :calendar_links, except: [:show]

Expand Down
10 changes: 5 additions & 5 deletions spec/lib/collects_recent_matches_for_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
let(:subject) { CollectsRecentMatchesForUser.new }

before(:example) do
@retrieves_slack_user_info = double(Slack::RetrievesSlackUserInfo)
@retrieve_slack_user_info = double(Slack::RetrieveSlackUserInfo)

allow(Slack::RetrievesSlackUserInfo).to receive(:new) { @retrieves_slack_user_info }
allow(Slack::RetrieveSlackUserInfo).to receive(:new) { @retrieve_slack_user_info }
end

it "merges matches and slack profile data from the database for match members" do
Expand All @@ -15,7 +15,7 @@
SlackUserProfile.create(name: "Leia", slack_user_id: "USER_ID_2", avatar_url: "https://example.com/x/512/512")
match = HistoricalMatch.create(members: ["USER_ID_1", "USER_ID_2"], grouping: "test", matched_on: match_date)

expect(@retrieves_slack_user_info).to_not receive(:call)
expect(@retrieve_slack_user_info).to_not receive(:call)

user_matches = subject.call(user: user)

Expand All @@ -38,7 +38,7 @@
match1 = HistoricalMatch.create(members: ["USER_ID_1", "USER_ID_2"], grouping: "test", matched_on: match_date - 1.day)
match2 = HistoricalMatch.create(members: ["USER_ID_1", "USER_ID_2"], grouping: "test", matched_on: match_date)

expect(@retrieves_slack_user_info).to_not receive(:call)
expect(@retrieve_slack_user_info).to_not receive(:call)

user_matches = subject.call(user: user)

Expand Down Expand Up @@ -67,7 +67,7 @@
match_date = Date.today
HistoricalMatch.create(members: ["USER_ID_1", "USER_ID_2"], grouping: "test", matched_on: match_date)

expect(@retrieves_slack_user_info).to receive(:call).with(user: "USER_ID_2")
expect(@retrieve_slack_user_info).to receive(:call).with(user: "USER_ID_2")

subject.call(user: user)
end
Expand Down
42 changes: 0 additions & 42 deletions spec/lib/slack/retrieves_slack_user_info_spec.rb

This file was deleted.

16 changes: 16 additions & 0 deletions test/models/historical_match_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,20 @@ class HistoricalMatchTest < ActiveSupport::TestCase

assert_equal matches, [match2]
end

test "#profiled_members returns the user profiles for each member" do
match = @subject.new(members: ["Frodo", "Sam"])
frodo_profile = SlackUserProfile.create(slack_user_id: "Frodo")
sam_profile = SlackUserProfile.create(slack_user_id: "Sam")

assert_equal match.profiled_members, [frodo_profile, sam_profile]
end

test "#profiled_members creates a user profile if one does not exist" do
retrieves_user_info = Mocktail.of_next(Slack::RetrieveSlackUserInfo)

@subject.new(members: ["Frodo"]).profiled_members

verify { retrieves_user_info.call(user: "Frodo") }
end
end
2 changes: 1 addition & 1 deletion test/services/mailer/build_group_mailer_message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Mailer
class BuildGroupMailerMessageTest < ActiveSupport::TestCase
setup do
@retrieves_slack_user_info = Mocktail.of_next(Slack::RetrievesSlackUserInfo)
@retrieve_slack_user_info = Mocktail.of_next(Slack::RetrieveSlackUserInfo)
@subject = BuildGroupMailerMessage.new
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class UseEmailToDeliverNotificationTest < ActiveSupport::TestCase
# Mocktail wasn't working for a class like GroupingMailer. It was returning nil
# and it wasn't clear why. I decided to use Minitest::Mock instead.
@mailer = Minitest::Mock.new
@retrieves_slack_user_info = Mocktail.of_next(Slack::RetrievesSlackUserInfo)
@retrieve_slack_user_info = Mocktail.of_next(Slack::RetrieveSlackUserInfo)
@build_group_mailer_message = Mocktail.of_next(Mailer::BuildGroupMailerMessage)

@group = group_with(name: "test", slack_channel_name: "test-channel")
Expand All @@ -22,10 +22,10 @@ class UseEmailToDeliverNotificationTest < ActiveSupport::TestCase
pending_notifications: [notification]
)

stubs { @retrieves_slack_user_info.call(user: "USER_ID_1") }.with {
stubs { @retrieve_slack_user_info.call(user: "USER_ID_1") }.with {
slack_user_message("USER_ID_1", "Luke", "luke@rebels.com")
}
stubs { @retrieves_slack_user_info.call(user: "USER_ID_2") }.with {
stubs { @retrieve_slack_user_info.call(user: "USER_ID_2") }.with {
slack_user_message("USER_ID_2", "Leia", "leia@rebels.com")
}
stubs { |m|
Expand Down Expand Up @@ -63,7 +63,7 @@ class UseEmailToDeliverNotificationTest < ActiveSupport::TestCase
pending_notifications: [notification]
)

stubs { |m| @retrieves_slack_user_info.call(user: m.any) }
stubs { |m| @retrieve_slack_user_info.call(user: m.any) }
.with { raise "Should not be called" }
stubs { |m|
@build_group_mailer_message.render(
Expand Down
47 changes: 47 additions & 0 deletions test/services/slack/retrieve_slack_user_info_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "test_helper"

module Slack
class RetrieveSlackUserInfoTest < ActiveSupport::TestCase
setup do
@subject = RetrieveSlackUserInfo.new
end

test "loads a user's information from slack" do
stub_slack_client do |slack_client|
@subject.call(user: "USER_ID")

verify { slack_client.users_info(user: "USER_ID") }
end
end

test "stores some user profile information to the database" do
stub_slack_client do |slack_client|
stubs { slack_client.users_info(user: "USER_ID") }.with do
slack_user_response("USER_ID")
end

assert_difference -> { SlackUserProfile.count } do
@subject.call(user: "USER_ID")
end

user_profile = SlackUserProfile.find_by(slack_user_id: "USER_ID")
assert_equal "Luke", user_profile.name
end
end

private

def slack_user_response(id)
Slack::Messages::Message.new(
id: id,
user: Slack::Messages::Message.new(
id: id,
profile: Slack::Messages::Message.new(
real_name: "Luke",
image_512: "https://example.com/x/512/512"
)
)
)
end
end
end

0 comments on commit 2cd2e1f

Please sign in to comment.