Skip to content
Merged
21 changes: 21 additions & 0 deletions app/jobs/get_usps_proofing_results_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class GetUspsProofingResultsJob < ApplicationJob

discard_on GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError

def email_analytics_attributes(enrollment)
{
timestamp: Time.zone.now,
user_id: enrollment.user_id,
service_provider: enrollment.issuer,
delay_time_seconds: mail_delivery_params[:wait],
}
end

def enrollment_analytics_attributes(enrollment, complete:)
{
enrollment_code: enrollment.enrollment_code,
Expand Down Expand Up @@ -220,8 +229,16 @@ def handle_failed_status(enrollment, response)
enrollment.update(status: :failed)
if response['fraudSuspected']
send_failed_fraud_email(enrollment.user, enrollment)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
**email_analytics_attributes(enrollment),
email_type: 'Failed fraud suspected',
)
else
send_failed_email(enrollment.user, enrollment)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
**email_analytics_attributes(enrollment),
email_type: 'Failed',
)
end
end

Expand All @@ -234,6 +251,10 @@ def handle_successful_status_update(enrollment, response)
passed: true,
reason: 'Successful status update',
)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
**email_analytics_attributes(enrollment),
email_type: 'Success',
)
enrollment.profile.activate
enrollment.update(status: :passed)
send_verified_email(enrollment.user, enrollment)
Expand Down
13 changes: 13 additions & 0 deletions app/services/analytics_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,19 @@ def idv_in_person_usps_proofing_results_job_enrollment_updated(
)
end

# Tracks emails that are initiated during GetUspsProofingResultsJob
# @param [String] email_type success, failed or failed fraud
def idv_in_person_usps_proofing_results_job_email_initiated(
email_type:,
**extra
)
track_event(
'GetUspsProofingResultsJob: Success or failure email initiated',
email_type: email_type,
**extra,
)
end

# Tracks users visiting the recovery options page
def account_reset_recovery_options_visit
track_event('Account Reset: Recovery Options Visited')
Expand Down
52 changes: 52 additions & 0 deletions spec/jobs/get_usps_proofing_results_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@
)
end

context 'email_analytics_attributes' do
before(:each) do
stub_request_passed_proofing_results
end
it 'logs message with email analytics attributes' do
freeze_time do
job.perform(Time.zone.now)
expect(job_analytics).to have_logged_event(
'GetUspsProofingResultsJob: Success or failure email initiated',
timestamp: Time.zone.now,
user_id: pending_enrollment.user_id,
service_provider: pending_enrollment.issuer,
delay_time_seconds: 3600,
)
end
end
end

Comment on lines +41 to +58
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally email_analytics_attributes seems to be a good candidate for a private method, since we're primarily concerned with each of the specific email types, and the helper method is an implementation detail that we could choose to refactor or remove altogether. As a private method, I don't think we'd need to test it at all in isolation like this, but I'd suggest enhancing the assertions below for the specific email types to ensure they include all the details we're looking for.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i separated out the email attributes from the earlier test for job analytics so I could use a completed enrollment rather then the pending enrollment used for the test for the other job analytics.

it 'updates the status of the enrollment and profile appropriately' do
freeze_time do
pending_enrollment.update(
Expand Down Expand Up @@ -407,6 +425,10 @@
'GetUspsProofingResultsJob: Enrollment status updated',
reason: 'Successful status update',
)
expect(job_analytics).to have_logged_event(
'GetUspsProofingResultsJob: Success or failure email initiated',
email_type: 'Success',
)
end
end

Expand All @@ -429,6 +451,36 @@
expect(job_analytics).to have_logged_event(
'GetUspsProofingResultsJob: Enrollment status updated',
)
expect(job_analytics).to have_logged_event(
'GetUspsProofingResultsJob: Success or failure email initiated',
email_type: 'Failed',
)
end
end

context 'when an enrollment fails and fraud is suspected' do
before(:each) do
stub_request_failed_suspected_fraud_proofing_results
end

it_behaves_like(
'enrollment with a status update',
passed: false,
status: 'failed',
response_json: UspsInPersonProofing::Mock::Fixtures.
request_failed_suspected_fraud_proofing_results_response,
)

it 'logs fraud failure details' do
job.perform(Time.zone.now)

expect(job_analytics).to have_logged_event(
'GetUspsProofingResultsJob: Enrollment status updated',
)
expect(job_analytics).to have_logged_event(
'GetUspsProofingResultsJob: Success or failure email initiated',
email_type: 'Failed fraud suspected',
)
end
end

Expand Down