diff --git a/app/jobs/send_inactive_user_emails_job.rb b/app/jobs/send_inactive_user_emails_job.rb index 026a1667b..5bd92f489 100644 --- a/app/jobs/send_inactive_user_emails_job.rb +++ b/app/jobs/send_inactive_user_emails_job.rb @@ -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| @@ -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 @@ -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]) diff --git a/lib/tasks/emails.rake b/lib/tasks/emails.rake new file mode 100644 index 000000000..3cb22fa00 --- /dev/null +++ b/lib/tasks/emails.rake @@ -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 diff --git a/spec/jobs/send_inactive_user_emails_job_spec.rb b/spec/jobs/send_inactive_user_emails_job_spec.rb index 59c06a43d..bb939bad0 100644 --- a/spec/jobs/send_inactive_user_emails_job_spec.rb +++ b/spec/jobs/send_inactive_user_emails_job_spec.rb @@ -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, { @@ -67,7 +67,7 @@ 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) @@ -75,7 +75,7 @@ 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)