diff --git a/app/builders/bulk_subscriber_list_email_builder_for_filtered_subscriptions.rb b/app/builders/bulk_subscriber_list_email_builder_for_filtered_subscriptions.rb deleted file mode 100644 index 552de6328..000000000 --- a/app/builders/bulk_subscriber_list_email_builder_for_filtered_subscriptions.rb +++ /dev/null @@ -1,53 +0,0 @@ -class BulkSubscriberListEmailBuilderForFilteredSubscriptions - def initialize(subscriber_list) - @subscriber_list = subscriber_list - end - - attr_reader :subscriber_list - - def call - records_to_insert.empty? ? [] : Email.insert_all!(records_to_insert).pluck("id") - end - - def target_subscriptions - subscriber_list - .subscriptions - .where(ended_reason: "bulk_unsubscribed") - .select { |sub| unsubscribed_on_target_date?(sub.ended_at) } - end - - def unsubscribed_on_target_date?(date_str) - (date_str.day == 22) && (date_str.month == 9) && (date_str.year == 2023) - end - - def records_to_insert - target_subscriptions.map do |subscription| - subscriber = subscription.subscriber - - { - address: subscriber.address, - subject: email_subject, - body: email_body, - subscriber_id: subscriber.id, - created_at: Time.zone.now, - updated_at: Time.zone.now, - } - end - end - - def email_body - <<~BODY - You asked GOV.UK to email you when we add or update a page about Trade marks. - - We emailed you earlier to say that the Trade marks page has been archived. The email included the wrong link for where you can find out more information about the topic. - - You can find out more information about intellectual property trade marks at https://www.gov.uk/government/collections/intellectual-property-trade-marks - - You can also sign up for email updates on that page if you would like to continue to get email updates from GOV.UK about trade marks. - BODY - end - - def email_subject - "CORRECTION - Update from GOV.UK for: Trade marks" - end -end diff --git a/lib/tasks/bulk_email.rake b/lib/tasks/bulk_email.rake index 67fa5095a..d5e412cf9 100644 --- a/lib/tasks/bulk_email.rake +++ b/lib/tasks/bulk_email.rake @@ -26,15 +26,4 @@ namespace :bulk_email do end puts "Sending #{email_ids.count} emails to subscribers on the following lists: #{subscriber_lists.pluck(:slug).join(', ')}" end - - desc "Send a bulk email to users that were bulk unsubscribed on Friday 22nd Sept in error" - task for_users_unsubscribed_in_error: :environment do - subscriber_list = SubscriberList.find_by(slug: "intellectual-property-trade-marks") - - email_ids = BulkSubscriberListEmailBuilderForFilteredSubscriptions.new(subscriber_list).call - email_ids.each do |id| - SendEmailWorker.perform_async_in_queue(id, queue: :send_email_immediate) - end - puts "Sending #{email_ids.count} emails to subscribers on subscriber list: #{subscriber_list.title}" - end end diff --git a/spec/builders/bulk_subscriber_list_email_builder_for_filtered_subscriptions_spec.rb b/spec/builders/bulk_subscriber_list_email_builder_for_filtered_subscriptions_spec.rb deleted file mode 100644 index c5896925d..000000000 --- a/spec/builders/bulk_subscriber_list_email_builder_for_filtered_subscriptions_spec.rb +++ /dev/null @@ -1,51 +0,0 @@ -RSpec.describe BulkSubscriberListEmailBuilderForFilteredSubscriptions do - describe ".call" do - let!(:trade_marks_subscriber_list) { create(:subscriber_list, title: "Trade marks", slug: "intellectual-property-trade-marks") } - - let(:email_body) do - <<~BODY - You asked GOV.UK to email you when we add or update a page about Trade marks. - - We emailed you earlier to say that the Trade marks page has been archived. The email included the wrong link for where you can find out more information about the topic. - - You can find out more information about intellectual property trade marks at https://www.gov.uk/government/collections/intellectual-property-trade-marks - - You can also sign up for email updates on that page if you would like to continue to get email updates from GOV.UK about trade marks. - BODY - end - - let(:email_subject) { "CORRECTION - Update from GOV.UK for: Trade marks" } - - let!(:target_date) { Time.zone.local(2023, 9, 22) } - let!(:other_date) { Time.zone.local(2021, 1, 1) } - - let!(:subscriber_one) { create(:subscriber) } - let!(:subscriber_two) { create(:subscriber) } - let!(:subscriber_three) { create(:subscriber) } - - let!(:subscription_bulk_unsubscribed_on_target_date) do - create(:subscription, subscriber: subscriber_one, subscriber_list: trade_marks_subscriber_list, ended_at: target_date, ended_reason: "bulk_unsubscribed") - end - - let!(:subscription_bulk_unsubscribed_on_other_date) do - create(:subscription, subscriber: subscriber_two, subscriber_list: trade_marks_subscriber_list, ended_at: other_date, ended_reason: "bulk_unsubscribed") - end - - let!(:subscription_unsubscribed_by_user_on_target_date) do - create(:subscription, subscriber: subscriber_three, subscriber_list: trade_marks_subscriber_list, ended_at: target_date, ended_reason: "subscriber_list_changed") - end - - it "should only create an email for the users who were bulk unsubscribed on the target date" do - email_ids = described_class.new(trade_marks_subscriber_list).call - - expect(email_ids.count).to eq 1 - - email = Email.find(email_ids).first - - expect(email.subscriber_id).to eq subscriber_one.id - expect(email.address).to eq subscriber_one.address - expect(email.subject).to eq email_subject - expect(email.body).to eq email_body - end - end -end diff --git a/spec/lib/tasks/bulk_email_spec.rb b/spec/lib/tasks/bulk_email_spec.rb index a8bc3e90d..3e4fd400c 100644 --- a/spec/lib/tasks/bulk_email_spec.rb +++ b/spec/lib/tasks/bulk_email_spec.rb @@ -52,55 +52,4 @@ ).to_stdout end end - - describe "for_users_unsubscribed_in_error" do - before do - Rake::Task["bulk_email:for_users_unsubscribed_in_error"].reenable - end - - it "builds emails for a the trade marks subscriber list" do - trade_marks_subscriber_list = create(:subscriber_list, title: "Trade marks", slug: "intellectual-property-trade-marks") - - expect(BulkSubscriberListEmailBuilderForFilteredSubscriptions) - .to receive(:new) - .with(trade_marks_subscriber_list) - .and_call_original - - Rake::Task["bulk_email:for_users_unsubscribed_in_error"].invoke(trade_marks_subscriber_list) - end - - it "enqueues the emails for delivery" do - trade_marks_subscriber_list = create(:subscriber_list, title: "Trade marks", slug: "intellectual-property-trade-marks") - builder_double = instance_double(BulkSubscriberListEmailBuilderForFilteredSubscriptions) - allow(BulkSubscriberListEmailBuilderForFilteredSubscriptions).to receive(:new).and_return(builder_double) - - allow(builder_double).to receive(:call) - .and_return([1, 2]) - - expect(SendEmailWorker) - .to receive(:perform_async_in_queue) - .with(1, queue: :send_email_immediate) - - expect(SendEmailWorker) - .to receive(:perform_async_in_queue) - .with(2, queue: :send_email_immediate) - - Rake::Task["bulk_email:for_users_unsubscribed_in_error"].invoke(trade_marks_subscriber_list) - end - - it "states how many emails are being sent, and to where" do - trade_marks_subscriber_list = create(:subscriber_list, title: "Trade marks", slug: "intellectual-property-trade-marks") - - builder_double = instance_double(BulkSubscriberListEmailBuilderForFilteredSubscriptions) - allow(BulkSubscriberListEmailBuilderForFilteredSubscriptions).to receive(:new).and_return(builder_double) - - allow(builder_double).to receive(:call) - .and_return([1, 2, 3, 4, 5]) - - expect { Rake::Task["bulk_email:for_users_unsubscribed_in_error"].invoke(trade_marks_subscriber_list) } - .to output( - /Sending 5 emails to subscribers on subscriber list: Trade marks/, - ).to_stdout - end - end end