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
26 changes: 22 additions & 4 deletions app/jobs/get_usps_proofing_results_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def enrollment_analytics_attributes(enrollment, complete:)
enrollment_code: enrollment.enrollment_code,
enrollment_id: enrollment.id,
minutes_since_last_status_check: enrollment.minutes_since_last_status_check,
minutes_since_last_status_check_completed:
enrollment.minutes_since_last_status_check_completed,
minutes_since_last_status_update: enrollment.minutes_since_last_status_update,
minutes_since_established: enrollment.minutes_since_established,
minutes_to_completion: complete ? enrollment.minutes_since_established : nil,
Expand Down Expand Up @@ -157,6 +159,7 @@ def handle_bad_request_error(err, enrollment)
**enrollment_analytics_attributes(enrollment, complete: false),
response_message: response_message,
)
enrollment.update(status_check_completed_at: Time.zone.now)
elsif response_message&.match(IPP_EXPIRED_ERROR_MESSAGE)
handle_expired_status_update(enrollment, err.response, response_message)
elsif response_message == IPP_INVALID_ENROLLMENT_CODE_MESSAGE % enrollment.enrollment_code
Expand Down Expand Up @@ -248,7 +251,11 @@ def handle_unsupported_id_type(enrollment, response)
primary_id_type: response['primaryIdType'],
reason: 'Unsupported ID type',
)
enrollment.update(status: :failed, proofed_at: proofed_at)
enrollment.update(
status: :failed,
proofed_at: proofed_at,
status_check_completed_at: Time.zone.now,
)

send_failed_email(enrollment.user, enrollment)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
Expand All @@ -265,7 +272,10 @@ def handle_expired_status_update(enrollment, response, response_message)
passed: false,
reason: 'Enrollment has expired',
)
enrollment.update(status: :expired)
enrollment.update(
status: :expired,
status_check_completed_at: Time.zone.now,
)

begin
send_deadline_passed_email(enrollment.user, enrollment) unless enrollment.deadline_passed_sent
Expand Down Expand Up @@ -321,7 +331,11 @@ def handle_failed_status(enrollment, response)
reason: 'Failed status',
)

enrollment.update(status: :failed, proofed_at: proofed_at)
enrollment.update(
status: :failed,
proofed_at: proofed_at,
status_check_completed_at: Time.zone.now,
)
if response['fraudSuspected']
send_failed_fraud_email(enrollment.user, enrollment)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
Expand All @@ -347,7 +361,11 @@ def handle_successful_status_update(enrollment, response)
reason: 'Successful status update',
)
enrollment.profile.activate
enrollment.update(status: :passed, proofed_at: proofed_at)
enrollment.update(
status: :passed,
proofed_at: proofed_at,
status_check_completed_at: Time.zone.now,
)
analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_email_initiated(
**email_analytics_attributes(enrollment),
email_type: 'Success',
Expand Down
5 changes: 5 additions & 0 deletions app/models/in_person_enrollment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def minutes_since_last_status_check
(Time.zone.now - status_check_attempted_at).seconds.in_minutes.round(2)
end

def minutes_since_last_status_check_completed
return unless status_check_completed_at.present?
(Time.zone.now - status_check_completed_at).seconds.in_minutes.round(2)
end

def minutes_since_last_status_update
return unless status_updated_at.present?
(Time.zone.now - status_updated_at).seconds.in_minutes.round(2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddStatusCheckCompletedAtToInPersonEnrollments < ActiveRecord::Migration[7.0]
def change
add_column :in_person_enrollments, :status_check_completed_at, :datetime,
comment: 'The last time a status check was successfully completed'
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_03_22_000756) do
ActiveRecord::Schema[7.0].define(version: 2023_04_03_232935) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "pgcrypto"
Expand Down Expand Up @@ -310,6 +310,7 @@
t.boolean "deadline_passed_sent", default: false, comment: "deadline passed email sent for expired enrollment"
t.datetime "proofed_at", precision: nil, comment: "timestamp when user attempted to proof at a Post Office"
t.boolean "capture_secondary_id_enabled", default: false, comment: "record and proof state ID and residential addresses separately"
t.datetime "status_check_completed_at", comment: "The last time a status check was successfully completed"
t.index ["profile_id"], name: "index_in_person_enrollments_on_profile_id"
t.index ["status_check_attempted_at"], name: "index_in_person_enrollments_on_status_check_attempted_at", where: "(status = 1)"
t.index ["unique_id"], name: "index_in_person_enrollments_on_unique_id", unique: true
Expand Down
44 changes: 42 additions & 2 deletions spec/jobs/get_usps_proofing_results_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
pending_enrollment.update(
enrollment_established_at: Time.zone.now - 3.days,
status_check_attempted_at: Time.zone.now - 15.minutes,
status_check_completed_at: Time.zone.now - 17.minutes,
status_updated_at: Time.zone.now - 2.days,
)

Expand All @@ -21,6 +22,7 @@
fraud_suspected: response['fraudSuspected'],
issuer: pending_enrollment.issuer,
minutes_since_last_status_check: 15.0,
minutes_since_last_status_check_completed: 17.0,
minutes_since_last_status_update: 2.days.in_minutes,
minutes_to_completion: 3.days.in_minutes,
minutes_since_established: 3.days.in_minutes,
Expand Down Expand Up @@ -72,6 +74,7 @@
pending_enrollment.reload
expect(pending_enrollment.status_updated_at).to eq(Time.zone.now)
expect(pending_enrollment.status_check_attempted_at).to eq(Time.zone.now)
expect(pending_enrollment.status_check_completed_at).to eq(Time.zone.now)
expect(pending_enrollment.status).to eq(status)
expect(pending_enrollment.profile.active).to eq(passed)
end
Expand Down Expand Up @@ -116,6 +119,20 @@
expect(pending_enrollment.status_check_attempted_at).to eq(Time.zone.now)
end
end

it 'does not update the status_check_completed_at timestamp' do
freeze_time do
pending_enrollment.update(
status_check_attempted_at: Time.zone.now - 1.day,
status_updated_at: Time.zone.now - 2.days,
)
job.perform(Time.zone.now)

pending_enrollment.reload
expect(pending_enrollment.status_updated_at).to eq(Time.zone.now - 2.days)
expect(pending_enrollment.status_check_completed_at).to be_nil
end
end
end

RSpec.shared_examples 'enrollment_encountering_an_error_that_has_a_nil_response' do |error_type:|
Expand All @@ -132,6 +149,14 @@
),
)
end

