From a755f7aa7d3233f646bb4a9520ef327ca08effab Mon Sep 17 00:00:00 2001 From: Becky Ransome <91424856+Beckyrose200@users.noreply.github.com> Date: Thu, 1 Sep 2022 12:34:58 +0100 Subject: [PATCH] Feature/letter for renewal confirmation (#1258) * Adding in CH API down error handling * New service for sending renewal confirmation letters and specs --- .../renewal_confirmation_letter_service.rb | 82 +++++++++++++ .../renewal_completion_service.rb | 15 ++- ...er_tier_ad_renewal_confirmation_letter.yml | 91 +++++++++++++++ ...ewal_confirmation_letter_business_name.yml | 91 +++++++++++++++ ...on_letter_registered_and_business_name.yml | 91 +++++++++++++++ ...al_confirmation_letter_registered_name.yml | 91 +++++++++++++++ ...enewal_confirmation_letter_service_spec.rb | 108 ++++++++++++++++++ .../renewal_completion_service_spec.rb | 15 +++ 8 files changed, 583 insertions(+), 1 deletion(-) create mode 100644 app/services/waste_carriers_engine/notify/renewal_confirmation_letter_service.rb create mode 100644 spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter.yml create mode 100644 spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_business_name.yml create mode 100644 spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_registered_and_business_name.yml create mode 100644 spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_registered_name.yml create mode 100644 spec/services/waste_carriers_engine/notify/renewal_confirmation_letter_service_spec.rb diff --git a/app/services/waste_carriers_engine/notify/renewal_confirmation_letter_service.rb b/app/services/waste_carriers_engine/notify/renewal_confirmation_letter_service.rb new file mode 100644 index 000000000..99e4e7472 --- /dev/null +++ b/app/services/waste_carriers_engine/notify/renewal_confirmation_letter_service.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +module WasteCarriersEngine + module Notify + class RenewalConfirmationLetterService < BaseService + include WasteCarriersEngine::ApplicationHelper + + def run(registration:) + @registration = registration + + client = Notifications::Client.new(WasteCarriersEngine.configuration.notify_api_key) + + client.send_letter(template_id: template, + reference: @registration.reg_identifier, + personalisation: personalisation) + end + + private + + def template + "f703000e-1e76-4529-912d-966691578da0" + end + + def personalisation + { + registration_type: registration_type, + contact_name: contact_name, + reg_identifier: @registration.reg_identifier, + company_name: company_name, + registered_address: registered_address, + phone_number: @registration.phone_number, + date_registered: date_registered, + expiry_date: expiry_date + }.merge(address_lines) + end + + def address_lines + address_values = [ + contact_name, + displayable_address(@registration.contact_address) + ].flatten + + address_hash = {} + + address_values.each_with_index do |value, index| + line_number = index + 1 + address_hash["address_line_#{line_number}".to_sym] = value + end + + address_hash + end + + def registered_address + displayable_address(@registration.contact_address).join(", ") + end + + def contact_name + "#{@registration.first_name} #{@registration.last_name}" + end + + def company_name + @registration.entity_display_name + end + + def date_registered + @registration.metaData.date_registered.in_time_zone("London").to_date.to_s + end + + def expiry_date + @registration.expires_on.in_time_zone("London").to_date.strftime("%e %B %Y").to_s + end + + def registration_type + return unless @registration.upper_tier? + + I18n.t( + "waste_carriers_engine.registration_type.upper.#{@registration.registration_type}" + ) + end + end + end +end diff --git a/app/services/waste_carriers_engine/renewal_completion_service.rb b/app/services/waste_carriers_engine/renewal_completion_service.rb index 6caa9f7d0..74bb0961a 100644 --- a/app/services/waste_carriers_engine/renewal_completion_service.rb +++ b/app/services/waste_carriers_engine/renewal_completion_service.rb @@ -40,7 +40,7 @@ def complete_renewal update_registration create_order_item_logs delete_transient_registration - send_confirmation_email + send_confirmation_messages end end end @@ -104,6 +104,19 @@ def delete_transient_registration transient_registration.delete end + def send_confirmation_messages + send_confirmation_letter unless registration.contact_email.present? + + send_confirmation_email + end + + def send_confirmation_letter + Notify::RenewalConfirmationLetterService.run(registration: registration) + rescue StandardError => e + Airbrake.notify(e, registration_no: registration.reg_identifier) if defined?(Airbrake) + Rails.logger.error "Confirmation letter error: #{e}" + end + def send_confirmation_email Notify::RenewalConfirmationEmailService.run(registration: registration) rescue StandardError => e diff --git a/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter.yml b/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter.yml new file mode 100644 index 000000000..a687c0dd0 --- /dev/null +++ b/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter.yml @@ -0,0 +1,91 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.notifications.service.gov.uk/v2/notifications/letter + body: + encoding: UTF-8 + string: '{"template_id":"f703000e-1e76-4529-912d-966691578da0","reference":"CBDU1","personalisation":{"registration_type":"carrier, + broker and dealer","contact_name":"Jane Doe","reg_identifier":"CBDU1","company_name":"Acme + Waste","registered_address":"42, Foo Gardens, Baz City, BS1 1AA","phone_number":"03708 + 506506","date_registered":" 1 September 2022","expiry_date":" 1 September + 2023","address_line_1":"Jane Doe","address_line_2":"42","address_line_3":"Foo + Gardens","address_line_4":"Baz City","address_line_5":"BS1 1AA"}}' + headers: + User-Agent: + - NOTIFY-API-RUBY-CLIENT/5.3.0 + Content-Type: + - application/json + Authorization: + - Basic + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 201 + message: Created + headers: + Content-Type: + - application/json + Content-Length: + - '2242' + Connection: + - keep-alive + Date: + - Thu, 01 Sep 2022 09:19:10 GMT + Access-Control-Allow-Headers: + - Content-Type,Authorization + Access-Control-Allow-Methods: + - GET,PUT,POST,DELETE + Access-Control-Allow-Origin: + - "*" + Server: + - gunicorn + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + X-B3-Spanid: + - 4fe55c3cd719a55d + X-B3-Traceid: + - 52a23dfcd12ca7584fe55c3cd719a55d + X-Vcap-Request-Id: + - dc56074a-b5e0-4d46-62b0-8cdcb284d790 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 f5db034a9eef3b097715a6b5d2c824a8.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - LHR50-P3 + X-Amz-Cf-Id: + - Xs161xFAp3-cc9HeAlhcHDxn7mnCdsOtMW3ENKFv054pM7VmhrjavQ== + body: + encoding: UTF-8 + string: '{"content":{"body":"Dear Jane Doe\r\n\r\nYour registration number is + still CBDU1\r\n\r\n#As an upper tier waste carrier, broker and dealer:\r\n*you + need to renew your registration every 3 years\r\n*you\u2019ll need to pay + a renewal charge each time you renew\r\n*if we cannot reach you, you\u2019ll + have to pay for a new registration\r\n\r\n#There are some simple rules you + must follow:\r\n*if you give your waste to another person or business you + must check they are properly authorised to accept it, for example as a permitted + site or a registered waste carrier\r\n*make sure the correct documentation + is completed for each transfer of waste and that it correctly describes the + waste\r\n*make sure any waste is safely handled and stored\r\n*minimise the + environmental impact of waste by prioritising waste prevention, reuse, recycling + and recovery over disposal\r\n\r\nThe Environment Agency certify that the + following information is entered in the register which they maintain under + regulation 28 of the Waste (England and Wales) Regulations 2011.\r\n#Carrier + details\r\nName of registered carrier - Acme Waste\r\n\r\nRegistered as - An + upper tier waste carrier, broker and dealer\r\n\r\nRegistered address - 42, + Foo Gardens, Baz City, BS1 1AA\r\n\r\nTelephone number - 03708 506506\r\n\r\nDate + of registration - 1 September 2022\r\n\r\nExpiry date of registration - 1 + September 2023\r\n\r\n#Updating your details\r\nIf any of the details you\u2019ve + given us change, you must update them within 28 days by contacting the Environment + Agency.\r\n\r\nIf you have enquiries please email nccc-carrierbroker@environment-agency.gov.uk + or contact the Environment Agency helpline: 03708 506506\r\n\r\nYours sincerely\r\n\r\nWaste + carrier renewal team","subject":"Your registration as an upper tier carrier, + broker and dealer has been renewed"},"id":"46aaa3d3-db01-4d57-8ac4-0eee9006befb","reference":"CBDU1","scheduled_for":null,"template":{"id":"f703000e-1e76-4529-912d-966691578da0","uri":"https://api.notifications.service.gov.uk/services/25cb6b94-8ce7-485b-918a-559f3b18f69c/templates/f703000e-1e76-4529-912d-966691578da0","version":5},"uri":"https://api.notifications.service.gov.uk/v2/notifications/46aaa3d3-db01-4d57-8ac4-0eee9006befb"} + +' + recorded_at: Thu, 01 Sep 2022 09:19:10 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_business_name.yml b/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_business_name.yml new file mode 100644 index 000000000..039051861 --- /dev/null +++ b/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_business_name.yml @@ -0,0 +1,91 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.notifications.service.gov.uk/v2/notifications/letter + body: + encoding: UTF-8 + string: '{"template_id":"f703000e-1e76-4529-912d-966691578da0","reference":"CBDU1","personalisation":{"registration_type":"carrier, + broker and dealer","contact_name":"Jane Doe","reg_identifier":"CBDU1","company_name":"Acme + Waste","registered_address":"42, Foo Gardens, Baz City, BS1 1AA","phone_number":"03708 + 506506","date_registered":" 1 September 2022","expiry_date":" 1 September + 2023","address_line_1":"Jane Doe","address_line_2":"42","address_line_3":"Foo + Gardens","address_line_4":"Baz City","address_line_5":"BS1 1AA"}}' + headers: + User-Agent: + - NOTIFY-API-RUBY-CLIENT/5.3.0 + Content-Type: + - application/json + Authorization: + - Basic + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 201 + message: Created + headers: + Content-Type: + - application/json + Content-Length: + - '2242' + Connection: + - keep-alive + Date: + - Thu, 01 Sep 2022 10:31:43 GMT + Access-Control-Allow-Headers: + - Content-Type,Authorization + Access-Control-Allow-Methods: + - GET,PUT,POST,DELETE + Access-Control-Allow-Origin: + - "*" + Server: + - gunicorn + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + X-B3-Spanid: + - 4c625de0b6750d6b + X-B3-Traceid: + - 2b584578f03b25ca4c625de0b6750d6b + X-Vcap-Request-Id: + - 8ad292dd-f615-4b75-5ac3-cb546b2f3637 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 f1b5ae62d9afc4ed1ebb4ac99a508444.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - LHR50-C1 + X-Amz-Cf-Id: + - U3Mpsi9UK4KcrCpEzI-5UusvvqQy0No4UjJ1blyKz69qTUxy3xjUKw== + body: + encoding: UTF-8 + string: '{"content":{"body":"Dear Jane Doe\r\n\r\nYour registration number is + still CBDU1\r\n\r\n#As an upper tier waste carrier, broker and dealer:\r\n*you + need to renew your registration every 3 years\r\n*you\u2019ll need to pay + a renewal charge each time you renew\r\n*if we cannot reach you, you\u2019ll + have to pay for a new registration\r\n\r\n#There are some simple rules you + must follow:\r\n*if you give your waste to another person or business you + must check they are properly authorised to accept it, for example as a permitted + site or a registered waste carrier\r\n*make sure the correct documentation + is completed for each transfer of waste and that it correctly describes the + waste\r\n*make sure any waste is safely handled and stored\r\n*minimise the + environmental impact of waste by prioritising waste prevention, reuse, recycling + and recovery over disposal\r\n\r\nThe Environment Agency certify that the + following information is entered in the register which they maintain under + regulation 28 of the Waste (England and Wales) Regulations 2011.\r\n#Carrier + details\r\nName of registered carrier - Acme Waste\r\n\r\nRegistered as - An + upper tier waste carrier, broker and dealer\r\n\r\nRegistered address - 42, + Foo Gardens, Baz City, BS1 1AA\r\n\r\nTelephone number - 03708 506506\r\n\r\nDate + of registration - 1 September 2022\r\n\r\nExpiry date of registration - 1 + September 2023\r\n\r\n#Updating your details\r\nIf any of the details you\u2019ve + given us change, you must update them within 28 days by contacting the Environment + Agency.\r\n\r\nIf you have enquiries please email nccc-carrierbroker@environment-agency.gov.uk + or contact the Environment Agency helpline: 03708 506506\r\n\r\nYours sincerely\r\n\r\nWaste + carrier renewal team","subject":"Your registration as an upper tier carrier, + broker and dealer has been renewed"},"id":"19fe0d02-b1ff-4ef7-b19b-f9be1dd91fb1","reference":"CBDU1","scheduled_for":null,"template":{"id":"f703000e-1e76-4529-912d-966691578da0","uri":"https://api.notifications.service.gov.uk/services/25cb6b94-8ce7-485b-918a-559f3b18f69c/templates/f703000e-1e76-4529-912d-966691578da0","version":5},"uri":"https://api.notifications.service.gov.uk/v2/notifications/19fe0d02-b1ff-4ef7-b19b-f9be1dd91fb1"} + +' + recorded_at: Thu, 01 Sep 2022 10:31:43 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_registered_and_business_name.yml b/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_registered_and_business_name.yml new file mode 100644 index 000000000..08680426e --- /dev/null +++ b/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_registered_and_business_name.yml @@ -0,0 +1,91 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.notifications.service.gov.uk/v2/notifications/letter + body: + encoding: UTF-8 + string: '{"template_id":"f703000e-1e76-4529-912d-966691578da0","reference":"CBDU3","personalisation":{"registration_type":"carrier, + broker and dealer","contact_name":"Jane Doe","reg_identifier":"CBDU3","company_name":"Zenith + Limited trading as Acme Waste","registered_address":"42, Foo Gardens, Baz + City, BS1 1AA","phone_number":"03708 506506","date_registered":" 1 September + 2022","expiry_date":" 1 September 2023","address_line_1":"Jane Doe","address_line_2":"42","address_line_3":"Foo + Gardens","address_line_4":"Baz City","address_line_5":"BS1 1AA"}}' + headers: + User-Agent: + - NOTIFY-API-RUBY-CLIENT/5.3.0 + Content-Type: + - application/json + Authorization: + - Basic + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 201 + message: Created + headers: + Content-Type: + - application/json + Content-Length: + - '2268' + Connection: + - keep-alive + Date: + - Thu, 01 Sep 2022 10:31:44 GMT + Access-Control-Allow-Headers: + - Content-Type,Authorization + Access-Control-Allow-Methods: + - GET,PUT,POST,DELETE + Access-Control-Allow-Origin: + - "*" + Server: + - gunicorn + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + X-B3-Spanid: + - 3dfd7082777a6d87 + X-B3-Traceid: + - 353b00c3a2cc6eed3dfd7082777a6d87 + X-Vcap-Request-Id: + - 86ed0e29-8ea2-4420-426a-ef3f91941038 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 71f478e240c18306c2c4c6fa48fc6dca.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - LHR50-C1 + X-Amz-Cf-Id: + - 7240_gYsgBH7uZpQ8_B1tvIGiB-IM2y9O-uIufwruDwcWwIFn8uUDw== + body: + encoding: UTF-8 + string: '{"content":{"body":"Dear Jane Doe\r\n\r\nYour registration number is + still CBDU3\r\n\r\n#As an upper tier waste carrier, broker and dealer:\r\n*you + need to renew your registration every 3 years\r\n*you\u2019ll need to pay + a renewal charge each time you renew\r\n*if we cannot reach you, you\u2019ll + have to pay for a new registration\r\n\r\n#There are some simple rules you + must follow:\r\n*if you give your waste to another person or business you + must check they are properly authorised to accept it, for example as a permitted + site or a registered waste carrier\r\n*make sure the correct documentation + is completed for each transfer of waste and that it correctly describes the + waste\r\n*make sure any waste is safely handled and stored\r\n*minimise the + environmental impact of waste by prioritising waste prevention, reuse, recycling + and recovery over disposal\r\n\r\nThe Environment Agency certify that the + following information is entered in the register which they maintain under + regulation 28 of the Waste (England and Wales) Regulations 2011.\r\n#Carrier + details\r\nName of registered carrier - Zenith Limited trading as Acme Waste\r\n\r\nRegistered + as - An upper tier waste carrier, broker and dealer\r\n\r\nRegistered address + - 42, Foo Gardens, Baz City, BS1 1AA\r\n\r\nTelephone number - 03708 506506\r\n\r\nDate + of registration - 1 September 2022\r\n\r\nExpiry date of registration - 1 + September 2023\r\n\r\n#Updating your details\r\nIf any of the details you\u2019ve + given us change, you must update them within 28 days by contacting the Environment + Agency.\r\n\r\nIf you have enquiries please email nccc-carrierbroker@environment-agency.gov.uk + or contact the Environment Agency helpline: 03708 506506\r\n\r\nYours sincerely\r\n\r\nWaste + carrier renewal team","subject":"Your registration as an upper tier carrier, + broker and dealer has been renewed"},"id":"cce3d47a-6d08-443a-b000-aec30803c255","reference":"CBDU3","scheduled_for":null,"template":{"id":"f703000e-1e76-4529-912d-966691578da0","uri":"https://api.notifications.service.gov.uk/services/25cb6b94-8ce7-485b-918a-559f3b18f69c/templates/f703000e-1e76-4529-912d-966691578da0","version":5},"uri":"https://api.notifications.service.gov.uk/v2/notifications/cce3d47a-6d08-443a-b000-aec30803c255"} + +' + recorded_at: Thu, 01 Sep 2022 10:31:44 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_registered_name.yml b/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_registered_name.yml new file mode 100644 index 000000000..98eea7acc --- /dev/null +++ b/spec/cassettes/notify_upper_tier_ad_renewal_confirmation_letter_registered_name.yml @@ -0,0 +1,91 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.notifications.service.gov.uk/v2/notifications/letter + body: + encoding: UTF-8 + string: '{"template_id":"f703000e-1e76-4529-912d-966691578da0","reference":"CBDU4","personalisation":{"registration_type":"carrier, + broker and dealer","contact_name":"Jane Doe","reg_identifier":"CBDU4","company_name":"Zenith + Limited","registered_address":"42, Foo Gardens, Baz City, BS1 1AA","phone_number":"03708 + 506506","date_registered":" 1 September 2022","expiry_date":" 1 September + 2023","address_line_1":"Jane Doe","address_line_2":"42","address_line_3":"Foo + Gardens","address_line_4":"Baz City","address_line_5":"BS1 1AA"}}' + headers: + User-Agent: + - NOTIFY-API-RUBY-CLIENT/5.3.0 + Content-Type: + - application/json + Authorization: + - Basic + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 201 + message: Created + headers: + Content-Type: + - application/json + Content-Length: + - '2246' + Connection: + - keep-alive + Date: + - Thu, 01 Sep 2022 10:31:44 GMT + Access-Control-Allow-Headers: + - Content-Type,Authorization + Access-Control-Allow-Methods: + - GET,PUT,POST,DELETE + Access-Control-Allow-Origin: + - "*" + Server: + - gunicorn + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + X-B3-Spanid: + - 04eed8b20af340a3 + X-B3-Traceid: + - 4f56c7cb5caf989904eed8b20af340a3 + X-Vcap-Request-Id: + - f41d4cf2-3e28-45ed-5683-0f9686feb951 + X-Cache: + - Miss from cloudfront + Via: + - 1.1 cd9298825de9a9f64f66b3bc944bdd08.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - LHR50-C1 + X-Amz-Cf-Id: + - ox9zM5V4KQ666jb2UwwfiBKN7YV1o2J6tUsb2ADyHYIDNIKx0OaXNA== + body: + encoding: UTF-8 + string: '{"content":{"body":"Dear Jane Doe\r\n\r\nYour registration number is + still CBDU4\r\n\r\n#As an upper tier waste carrier, broker and dealer:\r\n*you + need to renew your registration every 3 years\r\n*you\u2019ll need to pay + a renewal charge each time you renew\r\n*if we cannot reach you, you\u2019ll + have to pay for a new registration\r\n\r\n#There are some simple rules you + must follow:\r\n*if you give your waste to another person or business you + must check they are properly authorised to accept it, for example as a permitted + site or a registered waste carrier\r\n*make sure the correct documentation + is completed for each transfer of waste and that it correctly describes the + waste\r\n*make sure any waste is safely handled and stored\r\n*minimise the + environmental impact of waste by prioritising waste prevention, reuse, recycling + and recovery over disposal\r\n\r\nThe Environment Agency certify that the + following information is entered in the register which they maintain under + regulation 28 of the Waste (England and Wales) Regulations 2011.\r\n#Carrier + details\r\nName of registered carrier - Zenith Limited\r\n\r\nRegistered as + - An upper tier waste carrier, broker and dealer\r\n\r\nRegistered address + - 42, Foo Gardens, Baz City, BS1 1AA\r\n\r\nTelephone number - 03708 506506\r\n\r\nDate + of registration - 1 September 2022\r\n\r\nExpiry date of registration - 1 + September 2023\r\n\r\n#Updating your details\r\nIf any of the details you\u2019ve + given us change, you must update them within 28 days by contacting the Environment + Agency.\r\n\r\nIf you have enquiries please email nccc-carrierbroker@environment-agency.gov.uk + or contact the Environment Agency helpline: 03708 506506\r\n\r\nYours sincerely\r\n\r\nWaste + carrier renewal team","subject":"Your registration as an upper tier carrier, + broker and dealer has been renewed"},"id":"9634c46e-d360-497c-9747-7ac1dfc17ce5","reference":"CBDU4","scheduled_for":null,"template":{"id":"f703000e-1e76-4529-912d-966691578da0","uri":"https://api.notifications.service.gov.uk/services/25cb6b94-8ce7-485b-918a-559f3b18f69c/templates/f703000e-1e76-4529-912d-966691578da0","version":5},"uri":"https://api.notifications.service.gov.uk/v2/notifications/9634c46e-d360-497c-9747-7ac1dfc17ce5"} + +' + recorded_at: Thu, 01 Sep 2022 10:31:44 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/services/waste_carriers_engine/notify/renewal_confirmation_letter_service_spec.rb b/spec/services/waste_carriers_engine/notify/renewal_confirmation_letter_service_spec.rb new file mode 100644 index 000000000..a305391e3 --- /dev/null +++ b/spec/services/waste_carriers_engine/notify/renewal_confirmation_letter_service_spec.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +require "rails_helper" + +module WasteCarriersEngine + module Notify + RSpec.describe RenewalConfirmationLetterService do + before do + # Make sure its a real postcode for Notify validation purposes + allow_any_instance_of(WasteCarriersEngine::Address).to receive(:postcode).and_return("BS1 1AA") + end + + context "upper tier" do + describe ".run" do + let(:registration) { create(:registration, :has_required_data, expires_on: 1.year.from_now) } + let(:template_id) { "f703000e-1e76-4529-912d-966691578da0" } + + let(:company_name) { "Acme Waste" } + let(:registered_company_name) { "Zenith Limited" } + let(:presentation_name) { "#{registered_company_name} trading as #{company_name}" } + let(:expected_company_name) { company_name } + let(:company_name_regex) { /Name of registered carrier[^\w]*#{expected_company_name}\W/ } + let(:expected_notify_options) do + { + template_id: template_id, + reference: registration.reg_identifier, + personalisation: { + contact_name: "Jane Doe", + registration_type: "carrier, broker and dealer", + reg_identifier: registration.reg_identifier, + company_name: expected_company_name, + registered_address: "42, Foo Gardens, Baz City, BS1 1AA", + phone_number: "03708 506506", + date_registered: registration.metaData.date_registered.strftime("%e %B %Y"), + expiry_date: registration.expires_on.in_time_zone("London").to_date.strftime("%e %B %Y"), + address_line_1: "Jane Doe", + address_line_2: "42", + address_line_3: "Foo Gardens", + address_line_4: "Baz City", + address_line_5: "BS1 1AA" + } + } + end + + let(:cassette_name) { "notify_upper_tier_ad_renewal_confirmation_letter_business_name" } + subject do + VCR.use_cassette(cassette_name) do + described_class.run(registration: registration) + end + end + + before do + expect_any_instance_of(Notifications::Client) + .to receive(:send_letter) + .with(expected_notify_options) + .and_call_original + end + + it "sends a letter" do + expect(subject).to be_a(Notifications::Client::ResponseNotification) + expect(subject.template["id"]).to eq(template_id) + expect(subject.reference).to match(/CBDU*/) + expect(subject.content["subject"]).to eq( + "Your registration as an upper tier carrier, broker and dealer has been renewed" + ) + end + + context "without a registered company name" do + let(:expected_company_name) { company_name } + let(:cassette_name) { "notify_upper_tier_ad_renewal_confirmation_letter_business_name" } + + it "includes only the business name" do + expect(subject.content["body"]).to match(company_name_regex) + end + end + + context "with a registered company name" do + before do + registration.registered_company_name = registered_company_name + end + + context "without a business name" do + let(:expected_company_name) { registered_company_name } + let(:cassette_name) { "notify_upper_tier_ad_renewal_confirmation_letter_registered_name" } + + before do + registration.company_name = nil + end + + it "includes only the business name" do + expect(subject.content["body"]).to match(company_name_regex) + end + end + + context "with a business name" do + let(:expected_company_name) { "#{registered_company_name} trading as #{company_name}" } + let(:cassette_name) { "notify_upper_tier_ad_renewal_confirmation_letter_registered_and_business_name" } + + it "includes the presentation name" do + expect(subject.content["body"]).to match(company_name_regex) + end + end + end + end + end + end + end +end diff --git a/spec/services/waste_carriers_engine/renewal_completion_service_spec.rb b/spec/services/waste_carriers_engine/renewal_completion_service_spec.rb index f5bc3ee66..dd46ae9a3 100644 --- a/spec/services/waste_carriers_engine/renewal_completion_service_spec.rb +++ b/spec/services/waste_carriers_engine/renewal_completion_service_spec.rb @@ -198,6 +198,21 @@ module WasteCarriersEngine renewal_completion_service.complete_renewal end + + context "when there is no contact email" do + before do + transient_registration.update_attributes(contact_email: nil) + end + + it "sends a confirmation letter" do + expect(Notify::RenewalConfirmationLetterService) + .to receive(:run) + .with(registration: registration) + .once + + renewal_completion_service.complete_renewal + end + end end context "when the renewal cannot be completed" do