Browse Source

CLDC-1688 Impacted logs mailer

cldc-1688-deactivation-affected-logs
Sam Collard 2 years ago
parent
commit
55de9bae52
  1. 14
      app/controllers/locations_controller.rb
  2. 14
      app/controllers/schemes_controller.rb
  3. 38
      app/mailers/location_or_scheme_deactivation_mailer.rb
  4. 97
      spec/mailers/location_or_scheme_deactivation_mailer_spec.rb
  5. 8
      spec/requests/locations_controller_spec.rb
  6. 8
      spec/requests/schemes_controller_spec.rb

14
app/controllers/locations_controller.rb

@ -43,8 +43,16 @@ class LocationsController < ApplicationController
end
def deactivate
if @location.location_deactivation_periods.create!(deactivation_date: params[:deactivation_date]) && reset_location_and_scheme_for_logs!
if @location.location_deactivation_periods.create!(deactivation_date: params[:deactivation_date])
logs = reset_location_and_scheme_for_logs!
flash[:notice] = deactivate_success_notice
LocationOrSchemeDeactivationMailer.new.send_deactivation_mails(
logs.to_a,
url_for(controller: "update_logs", action: "show"),
@location.scheme.service_name,
@location.postcode,
)
end
redirect_to scheme_location_path(@scheme, @location)
end
@ -214,7 +222,9 @@ private
end
def reset_location_and_scheme_for_logs!
@location.lettings_logs.filter_by_before_startdate(params[:deactivation_date].to_time).update!(location: nil, scheme: nil)
logs = @location.lettings_logs.filter_by_before_startdate(params[:deactivation_date].to_time)
logs.update!(location: nil, scheme: nil, impacted_by_scheme_deactivation: true)
logs
end
def toggle_date(key)

14
app/controllers/schemes_controller.rb

@ -44,8 +44,15 @@ class SchemesController < ApplicationController
end
def deactivate
if @scheme.scheme_deactivation_periods.create!(deactivation_date: params[:deactivation_date]) && reset_location_and_scheme_for_logs!
if @scheme.scheme_deactivation_periods.create!(deactivation_date: params[:deactivation_date])
logs = reset_location_and_scheme_for_logs!
flash[:notice] = deactivate_success_notice
LocationOrSchemeDeactivationMailer.new.send_deactivation_mails(
logs.to_a,
url_for(controller: "update_logs", action: "show"),
@scheme.service_name,
)
end
redirect_to scheme_details_path(@scheme)
end
@ -344,7 +351,8 @@ private
end
def reset_location_and_scheme_for_logs!
@scheme.lettings_logs.filter_by_before_startdate(params[:deactivation_date].to_time).update!(location: nil, scheme: nil, impacted_by_scheme_deactivation: true)
# TODO: E-mail users
logs = @scheme.lettings_logs.filter_by_before_startdate(params[:deactivation_date].to_time)
logs.update!(location: nil, scheme: nil, impacted_by_scheme_deactivation: true)
logs
end
end

38
app/mailers/location_or_scheme_deactivation_mailer.rb

@ -0,0 +1,38 @@
class LocationOrSchemeDeactivationMailer < NotifyMailer
DEACTIVATION_TEMPLATE_ID = "8d07a8c3-a4e3-4102-8be7-4ee79e4183fd".freeze
def send_deactivation_mail(user, log_count, update_logs_url, scheme_name, postcode = nil)
send_email(
user.email,
DEACTIVATION_TEMPLATE_ID,
{
log_count:,
log_or_logs: log_count == 1 ? "log" : "logs",
update_logs_url:,
location_or_scheme_description: description(scheme_name, postcode),
},
)
end
def send_deactivation_mails(logs, update_logs_url, scheme_name, postcode = nil)
counts_by_user(logs).each do |user, count|
send_deactivation_mail(user, count, update_logs_url, scheme_name, postcode) if user
end
end
private
def counts_by_user(logs)
logs.each_with_object(Hash.new(0)) do |log, counts|
counts[log.created_by] += 1
end
end
def description(scheme_name, postcode)
if postcode
"the #{postcode} location from the #{scheme_name} scheme"
else
"the #{scheme_name} scheme"
end
end
end

97
spec/mailers/location_or_scheme_deactivation_mailer_spec.rb

