<commit_before># app/services/sync_tcc.rb

class SyncTcc

  def initialize(context)
    @tcc_definition = context
    @errors = {}
    @remote_service = MoodleAPI::MoodleUser.new
  end

  def call
    students = get_students

    students.with_progress "Sincronizando #{students.size} TCCs para #{@tcc_definition.internal_name}" do |student|
      synchronize_tcc(student)
    end
  end

  def display_errors!
    unless @errors.empty?
      rows = []
      rows << %w(type, attributes, errors)

      @errors.each do |type, (error_data)|
        rows << [type, error_data[:context], error_data[:message]]
      end

      puts Terminal::Table.new rows: rows
    end
  end

  def find_or_create_person(moodle_id)
    raise ArgumentError, 'É necessário passar um moodle_id' if moodle_id.nil?

    person = Person.find_or_initialize_by(moodle_id: moodle_id)

    # Só busca os dados do usuário se ele ainda foi criado
    unless person.persisted?
      attributes = @remote_service.find_users_by_field('id', moodle_id)
      person.attributes = {moodle_username: attributes.username, email: attributes.email, name: attributes.name}

      unless person.valid? && person.save
        register_error(:person, person.attributes, person.error.messages)
        return nil
      end
    end

    person
  end

  private

  def synchronize_tcc(student)
    tcc = Tcc.find_or_initialize_by(student: student)

    tcc.tcc_definition = @tcc_definition
    tutor = get_tutor(student.moodle_id)
    tcc.tutor = tutor
<commit_msg>alteração de condição para atualização dos dados do usuário<commit_after># app/services/sync_tcc.rb

class SyncTcc

  def initialize(context)
    @tcc_definition = context
    @errors = {}
    @remote_service = MoodleAPI::MoodleUser.new
  end

  def call
    students = get_students

    students.with_progress "Sincronizando #{students.size} TCCs para #{@tcc_definition.internal_name}" do |student|
      synchronize_tcc(student)
    end
  end

  def display_errors!
    unless @errors.empty?
      rows = []
      rows << %w(type, attributes, errors)

      @errors.each do |type, (error_data)|
        rows << [type, error_data[:context], error_data[:message]]
      end

      puts Terminal::Table.new rows: rows
    end
  end

  def find_or_create_person(moodle_id)
    raise ArgumentError, 'É necessário passar um moodle_id' if moodle_id.nil?

    person = Person.find_or_initialize_by(moodle_id: moodle_id)

    # sempre atualiza os dados da pessoa
    attributes = @remote_service.find_users_by_field('id', moodle_id)
    person.attributes = {moodle_username: attributes.username, email: attributes.email, name: attributes.name}

    unless person.valid? && person.save
      register_error(:person, person.attributes, person.error.messages)
      return nil
    end

    person
  end

  private

  def synchronize_tcc(student)
    tcc = Tcc.find_or_initialize_by(student: student)

    tcc.tcc_definition = @tcc_definition
    tutor = get_tutor(student.moodle_id)
    tcc.tutor = tutor

{'fraction_non_alphanumeric': 0.09099966788442378, 'fraction_numerical': 0.0, 'mean_word_length': 3.455621301775148, 'pattern_counts': {'":': 0, '<': 11, '<?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': '1969009', 'n_tokens_mistral': 1023, 'n_tokens_neox': 990, 'n_words': 224}