-
Notifications
You must be signed in to change notification settings - Fork 166
LG-11520: Add job to expire old GPO pending profiles #9545
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5441fe4
Migration to add profiles.gpo_verification_expired_at
matthinz e9a802c
Add Profile#deactivate_due_to_gpo_expiration
matthinz b5b7953
Allow setting code_sent_at on GpoConfirmationCode factories
matthinz 08a9e9d
Add job to expire GPO profiles that can't be verified
matthinz 0ec2fdf
Add `limit:` arg to GpoExpirationJob.perform
matthinz 28961da
Remove old column from schema.rb
matthinz e519e46
Merge branch 'main' into matthinz/gpo-expiration
matthinz a56e97a
`as_of` -> `now` in #perform
matthinz 82eb6b1
Use a hash to hold user fixtures
matthinz 6e49b41
Add gpo_verification_pending_at to analytics event
matthinz e8f01e5
Add min_profile_age parameter
matthinz 31f6604
Add Rake task for profile expiration backfill
matthinz c032fda
Merge branch 'main' into matthinz/gpo-expiration
matthinz f0d3e77
Use `Profile.all` instead of `Profile.where('1=1')`
matthinz 612e725
Fix linter issues in rake file
matthinz 2440bae
Use be_within to address CI failure
matthinz e7ebd28
Provide a little progress output during rake task
matthinz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| class GpoExpirationJob < ApplicationJob | ||
| queue_as :low | ||
|
|
||
| def initialize(analytics: nil) | ||
| @analytics = analytics | ||
| end | ||
|
|
||
| def expire_profile(profile:) | ||
| gpo_verification_pending_at = profile.gpo_verification_pending_at | ||
|
|
||
| profile.deactivate_due_to_gpo_expiration | ||
|
|
||
| analytics.idv_gpo_expired( | ||
| user_id: profile.user.uuid, | ||
| user_has_active_profile: profile.user.active_profile.present?, | ||
| letters_sent: profile.gpo_confirmation_codes.count, | ||
| gpo_verification_pending_at: gpo_verification_pending_at, | ||
| ) | ||
| end | ||
|
|
||
| def perform(now: Time.zone.now, limit: nil, min_profile_age: nil) | ||
| profiles = gpo_profiles_that_should_be_expired(as_of: now, min_profile_age: min_profile_age) | ||
|
|
||
| if limit.present? | ||
| profiles = profiles.limit(limit) | ||
| end | ||
|
|
||
| profiles.find_each do |profile| | ||
| expire_profile(profile: profile) | ||
| end | ||
| end | ||
|
|
||
| def gpo_profiles_that_should_be_expired(as_of:, min_profile_age: nil) | ||
| Profile. | ||
| and(are_pending_gpo_verification). | ||
| and(user_cant_request_more_letters(as_of: as_of)). | ||
| and(most_recent_code_has_expired(as_of: as_of)). | ||
| and(are_old_enough(as_of: as_of, min_profile_age: min_profile_age)) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def analytics | ||
| @analytics ||= Analytics.new(user: AnonymousUser.new, request: nil, session: {}, sp: nil) | ||
| end | ||
|
|
||
| def are_old_enough(as_of:, min_profile_age:) | ||
| return Profile.all if min_profile_age.blank? | ||
|
|
||
| max_created_at = as_of - min_profile_age | ||
|
|
||
| return Profile.where(created_at: ..max_created_at) | ||
| end | ||
|
|
||
| def are_pending_gpo_verification | ||
| Profile.where.not(gpo_verification_pending_at: nil) | ||
| end | ||
|
|
||
| def most_recent_code_has_expired(as_of:) | ||
| # Any Profile where the most recent code was sent *before* | ||
| # usps_confirmation_max_days days ago is now expired | ||
| max_code_sent_at = as_of - IdentityConfig.store.usps_confirmation_max_days.days | ||
|
|
||
| Profile.where( | ||
| id: GpoConfirmationCode. | ||
| select(:profile_id). | ||
| group(:profile_id). | ||
| having('max(code_sent_at) < ?', max_code_sent_at), | ||
| ) | ||
| end | ||
|
|
||
| def user_cant_request_more_letters(as_of:) | ||
| max_created_at = as_of - IdentityConfig.store.gpo_max_profile_age_to_send_letter_in_days.days | ||
| Profile.where(created_at: [..max_created_at]) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
db/primary_migrate/20231102211426_add_gpo_verification_expired_at_to_profiles.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class AddGpoVerificationExpiredAtToProfiles < ActiveRecord::Migration[7.1] | ||
| disable_ddl_transaction! | ||
|
|
||
| def change | ||
| add_column :profiles, :gpo_verification_expired_at, :datetime | ||
matthinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| add_index :profiles, :gpo_verification_expired_at, algorithm: :concurrently | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| namespace :profiles do | ||
| desc 'Backfill the gpo_verification_expired_at value' | ||
|
|
||
| ## | ||
| # Usage: | ||
| # | ||
| # Print pending updates | ||
| # bundle exec rake profiles:backfill_gpo_expiration > profiles.csv | ||
| # | ||
| # Commit updates | ||
| # bundle exec rake profiles:backfill_gpo_expiration UPDATE_PROFILES=true > profiles.csv | ||
| # | ||
| task backfill_gpo_expiration: :environment do |_task, _args| | ||
| min_profile_age = (ENV['MIN_PROFILE_AGE_IN_DAYS'].to_i || 100).days | ||
| update_profiles = ENV['UPDATE_PROFILES'] == 'true' | ||
|
|
||
| job = GpoExpirationJob.new | ||
|
|
||
| profiles = job.gpo_profiles_that_should_be_expired( | ||
| as_of: Time.zone.now, | ||
matthinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| min_profile_age: min_profile_age, | ||
| ) | ||
|
|
||
| count = 0 | ||
|
|
||
| profiles.find_each do |profile| | ||
| count += 1 | ||
|
|
||
| gpo_verification_pending_at = profile.gpo_verification_pending_at | ||
|
|
||
| if gpo_verification_pending_at.blank? | ||
| raise "Profile #{profile.id} does not have gpo_verification_pending_at" | ||
| end | ||
|
|
||
| puts "#{profile.id},#{gpo_verification_pending_at.iso8601}" | ||
|
|
||
| if update_profiles | ||
| warn "Expired #{count} profiles" if count % 100 == 0 | ||
| job.expire_profile(profile: profile) | ||
| elsif count % 100 == 0 | ||
| warn "Found #{count} profiles" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| ## | ||
| # Usage: | ||
| # | ||
| # Rollback the above: | ||
| # | ||
| # bundle exec rake profiles:rollback_backfill_gpo_expiration < profiles.csv | ||
| # | ||
| task rollback_backfill_gpo_expiration: :environment do |_task, _args| | ||
| profile_data = STDIN.read.split("\n").map do |profile_row| | ||
| profile_row.split(',') | ||
| end | ||
|
|
||
| warn "Updating #{profile_data.count} records" | ||
|
|
||
| profile_data.each do |profile_datum| | ||
| profile_id, gpo_verification_pending_at = profile_datum | ||
| Profile.where(id: profile_id).update!( | ||
| gpo_verification_pending_at: Time.zone.parse(gpo_verification_pending_at), | ||
| gpo_verification_expired_at: nil, | ||
| ) | ||
| warn profile_id | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.