Skip to content

Commit

Permalink
Merge pull request #21 from nestauk/disengaged-message
Browse files Browse the repository at this point in the history
Message disengaged users
  • Loading branch information
cdccollins authored Oct 7, 2024
2 parents 5ae1ab8 + 94db960 commit 983464e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ class User < ApplicationRecord
scope :wants_afternoon_message, -> { where(timing: "afternoon") }
scope :wants_evening_message, -> { where(timing: "evening") }
scope :no_preference_message, -> { where(timing: ["no_preference", nil]) }
scope :not_clicked_last_two_messages, -> {
joins(:messages)
.where(
messages: {
id: Message
.select(:id)
.where("messages.user_id = users.id")
.where.not(content_id: nil)
.order(created_at: :desc)
.limit(2)
}
)
.group("users.id")
.having("COUNT(CASE WHEN messages.clicked_at IS NULL THEN 1 END) = 2")
}

enum timing: {
morning: "morning",
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/scheduler.rake
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ namespace :scheduler do
RestartMessagesJob.perform_later(user)
end
end

desc "Check for disengaged users"
task check_for_disengaged_users: :environment do
User.contactable.not_clicked_last_two_messages.each do |user|
message = Message.create(user:, body: "Hey are you ok?")
SendCustomMessageJob.perform_later(message)
end
end
end
17 changes: 17 additions & 0 deletions test/lib/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,21 @@ class SchedulerTest < ActiveSupport::TestCase
assert user3.contactable
assert_nil user3.restart_at
end

test "check_for_disengaged_users" do
user = create(:user)
content = create(:content)
create(:message, user:, clicked_at: nil, content:)
create(:message, user:, clicked_at: nil, content:)

user2 = create(:user)
create(:message, user: user2, clicked_at: nil, content:)
create(:message, user: user2, clicked_at: Time.now, content:)

assert_enqueued_with(job: SendCustomMessageJob) do
Rake::Task["scheduler:check_for_disengaged_users"].execute
end

assert_equal 1, Message.where(body: "Hey are you ok?").count
end
end
25 changes: 25 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ class UserTest < ActiveSupport::TestCase
assert_includes User.no_preference_message, no_preference
end

test "not_clicked_last_two_messages scope" do
content = create(:content)
user1 = create(:user)
create(:message, user: user1, content:)
create(:message, user: user1, content:)

user2 = create(:user)
create(:message, user: user2, content:)
create(:message, user: user2, content:)
create(:message, user: user2, clicked_at: Time.now, content:)
create(:message, user: user2, clicked_at: Time.now, content:)

user3 = create(:user)
create(:message, user: user3, content:)
create(:message, user: user3, content:, clicked_at: Time.now)
create(:message, user: user3, content:)

user4 = create(:user)
create(:message, user: user4)
create(:message, user: user4)

assert_equal User.not_clicked_last_two_messages.to_a.size, 1
assert_equal User.not_clicked_last_two_messages, [user1]
end

test "child_age_in_months_today method" do
user = create(:user, child_birthday: Time.now - 5.months)

Expand Down

0 comments on commit 983464e

Please sign in to comment.