it 'does not update the status_check_completed_at timestamp' do
freeze_time do
job.perform(Time.zone.now)
pending_enrollment.reload
expect(pending_enrollment.status_check_completed_at).to be_nil
end
end
end

RSpec.describe GetUspsProofingResultsJob do
Expand Down Expand Up @@ -227,7 +252,12 @@

expect(pending_enrollments.pluck(:status_check_attempted_at)).to(
all(eq nil),
'failed test precondition: pending enrollments must not have status check time set',
'failed test precondition: pending enrollments must not set status check attempted time',
)

expect(pending_enrollments.pluck(:status_check_completed_at)).to(
all(eq nil),
'failed test precondition: pending enrollments must not set status check completed time',
)

freeze_time do
Expand All @@ -239,7 +269,16 @@
pluck(:status_check_attempted_at),
).to(
all(eq Time.zone.now),
'job must update status check time for all pending enrollments',
'job must update status check attempted time for all pending enrollments',
)

expect(
pending_enrollments.
map(&:reload).
pluck(:status_check_completed_at),
).to(
all(eq Time.zone.now),
'job must update status check completed time for all pending enrollments',
)
end
end
Expand Down Expand Up @@ -885,6 +924,7 @@
expect(pending_enrollment.enrollment_established_at).to eq(Time.zone.now - 3.days)
expect(pending_enrollment.status_updated_at).to eq(Time.zone.now - 1.day)
expect(pending_enrollment.status_check_attempted_at).to eq(Time.zone.now)
expect(pending_enrollment.status_check_completed_at).to eq(Time.zone.now)
end

expect(pending_enrollment.profile.active).to eq(false)
Expand Down
18 changes: 18 additions & 0 deletions spec/models/in_person_enrollment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@
end
end

describe 'minutes_since_last_status_check_completed' do
let(:enrollment) do
create(
:in_person_enrollment, :passed, status_check_completed_at: Time.zone.now - 2.hours
)
end

it 'returns number of minutes since last status check was completed' do
expect(enrollment.minutes_since_last_status_check_completed).to be_within(0.01).of(120)
end

it 'returns nil if enrollment has not completed a status check' do
enrollment.status_check_completed_at = nil

expect(enrollment.minutes_since_last_status_check_completed).to eq(nil)
end
end

describe 'minutes_since_status_updated' do
let(:enrollment) do
enrollment = create(:in_person_enrollment, :passed)
Expand Down