Skip to content

Commit

Permalink
Slightly larger refactor to get notifications working again
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxagon committed Feb 15, 2024
1 parent 3e39295 commit e35552f
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 219 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mailer
class BuildsGroupingMailerMessage
class BuildGroupMailerMessage
def render(recipient:, channel:, grouping:, other_members:)
GroupingMailer.encourage_match(
recipient: recipient,
Expand Down
33 changes: 33 additions & 0 deletions app/services/notify/use_email_to_deliver_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Notify
class UseEmailToDeliverNotification
def initialize
@retrieves_slack_user_info = Slack::RetrievesSlackUserInfo.new
@build_group_mailer_message = Mailer::BuildGroupMailerMessage.new
end

def call(notification, group)
return unless notification.use_email?

match = notification.historical_match

member_users = match.members.map { |id| convert_to_match_member(id) }
member_users.each do |user|
mailer = @build_group_mailer_message.render(
recipient: user,
channel: group.slack_channel_name,
grouping: match.grouping,
other_members: member_users.reject { |u| u.email == user.email }
)

mailer.deliver_now
end
end

private

def convert_to_match_member(member_id)
slack_user = @retrieves_slack_user_info.call(user: member_id)
Mailer::MatchMember.from_slack_user(slack_user)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
module Notify
class UsesSlackToDeliverNotification
def initialize(config: nil)
class UseSlackToDeliverNotification
def initialize
@opens_slack_conversation = Slack::OpensSlackConversation.new
@sends_slack_message = Slack::SendsSlackMessage.new
@builds_grouping_slack_message = Slack::BuildsGroupingSlackMessage.new

@config = config || Rails.application.config.x.matchmaking
end

def call(notification:)
def call(notification, group)
return unless notification.use_slack?

match = notification.historical_match

channel_name = channel_name_for_grouping(match.grouping)
match_conversation = @opens_slack_conversation.call(users: match.members)

@sends_slack_message.call(
channel: match_conversation,
# TODO refactor to pass match instead of match attributes
blocks: @builds_grouping_slack_message.render(grouping: match.grouping, members: match.members, channel_name: channel_name)
blocks: @builds_grouping_slack_message.render(
grouping: match.grouping,
members: match.members,
channel_name: group.slack_channel_name
)
)
end

Expand Down
42 changes: 0 additions & 42 deletions app/services/notify/uses_email_to_deliver_notification.rb

This file was deleted.

10 changes: 5 additions & 5 deletions app/services/rakes/send_pending_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def initialize(stdout:, stderr:)
@collect_groups = CollectGroups.new
@retrieves_pending_notifications = Notify::RetrievesPendingNotifications.new
@determines_retriability = Notify::DeterminesRetriability.new
@uses_email_to_deliver_notification = Notify::UsesEmailToDeliverNotification.new
@uses_slack_to_deliver_notification = Notify::UsesSlackToDeliverNotification.new
@use_email_to_deliver_notification = Notify::UseEmailToDeliverNotification.new
@use_slack_to_deliver_notification = Notify::UseSlackToDeliverNotification.new
end

def call
Expand All @@ -25,7 +25,7 @@ def call

if sendable_today?(group, notification)
notification_strategy = pick_strategy(notification)
notification_strategy&.call(notification: notification)
notification_strategy&.call(notification, group)
end

notification.delete
Expand All @@ -41,8 +41,8 @@ def sendable_today?(group, notification)
end

def pick_strategy(notification)
return @uses_email_to_deliver_notification if notification.use_email?
@uses_slack_to_deliver_notification if notification.use_slack?
return @use_email_to_deliver_notification if notification.use_email?
@use_slack_to_deliver_notification if notification.use_slack?
end
end
end
36 changes: 0 additions & 36 deletions spec/lib/mailer/builds_grouping_mailer_message_spec.rb

This file was deleted.

75 changes: 0 additions & 75 deletions spec/lib/notify/uses_email_to_deliver_notification_spec.rb

This file was deleted.

48 changes: 0 additions & 48 deletions spec/lib/notify/uses_slack_to_deliver_notification_spec.rb

This file was deleted.

35 changes: 35 additions & 0 deletions test/services/mailer/build_group_mailer_message_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "test_helper"

module Mailer
class BuildGroupMailerMessageTest < ActiveSupport::TestCase
setup do
@retrieves_slack_user_info = Mocktail.of_next(Slack::RetrievesSlackUserInfo)
@subject = BuildGroupMailerMessage.new
end

test "builds a mailer message for a group" do
recipient = Mailer::MatchMember.new(
email: "holmes@deduction.com",
name: "Sherlock"
)
other_member = Mailer::MatchMember.new(
email: "watson@deduction.com",
name: "John Watson"
)

mailer = @subject.render(
recipient: recipient,
grouping: "test_time",
channel: "rotating-test",
other_members: [other_member]
)

assert_equal ["holmes@deduction.com"], mailer.to
assert_equal ["doubot@testdouble.com"], mailer.from
assert_equal ["watson@deduction.com"], mailer.reply_to
assert_equal "Test Time with John Watson", mailer.subject
assert_match(/Howdy Sherlock/, mailer.body.to_s)
assert_match(/You've been matched up with John Watson for Test Time from the #rotating-test Slack channel/, mailer.body.to_s)
end
end
end
Loading

0 comments on commit e35552f

Please sign in to comment.