Skip to content

Commit

Permalink
Add a rake task to trigger the inactive users emails
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoolas25 committed May 30, 2021
1 parent 80085be commit ed299ad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
10 changes: 5 additions & 5 deletions app/jobs/send_inactive_user_emails_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class SendInactiveUserEmailsJob < ApplicationJob
queue_as :low

DEFAULT_MIN_REFUSED_MATCHES = 2 # At least two refused matches
DEFAULT_AGE_RANGE = nil..nil # Covers all ages
DEFAULT_SIGNED_IN_RANGE = nil..nil # Covers all dates
DEFAULT_AGE_RANGE = 0..200 # Covers all ages
DEFAULT_SIGNED_UP_RANGE = nil..nil # Covers all dates

def perform(*args)
self.class.inactive_user_ids(*args).each do |user_id|
Expand All @@ -14,7 +14,7 @@ def perform(*args)
end
end

def self.inactive_user_ids(min_refused_matches = DEFAULT_MIN_REFUSED_MATCHES, age_range = DEFAULT_AGE_RANGE, signed_in_date_range = DEFAULT_SIGNED_IN_RANGE)
def self.inactive_user_ids(min_refused_matches = DEFAULT_MIN_REFUSED_MATCHES, age_range = DEFAULT_AGE_RANGE, signed_up_date_range = DEFAULT_SIGNED_UP_RANGE)
sql = <<~SQL.squish
with target_users as (
select id
Expand Down Expand Up @@ -45,8 +45,8 @@ def self.inactive_user_ids(min_refused_matches = DEFAULT_MIN_REFUSED_MATCHES, ag
min_refused_matches: min_refused_matches,
min_birthdate: (age_range.end || 200).years.ago.to_date,
max_birthdate: (age_range.begin || 0).years.ago.to_date,
min_created_at: signed_in_date_range.begin || 200.years.ago,
max_created_at: signed_in_date_range.end || Date.current.end_of_day
min_created_at: signed_up_date_range.begin || 200.years.ago,
max_created_at: signed_up_date_range.end || Date.current.end_of_day
}
User.connection.select_values(
ActiveRecord::Base.send(:sanitize_sql_array, [sql, params])
Expand Down
44 changes: 44 additions & 0 deletions lib/tasks/emails.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace :emails do
desc "Send inactive users email to ask them to delete their profile"
task inactive_users: :environment do |_t, args|
tz = ActiveSupport::TimeZone["Europe/Paris"]

min_refused_matches = ENV["MIN_REFUSED_MATCHES"]&.to_i || 2

min_age_range = ENV["MIN_AGE_RANGE"]&.to_i || 0
max_age_range = ENV["MAX_AGE_RANGE"]&.to_i || 200
age_range = min_age_range..max_age_range

min_signed_up_date_range = ENV["MIN_SIGN_UP_DATE_RANGE"].try { |dt| tz.parse(dt) } || 200.years.ago
max_signed_up_date_range = ENV["MAX_SIGN_UP_DATE_RANGE"].try { |dt| tz.parse(dt) } || Date.current.end_of_day
signed_up_date_range = min_signed_up_date_range..max_signed_up_date_range

puts "The following filter will apply:"
puts "- Refused Matches >= #{min_refused_matches}"
puts "- #{min_age_range} <= Age <= #{max_age_range}"
puts "- #{min_signed_up_date_range} <= Sign-up Date <= #{max_signed_up_date_range}"
puts ""
puts "Number of emails to send: ##{SendInactiveUserEmailsJob.inactive_user_ids(min_refused_matches, age_range, signed_up_date_range).size}"
puts ""

if ENV["NO_HELP"].nil?
puts "To customize this, use environment variables:"
puts "- MIN_REFUSED_MATCHES: int default 2"
puts "- MIN_AGE_RANGE: integer; default: 0"
puts "- MAX_AGE_RANGE: integer; default: 200"
puts "- MIN_SIGN_UP_DATE_RANGE: datetime; default: 200 years ago; format: YYYY-MM-DD hh:mm:ss"
puts "- MAX_SIGN_UP_DATE_RANGE: datetime; default: end of today; format: YYYY-MM-DD hh:mm:ss"
puts ""
end

if ENV["NO_PROMPT"].nil?
puts "Is that okay? Enter to continue / Ctrl + C to abort"
puts ">"
gets
end

SendInactiveUserEmailsJob.perform_later(min_refused_matches, age_range, signed_up_date_range)

puts "Emails are enqueued!"
end
end
8 changes: 4 additions & 4 deletions spec/jobs/send_inactive_user_emails_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

RSpec.describe SendInactiveUserEmailsJob do
describe ".inactive_user_ids" do
subject(:inactive_user_ids) { described_class.inactive_user_ids(min_refused_matches, age_range, signed_in_date_range) }
subject(:inactive_user_ids) { described_class.inactive_user_ids(min_refused_matches, age_range, signed_up_date_range) }

let(:min_refused_matches) { described_class::DEFAULT_MIN_REFUSED_MATCHES }
let(:age_range) { described_class::DEFAULT_AGE_RANGE }
let(:signed_in_date_range) { described_class::DEFAULT_SIGNED_IN_RANGE }
let(:signed_up_date_range) { described_class::DEFAULT_SIGNED_UP_RANGE }

let!(:matching_user) do
create(:user, {
Expand Down Expand Up @@ -67,15 +67,15 @@
end

context "when a user was created too recently" do
let(:signed_in_date_range) { ..1.month.ago }
let(:signed_up_date_range) { ..1.month.ago }

it "excludes users creted too recently" do
expect(inactive_user_ids).not_to include(matching_user.id)
end
end

context "when a user was created too far in the past" do
let(:signed_in_date_range) { 10.days.ago.. }
let(:signed_up_date_range) { 10.days.ago.. }

it "excludes users created too far in the past" do
expect(inactive_user_ids).not_to include(matching_user.id)
Expand Down

0 comments on commit ed299ad

Please sign in to comment.