Skip to content
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
17 changes: 13 additions & 4 deletions app/jobs/get_usps_proofing_results_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ def handle_unsupported_id_type(enrollment, response)
status_check_completed_at: Time.zone.now,
)

# send SMS and email
send_enrollment_status_sms_notification(enrollment: enrollment)
send_failed_email(enrollment.user, enrollment)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
**email_analytics_attributes(enrollment),
Expand Down Expand Up @@ -244,8 +246,10 @@ def handle_expired_status_update(enrollment, response, response_message)
status: :expired,
status_check_completed_at: Time.zone.now,
)
# destroy phone number for expired

# destroy phone number for expired enrollments
enrollment.notification_phone_configuration&.destroy

begin
send_deadline_passed_email(enrollment.user, enrollment) unless enrollment.deadline_passed_sent
rescue StandardError => err
Expand Down Expand Up @@ -309,6 +313,9 @@ def handle_failed_status(enrollment, response)
proofed_at: proofed_at,
status_check_completed_at: Time.zone.now,
)

# send SMS and email
send_enrollment_status_sms_notification(enrollment: enrollment)
if response['fraudSuspected']
send_failed_fraud_email(enrollment.user, enrollment)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
Expand Down Expand Up @@ -342,6 +349,9 @@ def handle_successful_status_update(enrollment, response)
proofed_at: proofed_at,
status_check_completed_at: Time.zone.now,
)

# send SMS and email
send_enrollment_status_sms_notification(enrollment: enrollment)
send_verified_email(enrollment.user, enrollment)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
**email_analytics_attributes(enrollment),
Expand All @@ -365,6 +375,8 @@ def handle_unsupported_secondary_id(enrollment, response)
proofed_at: proofed_at,
status_check_completed_at: Time.zone.now,
)
# send SMS and email
send_enrollment_status_sms_notification(enrollment: enrollment)
send_failed_email(enrollment.user, enrollment)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
**email_analytics_attributes(enrollment),
Expand Down Expand Up @@ -393,9 +405,6 @@ def process_enrollment_response(enrollment, response)
else
handle_unsupported_status(enrollment, response)
end

# invoke job to send sms notification
send_enrollment_status_sms_notification(enrollment: enrollment)
end

