Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/letter for renewal confirmation #1258

Merged
merged 5 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading