Skip to content

Commit

Permalink
Add mailshot group for bc viewed + enrolled (#7199)
Browse files Browse the repository at this point in the history
* Add mailshot group for bc viewed + enrolled

* Add test
  • Loading branch information
iHiD authored Dec 16, 2024
1 parent ca16761 commit 078d444
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/mailshots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def show
@send_count = User::Mailshot.where(mailshot: @mailshot).count
@audiences = %w[
admins donors insiders challenge#12in23 challenge#48in24
bc_interested bc_beginners bc_juniors bc_mid_seniors
bc_viewed bc_enrolled bc_beginners bc_juniors bc_mid_seniors
bc_unspecified_recent_90
]
@audiences += (1..10).map { |min| "bc_unspecified##{min}" }
Expand Down
18 changes: 16 additions & 2 deletions app/models/mailshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,23 @@ def audience_for_challenge(slug)
]
end

def audience_for_bc_interested(_)
def audience_for_bc_viewed(_)
[
User::BootcampData.includes(:user),
User::BootcampData.not_enrolled.
joins(user: :data).
where('user_data.seniority': %i[absolute_beginner beginner]).
includes(user: :bootcamp_data),
lambda do |bootcamp_data|
return if bootcamp_data.paid? # Totally redundant, but still

bootcamp_data.user
end
]
end

def audience_for_bc_enrolled(_)
[
User::BootcampData.enrolled.includes(:user),
lambda do |bootcamp_data|
return if bootcamp_data.paid?

Expand Down
5 changes: 5 additions & 0 deletions app/models/user/bootcamp_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ class User::BootcampData < ApplicationRecord

belongs_to :user, optional: true

scope :enrolled, -> { where.not(enrolled_at: nil) }
scope :paid, -> { where.not(paid_at: nil) }
scope :not_enrolled, -> { where(enrolled_at: nil) }
scope :not_paid, -> { where(paid_at: nil) }

after_save do
User::Bootcamp::SubscribeToOnboardingEmails.defer(self)
end
Expand Down
25 changes: 22 additions & 3 deletions test/commands/mailshot/send_to_audience_segment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,24 @@ class Mailshot::SendToAudienceSegmentTest < ActiveSupport::TestCase
# Now some users that have viewed the page
absolute_beginner_viewed = create :user, seniority: :absolute_beginner
beginner_viewed = create :user, seniority: :beginner
[absolute_beginner_viewed, beginner_viewed].each do |user|
mid_viewed = create :user, seniority: :mid
[absolute_beginner_viewed, beginner_viewed, mid_viewed].each do |user|
create :user_bootcamp_data, user:
end

# Now some users that have viewed the page
absolute_beginner_enrolled = create :user, seniority: :absolute_beginner
beginner_enrolled = create :user, seniority: :beginner
[absolute_beginner_enrolled, beginner_enrolled].each do |user|
create :user_bootcamp_data, user:, enrolled_at: Time.current
end

# Now some that have paid
absolute_beginner_paid = create :user, seniority: :absolute_beginner
beginner_paid = create :user, seniority: :beginner
unspecified_paid = create :user, seniority: nil
[absolute_beginner_paid, beginner_paid, unspecified_paid].each do |user|
create :user_bootcamp_data, user:, paid_at: Time.current
create :user_bootcamp_data, user:, enrolled_at: Time.current, paid_at: Time.current
end

# Finally some unspecified users with specific ids for batches
Expand All @@ -188,15 +196,25 @@ class Mailshot::SendToAudienceSegmentTest < ActiveSupport::TestCase
mailshot = create :mailshot
User::Mailshot::Send.expects(:call).with(absolute_beginner_viewed, mailshot)
User::Mailshot::Send.expects(:call).with(beginner_viewed, mailshot)
User::Mailshot::Send.expects(:call).with(mid_viewed, mailshot).never # No mid+ users

Mailshot::SendToAudienceSegment.(mailshot, :bc_viewed, nil, 20, 0)

# Then enrolled people
mailshot = create :mailshot
User::Mailshot::Send.expects(:call).with(absolute_beginner_enrolled, mailshot)
User::Mailshot::Send.expects(:call).with(beginner_enrolled, mailshot)

Mailshot::SendToAudienceSegment.(mailshot, :bc_interested, nil, 20, 0)
Mailshot::SendToAudienceSegment.(mailshot, :bc_enrolled, nil, 20, 0)

# And now to beginners
mailshot = create :mailshot
User::Mailshot::Send.expects(:call).with(absolute_beginner, mailshot)
User::Mailshot::Send.expects(:call).with(absolute_beginner_viewed, mailshot)
User::Mailshot::Send.expects(:call).with(beginner, mailshot)
User::Mailshot::Send.expects(:call).with(beginner_viewed, mailshot)
User::Mailshot::Send.expects(:call).with(absolute_beginner_enrolled, mailshot)
User::Mailshot::Send.expects(:call).with(beginner_enrolled, mailshot)

Mailshot::SendToAudienceSegment.(mailshot, :bc_beginners, nil, 20, 0)

Expand All @@ -208,6 +226,7 @@ class Mailshot::SendToAudienceSegmentTest < ActiveSupport::TestCase
# And now to mids and seniors
mailshot = create :mailshot
User::Mailshot::Send.expects(:call).with(mid, mailshot)
User::Mailshot::Send.expects(:call).with(mid_viewed, mailshot)
User::Mailshot::Send.expects(:call).with(senior, mailshot)
Mailshot::SendToAudienceSegment.(mailshot, :bc_mid_seniors, nil, 20, 0)

Expand Down

0 comments on commit 078d444

Please sign in to comment.