def send_verified_email(user, enrollment)
Expand Down
121 changes: 60 additions & 61 deletions app/jobs/in_person/send_proofing_notification_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,74 @@ class SendProofingNotificationJob < ApplicationJob
def perform(enrollment_id)
return unless IdentityConfig.store.in_person_proofing_enabled &&
IdentityConfig.store.in_person_send_proofing_notifications_enabled
begin
enrollment = InPersonEnrollment.find_by(
{ id: enrollment_id },
include: [:notification_phone_configuration, :user],
)
return unless enrollment
# skip when enrollment status not success/failed/expired and no phone configured
if enrollment.skip_notification_sent_at_set?
# log event
analytics(user: enrollment.user).
idv_in_person_usps_proofing_results_notification_job_skipped(
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,
)
return
end
analytics(user: enrollment.user).
idv_in_person_usps_proofing_results_notification_job_started(
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,

enrollment = InPersonEnrollment.find_by(
{ id: enrollment_id },
include: [:notification_phone_configuration, :user],
)

if enrollment.nil? || !enrollment.eligible_for_notification?
analytics(user: enrollment&.user || AnonymousUser.new).
idv_in_person_send_proofing_notification_job_skipped(
enrollment_code: enrollment&.enrollment_code,
enrollment_id: enrollment_id,
)
if enrollment.expired?
# no sending message for expired status
enrollment.notification_phone_configuration&.destroy
return
end
return
end

# only send sms when success or failed
# send notification and log result
phone = enrollment.notification_phone_configuration.formatted_phone
message = notification_message(enrollment: enrollment)
response = Telephony.send_notification(
to: phone, message: message,
country_code: Phonelib.parse(phone).country
analytics(user: enrollment.user).
idv_in_person_send_proofing_notification_job_started(
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,
)
handle_telephony_result(enrollment: enrollment, phone: phone, telephony_result: response)
# if notification sent successful
enrollment.update(notification_sent_at: Time.zone.now) if response.success?
ensure
Rails.logger.error("Unknown enrollment with id #{enrollment_id}") unless enrollment.present?
analytics(user: enrollment.present? ? enrollment.user : AnonymousUser.new).
idv_in_person_usps_proofing_results_notification_job_completed(
enrollment_code: enrollment&.enrollment_code, enrollment_id: enrollment_id,
)
if enrollment.expired?
# no sending message for expired status
enrollment.notification_phone_configuration&.destroy
log_job_completed(enrollment: enrollment)
return
end

# send notification and log result when success or failed
phone = enrollment.notification_phone_configuration.formatted_phone
message = notification_message(enrollment: enrollment)
response = Telephony.send_notification(
to: phone, message: message,
country_code: Phonelib.parse(phone).country
)
handle_telephony_response(enrollment: enrollment, phone: phone, telephony_response: response)

enrollment.update(notification_sent_at: Time.zone.now) if response.success?

log_job_completed(enrollment: enrollment)
rescue StandardError => err
analytics(user: enrollment&.user || AnonymousUser.new).
idv_in_person_send_proofing_notification_job_exception(
enrollment_code: enrollment&.code,
enrollment_id: enrollment_id,
exception_class: err.class.to_s,
exception_message: err.message,
)
end

private

def handle_telephony_result(enrollment:, phone:, telephony_result:)
if telephony_result.success?
analytics(user: enrollment.user).
idv_in_person_usps_proofing_results_notification_sent_attempted(
success: true,
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,
telephony_result: telephony_result,
)
else
analytics(user: enrollment.user).
idv_in_person_usps_proofing_results_notification_sent_attempted(
success: false,
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,
telephony_result: telephony_result,
)
if telephony_result.error&.is_a?(Telephony::OptOutError)
PhoneNumberOptOut.mark_opted_out(phone)
end
def log_job_completed(enrollment:)
analytics(user: enrollment.user).
idv_in_person_send_proofing_notification_job_completed(
enrollment_code: enrollment.enrollment_code, enrollment_id: enrollment.id,
)
end

def handle_telephony_response(enrollment:, phone:, telephony_response:)
analytics(user: enrollment.user).
idv_in_person_send_proofing_notification_attempted(
success: telephony_response.success?,
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,
telephony_response: telephony_response.to_h,
)
if telephony_response.error&.is_a?(Telephony::OptOutError)
PhoneNumberOptOut.mark_opted_out(phone)
end
end

Expand Down
5 changes: 3 additions & 2 deletions app/models/in_person_enrollment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ def on_notification_sent_at_updated
end
end

def skip_notification_sent_at_set?
!notification_phone_configuration.present? || (!self.passed? && !self.failed? && !self.expired?)
def eligible_for_notification?
self.notification_phone_configuration.present? &&
(self.passed? || self.failed? || self.expired?)
end

private
Expand Down
167 changes: 97 additions & 70 deletions app/services/analytics_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,103 @@ def idv_in_person_ready_to_verify_what_to_bring_link_clicked(**extra)
)
end

# Track sms notification attempt
# @param [boolean] success sms notification successful or not
# @param [String] enrollment_code enrollment_code
# @param [String] enrollment_id enrollment_id
# @param [Hash] telephony_response response from Telephony gem
# @param [Hash] extra extra information
def idv_in_person_send_proofing_notification_attempted(
success:,
enrollment_code:,
enrollment_id:,
telephony_response:,
**extra
)
track_event(
'IdV: in person notification SMS send attempted',
success: success,
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
telephony_response: telephony_response,
**extra,
)
end

# Track sms notification job completion
# @param [String] enrollment_code enrollment_code
# @param [String] enrollment_id enrollment_id
# @param [Hash] extra extra information
def idv_in_person_send_proofing_notification_job_completed(
enrollment_code:,
enrollment_id:,
**extra
)
track_event(
'SendProofingNotificationAndDeletePhoneNumberJob: job completed',
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
**extra,
)
end

# Tracks exceptions that are raised when running InPerson::SendProofingNotificationJob
# @param [String] enrollment_code
# @param [String] enrollment_id
# @param [String] exception_class
# @param [String] exception_message
# @param [Hash] extra extra information
def idv_in_person_send_proofing_notification_job_exception(
enrollment_code:,
enrollment_id:,
exception_class: nil,
exception_message: nil,
**extra
)
track_event(
'SendProofingNotificationJob: Exception raised',
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
exception_class: exception_class,
exception_message: exception_message,
**extra,
)
end

# Track sms notification job skipped
# @param [String] enrollment_code enrollment_code
# @param [String] enrollment_id enrollment_id
# @param [Hash] extra extra information
def idv_in_person_send_proofing_notification_job_skipped(
enrollment_code:,
enrollment_id:,
**extra
)
track_event(
'SendProofingNotificationAndDeletePhoneNumberJob: job skipped',
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
**extra,
)
end

# Track sms notification job started
# @param [String] enrollment_code enrollment_code
# @param [String] enrollment_id enrollment_id
# @param [Hash] extra extra information
def idv_in_person_send_proofing_notification_job_started(
enrollment_code:,
enrollment_id:,
**extra
)
track_event(
'SendProofingNotificationAndDeletePhoneNumberJob: job started',
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
**extra,
)
end

# @param [String] flow_path Document capture path ("hybrid" or "standard")
# The user submitted the in person proofing switch_back step
def idv_in_person_switch_back_submitted(flow_path:, **extra)
Expand Down Expand Up @@ -1722,76 +1819,6 @@ def idv_in_person_usps_proofing_results_job_unexpected_response(
)
end

# Track sms notification job completion
# @param [String] enrollment_code enrollment_code
# @param [String] enrollment_id enrollment_id
# @param [Hash] extra extra information
def idv_in_person_usps_proofing_results_notification_job_completed(enrollment_code:,
enrollment_id:,
**extra)
track_event(
'SendProofingNotificationAndDeletePhoneNumberJob: job completed',
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
**extra,
)
end

# Track sms notification job skipped
# @param [String] enrollment_code enrollment_code
# @param [String] enrollment_id enrollment_id
# @param [Hash] extra extra information
def idv_in_person_usps_proofing_results_notification_job_skipped(
enrollment_code:,
enrollment_id:,
**extra
)
track_event(
'SendProofingNotificationAndDeletePhoneNumberJob: job skipped',
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
**extra,
)
end

# Track sms notification job started
# @param [String] enrollment_code enrollment_code
# @param [String] enrollment_id enrollment_id
# @param [Hash] extra extra information
def idv_in_person_usps_proofing_results_notification_job_started(enrollment_code:,
enrollment_id:,
**extra)
track_event(
'SendProofingNotificationAndDeletePhoneNumberJob: job started',
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
**extra,
)
end

# Track sms notification attempt
# @param [boolean] success sms notification successful or not
# @param [String] enrollment_code enrollment_code
# @param [String] enrollment_id enrollment_id
# @param [Telephony::Response] telephony_result
# @param [Hash] extra extra information
def idv_in_person_usps_proofing_results_notification_sent_attempted(
success:,
enrollment_code:,
enrollment_id:,
telephony_result:,
**extra
)
track_event(
'IdV: in person notification SMS send attempted',
success: success,
enrollment_code: enrollment_code,
enrollment_id: enrollment_id,
telephony_result: telephony_result,
**extra,
)
end

# Tracks if USPS in-person proofing enrollment request fails
# @param [String] context
# @param [String] reason
Expand Down
1 change: 0 additions & 1 deletion config/application.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ in_person_email_reminder_early_benchmark_in_days: 11
in_person_email_reminder_final_benchmark_in_days: 1
in_person_email_reminder_late_benchmark_in_days: 4
in_person_proofing_enabled: false
in_person_send_proofing_notifications_enabled: false
in_person_enrollment_validity_in_days: 30
in_person_enrollments_ready_job_email_body_pattern: '\A\s*(?<enrollment_code>\d{16})\s*\Z'
in_person_enrollments_ready_job_cron: '0/10 * * * *'
Expand Down
Loading