ChipCoons

ChipCoons

My Pheonix app is set up to be a bridge between a legacy JQuery web-app and Hubspot CRM.

I have all the functionality working to capture a contact in the JQuery side, pass it to the Pheonix app, and then sent a Create_Or_Update post to Hubspot leveraging a slightly updated Hubspotex implementation.

When the HTTP POST request is fired to Hubspot, it is returning a 200 response as expected, but I’m throwing an error in my Phoenix app and I’m not sure what to do to avoid the exception. I think I’m not handling the return correctly.

The function sending to Hubspot is:

  def send_to_hubspot(%Person{} = person) do
    email = person.email
    firstname = person.firstName
    lastname = person.lastName
    registration_event = person.event
    mobilephone = person.sms_phone
    body = %{properties: [
                %{property: "firstname", value: firstname},
                %{property: "lastname", value: lastname},
                %{property: "mobilephone", value: mobilephone},
                %{property: "registration_event", value: registration_event}
              ]
            }
    req = Hubspot.Contacts.create_or_update(email, body)
    case Hubspot.request(req) do
      {:ok, %HTTPoison.Response{status_code: 200}} ->
          person
      {:ok, %HTTPoison.Response{status_code: 409}} ->
        IO.puts "Conflict error response if you are trying to update the email address of a record, and there is an existing record with the new email address."
      {:error, %HTTPoison.Error{reason: reason}} ->
        IO.puts reason
    end

  end

