<commit_before>defmodule Core.EventsController do
  use Core.Web, :controller
  require Logger
  import ShorterMaps
  alias Osdi.{Attendance, Address, Event, Repo}

  @secret Application.get_env(:core, :update_secret)

  def get(conn, params) do
    district = get_district(params["district"] || conn.cookies["district"])

    {:ok, coordinates} =
      district
      |> get_coordinates()
      |> Poison.encode()

    render(
      conn,
      "events.html",
      [district: district, coordinates: coordinates, title: "Events"] ++
        GlobalOpts.get(conn, params)
    )
  end

  def iframe(conn, params = %{"district" => district}) do
    {:ok, coordinates} =
      district
      |> get_coordinates()
      |> Poison.encode()

    conn
    |> delete_resp_header("x-frame-options")
    |> render(
         "embedded.html",
         [
           layout: {Core.LayoutView, "bare.html"},
           district: district,
           coordinates: coordinates,
           title: "Events in #{district}"
         ] ++ GlobalOpts.get(conn, params)
       )
  end

  def iframe(conn, params) do
    {:ok, coordinates} = nil |> get_coordinates() |> Poison.encode()

    conn
    |> delete_resp_header("x-frame-options")
    |> render(
         "embedded.html",
         [
           layout: {Core.LayoutView, "bare.html"},
           district: "",
           coordinates: coordinates,
           title: "Events"
         ] ++ GlobalOpts.get(conn, params)
       )
  end

  def get_one(conn, params = %{"name" => event_name}) do
    event =
      case Stash.get(:event_cache, event_name) do
        nil -> nil
        event -> event
      end

    banner = get_banner(event.type)

    render(
      conn,
      "rsvp.html",
      [event: event, title: event.title, description: event.description, banner: banner] ++
        GlobalOpts.get(conn, params)
    )
  end

  def rsvp(
        conn,
        params = %{
          "slug" => event_name,
          "name" => name,
          "email" => email,
          "phone" => phone,
          "address" => address,
          "zip" => zip,
          "city" => city,
          "state" => state
        }
      ) do
    [first_name, last_name] =
      case String.split(name, ",") do
        [single] -> [single, ""]
        list -> [List.first(list), List.last(list)]
        _ -> ["", ""]
      end

    event = Stash.get(:event_cache, event_name)
    banner = get_banner(event.type)

    Task.async(fn ->
      Core.EventMailer.on_rsvp(event, ~m{first_name, last_name, email})
    end)

    referrer_data = Map.merge(get_source(params), get_referrer(conn))

    attendance =
      %{given_name: first_name, family_name: last_name}
      |> add_if_exists(:email_address, email, email)
      |> add_if_exists(:phone_number, phone, phone)
      |> add_if_exists(:postal_address, address, %Address{
           address_lines: [address],
           locality: city,
           region: state,
           postal_code: zip
         })

    Attendance.push(event.id, attendance, referrer_data)

    render(
      conn,
      "rsvp.html",
      [
        event: event,
        person: true,
        title: event.title,
        description: event.description,
        banner: banner
      ] ++ GlobalOpts.get(conn, params)
    )
  end

  defp add_if_exists(map, key, test, val) do
    if test != nil and test != "" do
      Map.put(map, key, val)
    else
      map
    end
  end

  defp get_source(%{"ref" => ref}), do: %{source: ref}
  defp get_source(_params), do: %{}

  defp get_referrer(conn) do
    case get_req_header(conn, "referrer") do
      nil -> %{}
      [] -> %{}
      url -> %{referrer: url}
    end
  end

  defp get_district(""), do: nil
  defp get_district(nil), do: nil

  defp get_district(district) do
    district
    |> String.upcase()
    |> District.normalize()
  end
