lib/tasks/fix_nil_ethnicity_arrays.rake
# - find draft applications that were ingested via ATP (or using provided hbx_id)
# - check and update person ethnicity arrays
# - check and update applicant ethnicity arrays
find draft applications that were ingested via ATP (or using provided hbx_id)
check and update person ethnicity arrays
check and update applicant ethnicity arrays
find draft applications that were ingested via ATP ( or using provided hbx_id ) check and update person ethnicity arrays check and update applicant ethnicity arrays
def remove_ethnicity_nils
      application_hbx_id = ENV['application_hbx_id']
      applications = if application_hbx_id.present?
                       FinancialAssistance::Application.draft.where(hbx_id: application_hbx_id)
                     else
                       FinancialAssistance::Application.draft.where(:transfer_id.nin => [nil, ''])
                     end
      puts "\nFinding person ethnicity arrays containing nil..."
      timestamp = Time.zone.now.strftime("%Y%m%d_%H%M%S")
      file_name = "#{Rails.root}/fix_person_nil_ethnicity_#{timestamp}.csv"
      FileUtils.touch(file_name)
      CSV.open(file_name, 'w+', headers: true) do |csv|
        csv << %w[APPLICATION_HBX_ID PRIMARY_HBX_ID PERSON_HBX_ID ETHNICITY UPDATED_ETHNICITY]
        applications.no_timeout.each do |application|
          application.applicants.no_timeout.each do |applicant|
            person = find_person(applicant.person_hbx_id)
            if person
              person_ethnicity = person.ethnicity
              next unless person_ethnicity && person_ethnicity.include?(nil)
              result = fix_person_ethnicity_array(person_ethnicity, application, person)
              csv << result unless result.nil?
            else
              puts "Error finding person document - hbx_id: #{applicant.person_hbx_id}"
            end
          end
        end
      end
      puts "\nFinding applicant ethnicity arrays containing nil..."
      timestamp = Time.zone.now.strftime("%Y%m%d_%H%M%S")
      file_name = "#{Rails.root}/fix_applicant_nil_ethnicity_#{timestamp}.csv"
      FileUtils.touch(file_name)
      CSV.open(file_name, 'w+', headers: true) do |csv|
        csv << %w[APPLICATION_HBX_ID PRIMARY_HBX_ID PERSON_HBX_ID ETHNICITY UPDATED_ETHNICITY]
        applications.no_timeout.each do |application|
          application.applicants.no_timeout.each do |applicant|
            next if applicant.nil?
            applicant_ethnicity = applicant.ethnicity
            next unless applicant_ethnicity && applicant_ethnicity.include?(nil)
            result = fix_applicant_ethnicity_array(applicant_ethnicity, application, applicant)
            csv << result unless result.nil?
          end
        end
      end
    end
def remove_ethnicity_nils application_hbx_id = ENV [ 'application_hbx_id' ] applications = if application_hbx_id . present? FinancialAssistance :: Application . draft . where ( hbx_id : application_hbx_id ) else FinancialAssistance :: Application . draft . where ( :transfer_id . nin => [ nil , '' ] ) end puts "\nFinding person ethnicity arrays containing nil..." timestamp = Time . zone . now . strftime ( "%Y%m%d_%H%M%S" ) file_name = "#{Rails.root}/fix_person_nil_ethnicity_#{timestamp}.csv" FileUtils . touch ( file_name ) CSV . open ( file_name , 'w+' , headers : true ) do | csv | csv << %w[ APPLICATION_HBX_ID PRIMARY_HBX_ID PERSON_HBX_ID ETHNICITY UPDATED_ETHNICITY ] applications . no_timeout . each do | application | application . applicants . no_timeout . each do | applicant | person = find_person ( applicant . person_hbx_id ) if person person_ethnicity = person . ethnicity next unless person_ethnicity && person_ethnicity . include? ( nil ) result = fix_person_ethnicity_array ( person_ethnicity , application , person ) csv << result unless result . nil? else puts "Error finding person document - hbx_id: #{applicant.person_hbx_id}" end end end end puts "\nFinding applicant ethnicity arrays containing nil..." timestamp = Time . zone . now . strftime ( "%Y%m%d_%H%M%S" ) file_name = "#{Rails.root}/fix_applicant_nil_ethnicity_#{timestamp}.csv" FileUtils . touch ( file_name ) CSV . open ( file_name , 'w+' , headers : true ) do | csv | csv << %w[ APPLICATION_HBX_ID PRIMARY_HBX_ID PERSON_HBX_ID ETHNICITY UPDATED_ETHNICITY ] applications . no_timeout . each do | application | application . applicants . no_timeout . each do | applicant | next if applicant . nil? applicant_ethnicity = applicant . ethnicity next unless applicant_ethnicity && applicant_ethnicity . include? ( nil ) result = fix_applicant_ethnicity_array ( applicant_ethnicity , application , applicant ) csv << result unless result . nil? end end end end
find draft applications that were ingested via ATP (or using provided hbx_id)
 check and update person ethnicity arrays
 check and update applicant ethnicity arrays
find draft applications that were ingested via ATP ( or using provided hbx_id ) check and update person ethnicity arrays check and update applicant ethnicity arrays
# Run on application with provided hbx_id # Run on all ATP-ingested draft applications # Fix person ethnicity arrays # puts "Removing ethnicity nil(s) from person #{person.hbx_id}" # Fix applicant ethnicity arrays # puts "Removing ethnicity nil(s) from applicant #{applicant.person_hbx_id}"