From f3ab2c496dcf84c2da571bac82ed609ca366a654 Mon Sep 17 00:00:00 2001 From: Jack <113976590+bibblobcode@users.noreply.github.com> Date: Fri, 16 Jun 2023 14:53:29 +0100 Subject: [PATCH] Do not send deactivation emails when user not present (#1705) --- app/controllers/locations_controller.rb | 17 +++++++++----- app/controllers/schemes_controller.rb | 15 ++++++++---- spec/requests/locations_controller_spec.rb | 27 ++++++++++++++++++---- spec/requests/schemes_controller_spec.rb | 12 +++++++++- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 453c0934f..7ba4a1115 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -180,12 +180,17 @@ class LocationsController < ApplicationController logs = reset_location_and_scheme_for_logs! flash[:notice] = deactivate_success_notice - logs.group_by(&:created_by).transform_values(&:count).compact.each do |user, count| - LocationOrSchemeDeactivationMailer.send_deactivation_mail(user, - count, - url_for(controller: "lettings_logs", action: "update_logs"), - @location.scheme.service_name, - @location.postcode).deliver_later + + logs.group_by(&:created_by).transform_values(&:count).each do |user, count| + next unless user + + LocationOrSchemeDeactivationMailer.send_deactivation_mail( + user, + count, + url_for(controller: "lettings_logs", action: "update_logs"), + @location.scheme.service_name, + @location.postcode, + ).deliver_later end end redirect_to scheme_location_path(@scheme, @location) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 784ad5f62..3a2be3909 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -58,11 +58,16 @@ class SchemesController < ApplicationController logs = reset_location_and_scheme_for_logs! flash[:notice] = deactivate_success_notice - logs.group_by(&:created_by).transform_values(&:count).compact.each do |user, count| - LocationOrSchemeDeactivationMailer.send_deactivation_mail(user, - count, - url_for(controller: "lettings_logs", action: "update_logs"), - @scheme.service_name).deliver_later + + logs.group_by(&:created_by).transform_values(&:count).each do |user, count| + next unless user + + LocationOrSchemeDeactivationMailer.send_deactivation_mail( + user, + count, + url_for(controller: "lettings_logs", action: "update_logs"), + @scheme.service_name, + ).deliver_later end end redirect_to scheme_details_path(@scheme) diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 88d73901c..81b2e56b0 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1467,13 +1467,14 @@ RSpec.describe LocationsController, type: :request do context "when confirming deactivation" do let(:params) { { deactivation_date:, confirm: true, deactivation_date_type: "other" } } - let(:mailer) { instance_double(LocationOrSchemeDeactivationMailer) } - let(:user_a) { create(:user, email: "user_a@example.com") } - let(:user_b) { create(:user, email: "user_b@example.com") } + let(:user_a) { create(:user) } + let(:user_b) { create(:user) } before do - create_list(:lettings_log, 1, :sh, location:, scheme:, startdate:, created_by: user_a) + allow(LocationOrSchemeDeactivationMailer).to receive(:send_deactivation_mail).and_call_original + + create(:lettings_log, :sh, location:, scheme:, startdate:, created_by: user_a) create_list(:lettings_log, 3, :sh, location:, scheme:, startdate:, created_by: user_b) Timecop.freeze(Time.utc(2022, 10, 10)) @@ -1511,6 +1512,24 @@ RSpec.describe LocationsController, type: :request do lettings_log.reload expect(lettings_log.unresolved).to eq(true) end + + it "sends deactivation emails" do + expect(LocationOrSchemeDeactivationMailer).to have_received(:send_deactivation_mail).with( + user_a, + 1, + update_logs_lettings_logs_url, + location.scheme.service_name, + location.postcode, + ) + + expect(LocationOrSchemeDeactivationMailer).to have_received(:send_deactivation_mail).with( + user_b, + 3, + update_logs_lettings_logs_url, + location.scheme.service_name, + location.postcode, + ) + end end context "and the users need to be notified" do diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 4c7bad55a..18ee51756 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -1930,7 +1930,6 @@ RSpec.describe SchemesController, type: :request do context "when confirming deactivation" do let(:params) { { deactivation_date:, confirm: true, deactivation_date_type: "other" } } - let(:mailer) { instance_double(LocationOrSchemeDeactivationMailer) } before do Timecop.freeze(Time.utc(2022, 10, 10)) @@ -1943,6 +1942,8 @@ RSpec.describe SchemesController, type: :request do context "and a log startdate is after scheme deactivation date" do before do + allow(LocationOrSchemeDeactivationMailer).to receive(:send_deactivation_mail).and_call_original + patch "/schemes/#{scheme.id}/deactivate", params: end @@ -1969,6 +1970,15 @@ RSpec.describe SchemesController, type: :request do lettings_log.reload expect(lettings_log.unresolved).to eq(true) end + + it "sends deactivation emails" do + expect(LocationOrSchemeDeactivationMailer).to have_received(:send_deactivation_mail).with( + user, + 1, + update_logs_lettings_logs_url, + scheme.service_name, + ) + end end context "and a log startdate is before scheme deactivation date" do