<commit_before>namespace :form_answers do

  desc 'fixes missing org_chart from document'
  task fix_missing_org_chart: :environment do
    FormAnswerAttachment.where(question_key: "org_chart").find_each do |attachment|
      attachment.form_answer.document["org_chart"] = { "0" => { "file" => attachment.id } }
      attachment.form_answer.save!
    end
  end

  desc 'Adds search indexed data'
  task refresh_search_indexes: :environment do
    FormAnswer.find_each do |f|
      user = f.user
      if user.present?
        f.user_full_name = user.full_name
        f.save
      end
    end
  end

  desc "Adds assessors fk"
  task refresh_assessors_ids: :environment do
    FormAnswer.find_each do |f|
      primary = f.assessors.primary
      secondary = f.assessors.secondary
      f.primary_assessor_id = primary.try(:id)
      f.secondary_assessor_id = secondary.try(:id)
      f.save
    end
  end

  # For single use only
  desc "Rebuilds the urns for all apps"
  task rebuild_urn: :environment do
    FormAnswer.connection.execute("ALTER SEQUENCE urn_seq_2016 RESTART")
    FormAnswer.connection.execute("ALTER SEQUENCE urn_seq_promotion_2016 RESTART")
    FormAnswer.update_all(urn: nil)
    FormAnswer.find_each(&:save!)
  end

  desc "Adds the county/remove country to all form answers"
  task refresh_attributes: :environment do
    FormAnswer.find_each do |f|
      next if f.promotion?
      f.document.delete("principal_address_country")
      f.document["principal_address_county"] = "Buckinghamshire" if f.submitted?
      f.save(validate: false)
    end
  end

  desc "Removes the deprecated answers from forms"
  task remove_overseas_sales: :environment do
    FormAnswer.where(award_type: "trade").find_each do |f|
      attributes = [
        "overseas_sales_direct_1of3",
        "overseas_sales_direct_1of6",
        "overseas_sales_direct_2of3",
        "overseas_sales_direct_3of3",
        "overseas_sales_direct_2of6",
        "overseas_sales_direct_3of6",
        "overseas_sales_direct_4of6",
        "overseas_sales_direct_5of6",
        "overseas_sales_direct_6of6",
        "overseas_sales_indirect_1of3",
        "overseas_sales_indirect_2of3",
        "overseas_sales_indirect_3of3",
        "overseas_sales_indirect_1of6",
        "overseas_sales_indirect_2of6",
        "overseas_sales_indirect_3of6",
        "overseas_sales_indirect_4of6",
        "overseas_sales_indirect_5of6",
        "overseas_sales_indirect_6of6"

      ]
      attributes.each do |a|
        f.document.delete(a)
      end
      f.save(validate: false)
    end
  end

  desc "Populate the form progresses"
  # Remove after use - it assumes that progress set up before save
  task populate_form_answer_progresses: :environment do
    output = {
      saved: [],
      not_saved: []
    }
    FormAnswer.find_each do |f|
      if f.save
        output[:saved] << f.id
      else
        output[:not_saved] << f.id
      end
    end

    p "Saved: #{output[:saved].size}; not saved: #{output[:not_saved].inspect}"
  end

  desc "Resaves company_or_nominee_name field"
  task resave_company_or_nominee_name: :environment do
    FormAnswer.find_each do |f|
      f.update_column(:company_or_nominee_name, f.company_or_nominee_from_document)
    end
  end
end<commit_msg>remove legacy rake task [ci skip]<commit_after>namespace :form_answers do

  desc 'fixes missing org_chart from document'
  task fix_missing_org_chart: :environment do
    FormAnswerAttachment.where(question_key: "org_chart").find_each do |attachment|
      attachment.form_answer.document["org_chart"] = { "0" => { "file" => attachment.id } }
      attachment.form_answer.save!
    end
  end

  desc 'Adds search indexed data'
  task refresh_search_indexes: :environment do
    FormAnswer.find_each do |f|
      user = f.user
      if user.present?
        f.user_full_name = user.full_name
        f.save
      end
    end
  end

  desc "Adds assessors fk"
  task refresh_assessors_ids: :environment do
    FormAnswer.find_each do |f|
      primary = f.assessors.primary
      secondary = f.assessors.secondary
      f.primary_assessor_id = primary.try(:id)
      f.secondary_assessor_id = secondary.try(:id)
      f.save
    end
  end

  # For single use only
  desc "Rebuilds the urns for all apps"
  task rebuild_urn: :environment do
    FormAnswer.connection.execute("ALTER SEQUENCE urn_seq_2016 RESTART")
    FormAnswer.connection.execute("ALTER SEQUENCE urn_seq_promotion_2016 RESTART")
    FormAnswer.update_all(urn: nil)
    FormAnswer.find_each(&:save!)
  end

  desc "Adds the county/remove country to all form answers"
  task refresh_attributes: :environment do
    FormAnswer.find_each do |f|
      next if f.promotion?
      f.document.delete("principal_address_country")
      f.document["principal_address_county"] = "Buckinghamshire" if f.submitted?
      f.save(validate: false)
    end
  end

  desc "Removes the deprecated answers from forms"
  task remove_overseas_sales: :environment do
    FormAnswer.where(award_type: "trade").find_each do |f|
      attributes = [
        "overseas_sales_direct_1of3",
        "overseas_sales_direct_1of6",
        "overseas_sales_direct_2of3",
        "overseas_sales_direct_3of3",
        "overseas_sales_direct_2of6",
        "overseas_sales_direct_3of6",
        "overseas_sales_direct_4of6",
        "overseas_sales_direct_5of6",
        "overseas_sales_direct_6of6",
        "overseas_sales_indirect_1of3",
        "overseas_sales_indirect_2of3",
        "overseas_sales_indirect_3of3",
        "overseas_sales_indirect_1of6",
        "overseas_sales_indirect_2of6",
        "overseas_sales_indirect_3of6",
        "overseas_sales_indirect_4of6",
        "overseas_sales_indirect_5of6",
        "overseas_sales_indirect_6of6"

      ]
      attributes.each do |a|
        f.document.delete(a)
      end
      f.save(validate: false)
    end
  end

  desc "Resaves company_or_nominee_name field"
  task resave_company_or_nominee_name: :environment do
    FormAnswer.find_each do |f|
      f.update_column(:company_or_nominee_name, f.company_or_nominee_from_document)
    end
  end
end
{'fraction_non_alphanumeric': 0.08406790728044401, 'fraction_numerical': 0.014691478942213516, 'mean_word_length': 3.260778859527121, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2822609', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2078, 'n_words': 410}