@ -0,0 +1,97 @@
require "rails_helper"
RSpec.describe LocationOrSchemeDeactivationMailer do
let(:notify_client) { instance_double(Notifications::Client) }
before do
allow(Notifications::Client).to receive(:new).and_return(notify_client)
allow(notify_client).to receive(:send_email).and_return(true)
end
describe "#send_deactivation_mail" do
let(:user) { FactoryBot.create(:user, email: "user@example.com") }
it "sends a deactivation E-mail via notify" do
update_logs_url = :update_logs_url
expect(notify_client).to receive(:send_email).with(hash_including({
email_address: user.email,
template_id: described_class::DEACTIVATION_TEMPLATE_ID,
personalisation: hash_including({ update_logs_url: }),
}))
described_class.new.send_deactivation_mail(user, 3, update_logs_url, "Test Scheme Name", "test postcode")
end
it "singularises 'logs' correctly" do
expect(notify_client).to receive(:send_email).with(hash_including({
personalisation: hash_including({
log_count: 1,
log_or_logs: "log",
}),
}))
described_class.new.send_deactivation_mail(user, 1, :update_logs_url, :scheme_name)
end
it "pluralises 'logs' correctly" do
expect(notify_client).to receive(:send_email).with(hash_including({
personalisation: hash_including({
log_count: 2,
log_or_logs: "logs",
}),
}))
described_class.new.send_deactivation_mail(user, 2, :update_logs_url, :scheme_name)
end
it "describes a scheme" do
scheme_name = "Test Scheme"
expect(notify_client).to receive(:send_email).with(hash_including({
personalisation: hash_including({
location_or_scheme_description: "the #{scheme_name} scheme",
}),
}))
described_class.new.send_deactivation_mail(user, 3, :update_logs_url, scheme_name)
end
it "describes a location within a scheme" do
scheme_name = "Test Scheme"
postcode = "test postcode"
expect(notify_client).to receive(:send_email).with(hash_including({
personalisation: hash_including({
location_or_scheme_description: "the #{postcode} location from the #{scheme_name} scheme",
}),
}))
described_class.new.send_deactivation_mail(user, 3, :update_logs_url, scheme_name, postcode)
end
end
describe "#send_deactivation_mails" do
let(:user_a) { FactoryBot.create(:user, email: "user_a@example.com") }
let(:user_a_logs) { FactoryBot.create_list(:lettings_log, 1, created_by: user_a) }
let(:user_b) { FactoryBot.create(:user, email: "user_b@example.com") }
let(:user_b_logs) { FactoryBot.create_list(:lettings_log, 3, created_by: user_b) }
let(:logs) { user_a_logs + user_b_logs }
it "sends E-mails to the creators of affected logs with counts" do
expect(notify_client).to receive(:send_email).with(hash_including({
email_address: user_a.email,
personalisation: hash_including({ log_count: user_a_logs.count }),
}))
expect(notify_client).to receive(:send_email).with(hash_including({
email_address: user_b.email,
personalisation: hash_including({ log_count: user_b_logs.count }),
}))
described_class.new.send_deactivation_mails(logs, :update_logs_url, "Test Scheme", "test postcode")
end
end
end

8
spec/requests/locations_controller_spec.rb

@ -1278,8 +1278,12 @@ 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) }
before do
allow(LocationOrSchemeDeactivationMailer).to receive(:new).and_return(mailer)
allow(mailer).to receive(:send_deactivation_mails)
Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user
patch "/schemes/#{scheme.id}/locations/#{location.id}/deactivate", params:
@ -1306,6 +1310,10 @@ RSpec.describe LocationsController, type: :request do
expect(lettings_log.location).to eq(nil)
expect(lettings_log.scheme).to eq(nil)
end
it "sends update E-mails for affected logs" do
expect(mailer).to have_received(:send_deactivation_mails).with([lettings_log], "http://www.example.com/lettings-logs/update-logs", scheme.service_name, location.postcode)
end
end
context "and a log startdate is before location deactivation date" do

8
spec/requests/schemes_controller_spec.rb

@ -1805,8 +1805,12 @@ 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
allow(LocationOrSchemeDeactivationMailer).to receive(:new).and_return(mailer)
allow(mailer).to receive(:send_deactivation_mails)
Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user
patch "/schemes/#{scheme.id}/deactivate", params:
@ -1834,6 +1838,10 @@ RSpec.describe SchemesController, type: :request do
expect(lettings_log.scheme).to eq(nil)
expect(lettings_log.scheme).to eq(nil)
end
it "sends update E-mails for affected logs" do
expect(mailer).to have_received(:send_deactivation_mails).with([lettings_log], "http://www.example.com/lettings-logs/update-logs", scheme.service_name)
end
end
context "and a log startdate is before scheme deactivation date" do

Loading…
Cancel
Save