diff --git a/app/controllers/admin/mailshots_controller.rb b/app/controllers/admin/mailshots_controller.rb index 6a6b6869b0..627ee7be9c 100644 --- a/app/controllers/admin/mailshots_controller.rb +++ b/app/controllers/admin/mailshots_controller.rb @@ -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}" } diff --git a/app/models/mailshot.rb b/app/models/mailshot.rb index 9c7ea20a92..d963ee94f1 100644 --- a/app/models/mailshot.rb +++ b/app/models/mailshot.rb @@ -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? diff --git a/app/models/user/bootcamp_data.rb b/app/models/user/bootcamp_data.rb index c3e5ae0f3e..5d64cd4ff5 100644 --- a/app/models/user/bootcamp_data.rb +++ b/app/models/user/bootcamp_data.rb @@ -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 diff --git a/test/commands/mailshot/send_to_audience_segment_test.rb b/test/commands/mailshot/send_to_audience_segment_test.rb index 0c34a9ee5e..e8ef70cf3d 100644 --- a/test/commands/mailshot/send_to_audience_segment_test.rb +++ b/test/commands/mailshot/send_to_audience_segment_test.rb @@ -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 @@ -188,8 +196,16 @@ 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 @@ -197,6 +213,8 @@ class Mailshot::SendToAudienceSegmentTest < ActiveSupport::TestCase 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) @@ -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)