<commit_before>
require "cwc"

CongressForms::App.controller do
  get "cwc/:office_code/fields" do
    cm = CongressMember.find_by_cwc_office_code(params[:office_code])
    cm.as_cwc_required_json.to_json
  end

  post "cwc/:office_code/messages" do
    content_type :json

    cm = CongressMember.find_by_cwc_office_code(params[:office_code])
    return { status: "error", message: "Congress member with provided bio id not found" }.to_json if cm.nil?

    missing_parameters = []
    fields = params["fields"] || {}
    cm.as_cwc_required_json[:required_actions].each do |field|
      unless fields.include?(field["value"])
        missing_parameters << field["value"]
      end
    end

    if missing_parameters.any?
      see_other = "/cwc/#{params[:office_code]}/fields"
      message = "Missing parameters (#{missing_parameters.join(', ')}). See #{see_other} for required parameters."
      return { status: "error", message: message }.to_json
    end

    begin
      if params["organization"]
        cm.message_via_cwc(fields, campaign_tag: params["campaign_tag"],
                           organization: { name: params["organization"] })
      else
        cm.message_via_cwc(fields, campaign_tag: params["campaign_tag"])
      end
      { status: "success" }.to_json
    rescue Cwc::BadRequest => e
      logger.warn("Cwc::BadRequest:")
      e.errors.each{ |error| logger.warn("  * #{error}") }
      { status: "error" }.to_json
    end<commit_msg>Dont require $MESSAGE param if $STATEMENT is given<commit_after>
require "cwc"

CongressForms::App.controller do
  get "cwc/:office_code/fields" do
    cm = CongressMember.find_by_cwc_office_code(params[:office_code])
    cm.as_cwc_required_json.to_json
  end

  post "cwc/:office_code/messages" do
    content_type :json

    cm = CongressMember.find_by_cwc_office_code(params[:office_code])
    return { status: "error", message: "Congress member with provided bio id not found" }.to_json if cm.nil?

    missing_parameters = []
    fields = params["fields"] || {}
    fields["$MESSAGE"] ||= fields["$STATEMENT"]
    cm.as_cwc_required_json[:required_actions].each do |field|
      unless fields.include?(field["value"])
        missing_parameters << field["value"]
      end
    end

    if missing_parameters.any?
      see_other = "/cwc/#{params[:office_code]}/fields"
      message = "Missing parameters (#{missing_parameters.join(', ')}). See #{see_other} for required parameters."
      return { status: "error", message: message }.to_json
    end

    begin
      if params["organization"]
        cm.message_via_cwc(fields, campaign_tag: params["campaign_tag"],
                           organization: { name: params["organization"] })
      else
        cm.message_via_cwc(fields, campaign_tag: params["campaign_tag"])
      end
      { status: "success" }.to_json
    rescue Cwc::BadRequest => e
      logger.warn("Cwc::BadRequest:")
      e.errors.each{ |error| logger.warn("  * #{error}") }
      { status: "error" }.to_json
    end
{'fraction_non_alphanumeric': 0.13193752077102028, 'fraction_numerical': 0.0, 'mean_word_length': 3.38136826783115, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5248792', 'n_tokens_mistral': 963, 'n_tokens_neox': 930, 'n_words': 198}