<commit_msg>nil address is fine for rsvp<commit_after>defmodule Core.EventsController do
  use Core.Web, :controller
  require Logger
  import ShorterMaps
  alias Osdi.{Attendance, Address, Event, Repo}

  @secret Application.get_env(:core, :update_secret)

  def get(conn, params) do
    district = get_district(params["district"] || conn.cookies["district"])

    {:ok, coordinates} =
      district
      |> get_coordinates()
      |> Poison.encode()

    render(
      conn,
      "events.html",
      [district: district, coordinates: coordinates, title: "Events"] ++
        GlobalOpts.get(conn, params)
    )
  end

  def iframe(conn, params = %{"district" => district}) do
    {:ok, coordinates} =
      district
      |> get_coordinates()
      |> Poison.encode()

    conn
    |> delete_resp_header("x-frame-options")
    |> render(
         "embedded.html",
         [
           layout: {Core.LayoutView, "bare.html"},
           district: district,
           coordinates: coordinates,
           title: "Events in #{district}"
         ] ++ GlobalOpts.get(conn, params)
       )
  end

  def iframe(conn, params) do
    {:ok, coordinates} = nil |> get_coordinates() |> Poison.encode()

    conn
    |> delete_resp_header("x-frame-options")
    |> render(
         "embedded.html",
         [
           layout: {Core.LayoutView, "bare.html"},
           district: "",
           coordinates: coordinates,
           title: "Events"
         ] ++ GlobalOpts.get(conn, params)
       )
  end

  def get_one(conn, params = %{"name" => event_name}) do
    event =
      case Stash.get(:event_cache, event_name) do
        nil -> nil
        event -> event
      end

    banner = get_banner(event.type)

    render(
      conn,
      "rsvp.html",
      [event: event, title: event.title, description: event.description, banner: banner] ++
        GlobalOpts.get(conn, params)
    )
  end

  def rsvp(
        conn,
        params = %{
          "slug" => event_name,
          "name" => name,
          "email" => email,
          "phone" => phone,
          "address" => address,
          "zip" => zip,
          "city" => city,
          "state" => state
        }
      ) do
    [first_name, last_name] =
      case String.split(name, ",") do
        [single] -> [single, ""]
        list -> [List.first(list), List.last(list)]
        _ -> ["", ""]
      end

    event = Stash.get(:event_cache, event_name)
    banner = get_banner(event.type)

    Task.async(fn ->
      Core.EventMailer.on_rsvp(event, ~m{first_name, last_name, email})
    end)

    referrer_data = Map.merge(get_source(params), get_referrer(conn))

    attendance =
      %{given_name: first_name, family_name: last_name}
      |> add_if_exists(:email_address, email, email)
      |> add_if_exists(:phone_number, phone, phone)
      |> add_if_exists(:postal_address, address, %Address{
           address_lines: [address],
           locality: city,
           region: state,
           postal_code: zip
         })

    Attendance.push(event.id, attendance, referrer_data)

    render(
      conn,
      "rsvp.html",
      [
        event: event,
        person: true,
        title: event.title,
        description: event.description,
        banner: banner
      ] ++ GlobalOpts.get(conn, params)
    )
  end

  defp add_if_exists(map, key, test, val) do
    if test != nil and test != "" do
      Map.put(map, key, val)
    else
      Map.put(map, key, nil)
    end
  end

  defp get_source(%{"ref" => ref}), do: %{source: ref}
  defp get_source(_params), do: %{}

  defp get_referrer(conn) do
    case get_req_header(conn, "referrer") do
      nil -> %{}
      [] -> %{}
      url -> %{referrer: url}
    end
  end

  defp get_district(""), do: nil
  defp get_district(nil), do: nil

  defp get_district(district) do
    district
    |> String.upcase()
    |> District.normalize()
  end

{'fraction_non_alphanumeric': 0.13730839179007534, 'fraction_numerical': 0.0, 'mean_word_length': 2.1644060830250718, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 73, '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': '1993233', 'n_tokens_mistral': 2547, 'n_tokens_neox': 2464, 'n_words': 547}