Skip to content

Commit

Permalink
Feature/letter for renewal confirmation (#1258)
Browse files Browse the repository at this point in the history
* Adding in CH API down error handling

* New service for sending renewal confirmation letters and specs
  • Loading branch information
Beckyrose200 committed Sep 1, 2022
1 parent 94c0f1b commit a755f7a
Show file tree
Hide file tree
Showing 8 changed files with 583 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
15 changes: 14 additions & 1 deletion app/services/waste_carriers_engine/renewal_completion_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a755f7a

Please sign in to comment.