<commit_before># frozen_string_literal: true
class ManageSurveyResponse
  attr_accessor :action

  def initialize(page:, form:, params:, action_id: nil)
    @form = form
    @params = sanitize_params(params, form)
    @validator = FormValidator.new(@params, @form.form_elements)
    @action = action_id.present? ? Action.find(action_id) : Action.new(page: page)
  end

  def run
    assign_member
    if action_valid?
      update_member
      update_action
      publish_event
    end

    action_valid?
  end

  def errors
    @validator.errors
  end

  private

  def sanitize_params(params, form)
    params.slice(*form.element_names)
  end

  def assign_member
    @action.member ||= if @params[:akid].present?
                         Member.find_by_akid(@params[:akid])
                       elsif @params[:email].present?
                         Member.find_or_initialize_by(email: @params[:email])
                       end
  end

  def action_valid?
    @action.member.present? && @validator.valid?
  end

  def update_member
    MemberUpdater.run(@action.member, @params)
  end

  def update_action
    @action.form_data.merge!(@params)
    @action.save!
  end

  def publish_event
    ActionQueue::Pusher.push(:new_survey_response, @action)
  end<commit_msg>Reassigning action.member on every survey response<commit_after># frozen_string_literal: true
class ManageSurveyResponse
  attr_accessor :action

  def initialize(page:, form:, params:, action_id: nil)
    @form = form
    @params = sanitize_params(params, form)
    @validator = FormValidator.new(@params, @form.form_elements)
    @action = action_id.present? ? Action.find(action_id) : Action.new(page: page)
  end

  def run
    assign_member
    if action_valid?
      update_member
      update_action
      publish_event
    end

    action_valid?
  end

  def errors
    @validator.errors
  end

  private

  def sanitize_params(params, form)
    params.slice(*form.element_names)
  end

  def assign_member
    @action.member = Member.find_or_initialize_by(email: @params[:email])
  end

  def action_valid?
    @action.member.present? && @validator.valid?
  end

  def update_member
    MemberUpdater.run(@action.member, @params)
  end

  def update_action
    @action.form_data.merge!(@params)
    @action.save!
  end

  def publish_event
    ActionQueue::Pusher.push(:new_survey_response, @action)
  end
{'fraction_non_alphanumeric': 0.09467951403435275, 'fraction_numerical': 0.0, 'mean_word_length': 3.131487889273356, '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': '4425271', 'n_tokens_mistral': 776, 'n_tokens_neox': 759, 'n_words': 158}