I am calling the function after the Ecto/Postgres database is updated in this function (which is where I think my actual problem is…

  def update_person(%Person{} = person, attrs) do
    person
    |> Person.changeset(attrs)
    |> Repo.update()
    send_to_hubspot(person)
  end

And I’m getting the following exception in the console:

[error] #PID<0.538.0> running ScandimensionWeb.Endpoint (connection #PID<0.537.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: PUT /api/people/1
** (exit) an exception was raised:

    ** (FunctionClauseError) no function clause matching in ScandimensionWeb.FallbackController.call/2
        (scandimension) lib/scandimension_web/controllers/fallback_controller.ex:10: 
            ScandimensionWeb.FallbackController.call(%Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, 
                assigns: %{}, 
                before_send: [#Function<0.58261320/1 in Plug.Session.before_send/2>, #Function<1.112466771/1 in Plug.Logger.call/2>, #Function<0.33752287/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], 
                body_params: %{"person" => %{"email" => "test@test.com", "event" => "AustinMakerFaire", "firstName" => "Tom", "id" => 1, "lastName" => "Tester", "slot_score" => 5, "sms_phone" => "8015551212"}}, 
                cookies: %{"_scandimension_key" => "SFMyNTY.g3QAAAABbQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.n-kVxZ-a6yhdrUfPzwxSbxhtBjqizNzaQF5XPYsXi1g"}, 
                halted: false, host: "localhost", method: "PUT", owner: #PID<0.538.0>, 
                params: %{"id" => "1", "person" => %{"email" => "test@test.com", "event" => "AustinMakerFaire", "firstName" => "Tom", "id" => 1, "lastName" => "Tester", "slot_score" => 5, "sms_phone" => "8015551212"}}, 
                path_info: ["api", "people", "1"], 
                path_params: %{"id" => "1"}, port: 4000, private: %{ScandimensionWeb.Router => {[], %{}}, 
                :phoenix_action => :update, 
                :phoenix_controller => ScandimensionWeb.PersonController, 
                :phoenix_endpoint => ScandimensionWeb.Endpoint, 
                :phoenix_format => "json", 
                :phoenix_layout => {ScandimensionWeb.LayoutView, :app}, 
                :phoenix_pipelines => [:api], 
                :phoenix_router => ScandimensionWeb.Router, 
                :phoenix_view => ScandimensionWeb.PersonView, 
                :plug_session => %{"current_user_id" => 1}, :plug_session_fetch => :done}, 
                query_params: %{}, query_string: "", 
                remote_ip: {127, 0, 0, 1}, r
                eq_cookies: %{"_scandimension_key" => "SFMyNTY.g3QAAAABbQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.n-kVxZ-a6yhdrUfPzwxSbxhtBjqizNzaQF5XPYsXi1g"}, 
                req_headers: [{"accept", "*/*"}, 
                    {"accept-encoding", "gzip, deflate"}, 
                    {"cache-control", "no-cache"}, 
                    {"connection", "keep-alive"}, 
                    {"content-length", "148"}, 
                    {"content-type", "application/json"}, 
                    {"cookie", "_scandimension_key=SFMyNTY.g3QAAAABbQAAAA9jdXJyZW50X3VzZXJfaWRhAQ.n-kVxZ-a6yhdrUfPzwxSbxhtBjqizNzaQF5XPYsXi1g"}, 
                    {"host", "localhost:4000"}, 
                    {"postman-token", "72fffb40-c564-4aa1-91bf-f3c6960ed369"}, 
                    {"user-agent", "PostmanRuntime/7.11.0"}], 
                request_path: "/api/people/1", 
                resp_body: nil, 
                resp_cookies: %{}, 
                resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "FZkgGTd0_LBHp24AAAGC"}], 
                    scheme: :http, 
                    script_name: [], 
                    secret_key_base: :..., 
                    state: :unset, 
                    status: nil}, 
                    %Scandimension.Prospects.Person{__meta__: #Ecto.Schema.Metadata<:loaded, "people">, email: "test@test.com", event: nil, firstName: "Tom", id: 1, inserted_at: ~N[2019-04-25 16:08:38], is_synched: false, lastName: "Tester", slot_score: 5, sms_phone: "8015551212", updated_at: ~N[2019-04-26 20:20:05]})
        (scandimension) lib/scandimension_web/controllers/person_controller.ex:1: ScandimensionWeb.PersonController.action/2
        (scandimension) lib/scandimension_web/controllers/person_controller.ex:1: ScandimensionWeb.PersonController.phoenix_controller_pipeline/2
        (scandimension) lib/scandimension_web/endpoint.ex:1: ScandimensionWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
        (scandimension) lib/scandimension_web/endpoint.ex:1: ScandimensionWeb.Endpoint.plug_builder_call/2
        (scandimension) lib/plug/debugger.ex:122: ScandimensionWeb.Endpoint."call (overridable 3)"/2
        (scandimension) lib/scandimension_web/endpoint.ex:1: ScandimensionWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2
        (cowboy) /Users/chipcoons/Dev/Phoenix/scandimension/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /Users/chipcoons/Dev/Phoenix/scandimension/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
        (cowboy) /Users/chipcoons/Dev/Phoenix/scandimension/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Showing Posts 1 to 8

axelson

axelson

Scenic Core Team

Can you show your fallback controller and PersonController?

ChipCoons

ChipCoons OP

defmodule ScandimensionWeb.PersonController do
  use ScandimensionWeb, :controller

  import Ecto.Query, warn: false

  alias Scandimension.Repo
  alias Scandimension.Prospects
  alias Scandimension.Prospects.Person

  action_fallback ScandimensionWeb.FallbackController

  def index(conn, _params) do
    people = Prospects.list_people()
    render(conn, "index.json", people: people)
  end

  def create(conn, %{"person" => person_params}) do
    with {:ok, %Person{} = person} <- Prospects.create_person(person_params) do
      conn
      |> put_status(:created)
      |> put_resp_header("location", Routes.person_path(conn, :show, person))
      |> render("show.json", person: person)
    end
  end

  def show(conn, %{"id" => id}) do
    person = Prospects.get_person!(id)
    render(conn, "show.json", person: person)
  end

  def update(conn, %{"id" => id, "person" => person_params}) do
    person = Prospects.get_person!(id)

    with {:ok, %Person{} = person} <- Prospects.update_person(person, person_params) do
      render(conn, "show.json", person: person)
    end
  end

  def delete(conn, %{"id" => id}) do
    person = Prospects.get_person!(id)

    with {:ok, %Person{}} <- Prospects.delete_person(person) do
      send_resp(conn, :no_content, "")
    end
  end

  def fetch_person(conn, %{"email" => email, "event" => event }) do
    query = from "people", where: [email: ^email]

    if (Repo.exists?(query)) do
      person = Prospects.get_person_byEmail!(email)
      
      render(conn, "show.json", person: person)
    else
      temp = %{email: email, event: event}

      with {:ok, %Person{} = person} <- Prospects.create_person(temp) do
        conn
        |> put_status(:created)
        |> put_resp_header("location", Routes.person_path(conn, :show, person))
        |> render("show.json", person: person)
      end
    end

  end

end

FallbackController:

defmodule ScandimensionWeb.FallbackController do
  @moduledoc """
  Translates controller action results into valid `Plug.Conn` responses.

  See `Phoenix.Controller.action_fallback/1` for more details.
  """
  use ScandimensionWeb, :controller


  def call(conn, {:error, :not_found}) do
    conn
    |> put_status(:not_found)
    |> put_view(ScandimensionWeb.ErrorView)
    |> render(:"404")
  end

  def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
    conn
    |> put_status(:unprocessable_entity)
    |> put_view(ScandimensionWeb.ChangesetView)
    |> render("error.json", changeset: changeset)
  end


end
axelson

axelson

Scenic Core Team

Is Prospects.create_person(person_params) returning a result that matches {:ok, %Persion{}? If it’s just directly calling send_to_hubspot then it doesn’t appear to be (and you probably want to finesse the return values of send_to_hubspot anyway).

ChipCoons

ChipCoons OP

Right now I’m only using the update_person call…

However, I am looking at changing the parameters to the send_to_hubspot() function so I can just chain the results:

  def update_person(%Person{} = person, attrs) do
    person
    |> Person.changeset(attrs)
    |> Repo.update()
    |> send_to_hubspot()

But I haven’t quite got that wired up yet.

axelson

axelson

Scenic Core Team

Ah, right. So what is Prospects.update_person/2 returning?

ChipCoons

ChipCoons OP

{:ok, %Person{}}

If I pass that result into send_to_hubspot, I should be able to pattern match on the value, and then populate the JSON body to send to Hubspot, right? But I’m getting messed up in the syntax of writing that for some reason.

ChipCoons

ChipCoons OP

I cleaned up my function some and got rid of the error.

  def send_to_hubspot(attrs \\ %{}) do

    with {:ok, person} = attrs do
      email = person.email
      firstname = person.firstName
      lastname = person.lastName
      registration_event = person.event
      mobilephone = person.sms_phone
      body = %{properties: [
                %{property: "firstname", value: firstname},
                %{property: "lastname", value: lastname},
                %{property: "mobilephone", value: mobilephone},
                %{property: "registration_event", value: registration_event}
              ]
            }

      req = Hubspot.Contacts.create_or_update(email, body)
          case Hubspot.request(req) do
            {:ok, %HTTPoison.Response{status_code: 200}} ->
                {:ok, person}
            {:ok, %HTTPoison.Response{status_code: 409}} ->
              IO.puts "Conflict error response if you are trying to update the email address of a record, and there is an existing record with the new email address."
            {:error, %HTTPoison.Error{reason: reason}} ->
              IO.puts reason
          end
    end
  end

This lets me change the update function to be:

  def update_person(%Person{} = person, attrs) do
    person
    |> Person.changeset(attrs)
    |> Repo.update()
    |> send_to_hubspot()
  end

And data is flowing end-to-end without throwing exceptions. Thanks for the assistance.

axelson

axelson

Scenic Core Team

I’m glad that you got it to work!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews