<commit_before>class CaseLinkingService
  attr_reader :result, :error

  def initialize(user, kase, link_case_number)
    @user = user
    @case = kase
    @link_case_number = link_case_number
    @link_case = nil
    @result = :incomplete
  end

  def create
    ActiveRecord::Base.transaction do

      # By using a LinkedCase object here we can retrieve validation errors
      # from the models
      @case_link = LinkedCase.new(
        linked_case_number: @link_case_number.to_s&.strip
      )

      #Create the links
      @case.related_case_links << @case_link

      if @case.valid?
        @linked_case = @case_link.linked_case
        # Log event in both cases
        @case.state_machine.link_a_case!(acting_user: @user,
                                         acting_team: @user.teams_for_case(@case).first,
                                         linked_case_id: @linked_case.id)
        @linked_case.state_machine.link_a_case!(acting_user: @user,
                                                acting_team: @user.teams_for_case(@case).first,
                                                linked_case_id: @case.id)
        @result = :ok
      else
        extract_case_link_errors_to_case(@case_link, @case)
        @result = :validation_error
      end
    end
    @result
  rescue => err
    Rails.logger.error err.to_s
    Rails.logger.error err.backtrace.join("\n\t")
    @error = err
    @result = :error
  end

  def add_case_link_to_case(case_link, kase)
    if case_link.errors[:linked_case_number].present?
      kase.errors.add(:linked_case_number,
                      case_link.errors[:linked_case_number].first)
    elsif case_link.errors[:linked_case].present?
      kase.errors.add(:linked_case_number,
                       case_link.errors[:linked_case].first)
    end
  end

  def destroy
    ActiveRecord::Base.transaction do
      # find the linked case
      @link_case = @case.related_cases.find_by(number: @link_case_number)

      # Destroy the links
      @case.related_cases.destroy(@link_case)

      # Log event in both cases
      @case.state_machine.remove_linked_case!(acting_user: @user,
                                       acting_team: @user.teams_for_case(@case).first,
                                       linked_case_id: @link_case.id)
      @link_case.state_machine.remove_linked_case!(acting_user: @user,
                                            acting_team: @user.teams_for_case(@case).first,
                                            linked_case_id: @case.id)

      @result = :ok

    end

    @result
  rescue => err
    Rails.logger.error err.to_s
    Rails.logger.error err.backtrace.join("\n\t")
    @error = err
    @result = :error
  end
end
<commit_msg>CT-1776 fix typo in add_case_link_to_case method name<commit_after>class CaseLinkingService
  attr_reader :result, :error

  def initialize(user, kase, link_case_number)
    @user = user
    @case = kase
    @link_case_number = link_case_number
    @link_case = nil
    @result = :incomplete
  end

  def create
    ActiveRecord::Base.transaction do

      # By using a LinkedCase object here we can retrieve validation errors
      # from the models
      @case_link = LinkedCase.new(
        linked_case_number: @link_case_number.to_s&.strip
      )

      #Create the links
      @case.related_case_links << @case_link

      if @case.valid?
        @linked_case = @case_link.linked_case
        # Log event in both cases
        @case.state_machine.link_a_case!(acting_user: @user,
                                         acting_team: @user.teams_for_case(@case).first,
                                         linked_case_id: @linked_case.id)
        @linked_case.state_machine.link_a_case!(acting_user: @user,
                                                acting_team: @user.teams_for_case(@case).first,
                                                linked_case_id: @case.id)
        @result = :ok
      else
        add_case_link_errors_to_case(@case_link, @case)
        @result = :validation_error
      end
    end
    @result
  rescue => err
    Rails.logger.error err.to_s
    Rails.logger.error err.backtrace.join("\n\t")
    @error = err
    @result = :error
  end

  def add_case_link_errors_to_case(case_link, kase)
    if case_link.errors[:linked_case_number].present?
      kase.errors.add(:linked_case_number,
                      case_link.errors[:linked_case_number].first)
    elsif case_link.errors[:linked_case].present?
      kase.errors.add(:linked_case_number,
                       case_link.errors[:linked_case].first)
    end
  end

  def destroy
    ActiveRecord::Base.transaction do
      # find the linked case
      @link_case = @case.related_cases.find_by(number: @link_case_number)

      # Destroy the links
      @case.related_cases.destroy(@link_case)

      # Log event in both cases
      @case.state_machine.remove_linked_case!(acting_user: @user,
                                       acting_team: @user.teams_for_case(@case).first,
                                       linked_case_id: @link_case.id)
      @link_case.state_machine.remove_linked_case!(acting_user: @user,
                                            acting_team: @user.teams_for_case(@case).first,
                                            linked_case_id: @case.id)

      @result = :ok

    end

    @result
  rescue => err
    Rails.logger.error err.to_s
    Rails.logger.error err.backtrace.join("\n\t")
    @error = err
    @result = :error
  end
end

{'fraction_non_alphanumeric': 0.08876384098747504, 'fraction_numerical': 0.0007260845888546016, 'mean_word_length': 1.9623655913978495, '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': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '506958', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1624, 'n_words': 306}