Skip to content

Commit

Permalink
Add unspecifieds (#7180)
Browse files Browse the repository at this point in the history
* Add unspecifieds

* Add more categories

* Add tests
  • Loading branch information
iHiD authored Dec 3, 2024
1 parent e8c6aff commit 1fa98bd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app/controllers/admin/mailshots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ 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_unspecified
bc_interested bc_beginners bc_juniors bc_mid_seniors
bc_unspecified_recent_90
]
@audiences += (1..10).map { |min| "bc_unspecified##{min}" }
@audiences += [100, 10, 3, 2, 1].map { |min| "reputation##{min}" }
@audiences += [10, 30, 60, 90].map { |min| "recent##{min}" }
@audiences += Track.pluck(:slug).map { |slug| "track##{slug}" }
Expand Down
20 changes: 18 additions & 2 deletions app/models/mailshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ def audience_for_bc_mid_seniors(_)
]
end

def audience_for_bc_unspecified(_)
def audience_for_bc_unspecified_recent_90(_)
[
User::Data.where(seniority: nil).includes(user: :bootcamp_data),
User::Data.where(seniority: nil).
where('user_data.last_visited_on >= ?', Time.current - 90.days).
includes(user: :bootcamp_data),
lambda do |user_data|
user = user_data.user
return if user.bootcamp_data&.paid?
Expand All @@ -120,4 +122,18 @@ def audience_for_bc_unspecified(_)
end
]
end

def audience_for_bc_unspecified(batch)
start_id = (batch.to_i - 1) * 200_000
end_id = (batch.to_i * 200_000) - 1
[
User.where(id: (start_id..end_id)).includes(:data, :bootcamp_data),
lambda do |user|
return if user.seniority.present?
return if user.bootcamp_data&.paid?

user
end
]
end
end
24 changes: 21 additions & 3 deletions test/commands/mailshot/send_to_audience_segment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ class Mailshot::SendToAudienceSegmentTest < ActiveSupport::TestCase
junior = create :user, seniority: :junior
mid = create :user, seniority: :mid
senior = create :user, seniority: :senior
unspecified = create :user, seniority: nil

# Now some users that have viewed the page
absolute_beginner_viewed = create :user, seniority: :absolute_beginner
Expand All @@ -181,6 +180,10 @@ class Mailshot::SendToAudienceSegmentTest < ActiveSupport::TestCase
create :user_bootcamp_data, user:, paid_at: Time.current
end

# Finally some unspecified users with specific ids for batches
unspecified_id_199_999 = create :user, seniority: nil, id: 199_999
unspecified_id_200_000 = create :user, seniority: nil, id: 200_000

# Let's start with a mailshot to interested people
mailshot = create :mailshot
User::Mailshot::Send.expects(:call).with(absolute_beginner_viewed, mailshot)
Expand Down Expand Up @@ -210,8 +213,23 @@ class Mailshot::SendToAudienceSegmentTest < ActiveSupport::TestCase

# And now to unspecifieds
mailshot = create :mailshot
User::Mailshot::Send.expects(:call).with(unspecified, mailshot)
Mailshot::SendToAudienceSegment.(mailshot, :bc_unspecified, nil, 20, 0)
User::Mailshot::Send.expects(:call).with(unspecified_id_199_999, mailshot)
Mailshot::SendToAudienceSegment.(mailshot, :bc_unspecified, 1, 20, 0)

User::Mailshot::Send.expects(:call).with(unspecified_id_200_000, mailshot)
Mailshot::SendToAudienceSegment.(mailshot, :bc_unspecified, 2, 20, 0)
end

test "schedules bc unspecified audience for recently active" do
mailshot = create :mailshot

user = create :user, last_visited_on: 89.days.ago, seniority: nil
create :user, last_visited_on: 91.days.ago, seniority: nil
create :user, last_visited_on: 89.days.ago, seniority: :mid

User::Mailshot::Send.expects(:call).with(user, mailshot)

Mailshot::SendToAudienceSegment.(mailshot, :bc_unspecified_recent_90, nil, 10, 0)
end

test "requeues with deserialization error" do
Expand Down

0 comments on commit 1fa98bd

Please sign in to comment.