<commit_before>module CleverIntegration::SignUp::Teacher

  def self.run(auth_hash, requesters)
    parsed_data = self.parse_data(auth_hash)
    district = District.find_by(clever_id: parsed_data[:district_id])

    # TODO: error message for no district found (district has not authorized this school yet)
    if district.nil?
      result = {type: 'user_failure', data: "District has not authorized this school yet"}
    else
      teacher = self.create_teacher(parsed_data)
      if teacher.present?
        self.associate_teacher_to_district(teacher, district)
        school = self.import_school(teacher, district.token, requesters)
        classrooms = self.import_classrooms(teacher, district.token, requesters)
        students = self.import_students(classrooms, district.token)
        result = {type: 'user_success', data: teacher}
      else
        result = {type: 'user_failure', data: "No Teacher Present"}
      end
    end
    result
  end

  private

  def self.parse_data(auth_hash)
    CleverIntegration::Parsers::TeacherFromAuth.run(auth_hash)
  end

  def self.create_teacher(parsed_data)
    CleverIntegration::Creators::Teacher.run(parsed_data)
  end

  def self.associate_teacher_to_district(teacher, district)
    CleverIntegration::Associators::TeacherToDistrict.run(teacher, district)
  end
<commit_msg>support importing teachers from clever with no district<commit_after>module CleverIntegration::SignUp::Teacher

  def self.run(auth_hash, requesters)
    parsed_data = self.parse_data(auth_hash)
    district = District.find_by(clever_id: parsed_data[:district_id])

    if district
      self.district_integration(parsed_data, requesters, district)
    else
      self.library_integration(auth_hash)
    end
  end

  private

  def self.library_integration(auth_hash)
    CleverIntegration::Importers::Library.run(auth_hash)
  end

  def self.district_integration(auth_hash, requesters, district)
    teacher = self.create_teacher(auth_hash)
    if teacher.present?
      self.associate_teacher_to_district(teacher, district)
      school = self.import_school(teacher, district.token, requesters)
      classrooms = self.import_classrooms(teacher, district.token, requesters)
      students = self.import_students(classrooms, district.token)
      {type: 'user_success', data: teacher}
    else
      {type: 'user_failure', data: "No Teacher Present"}
    end
  end

  def self.parse_data(auth_hash)
    CleverIntegration::Parsers::TeacherFromAuth.run(auth_hash)
  end

  def self.create_teacher(parsed_data)
    CleverIntegration::Creators::Teacher.run(parsed_data)
  end

  def self.associate_teacher_to_district(teacher, district)
    CleverIntegration::Associators::TeacherToDistrict.run(teacher, district)
  end

{'fraction_non_alphanumeric': 0.08849235251274581, 'fraction_numerical': 0.0, 'mean_word_length': 4.483033932135728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4230879', 'n_tokens_mistral': 884, 'n_tokens_neox': 846, 'n_words': 166}