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

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews