From d94d85447835ab625e9fb76764dccfafd92776af Mon Sep 17 00:00:00 2001 From: Morgan Sadr-Hashemi Date: Wed, 15 Apr 2020 17:24:24 +0100 Subject: [PATCH] HTTPClient can add email activity --- lib/closex/client_behaviour.ex | 3 + lib/closex/http_client.ex | 8 ++ mix.exs | 2 +- mix.lock | 6 +- .../vcr_cassettes/create_email_activity.json | 42 ++++++++++ test/http_client_test.exs | 80 +++++++++++++++++++ 6 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/vcr_cassettes/create_email_activity.json diff --git a/lib/closex/client_behaviour.ex b/lib/closex/client_behaviour.ex index 83135b3..d553cf2 100644 --- a/lib/closex/client_behaviour.ex +++ b/lib/closex/client_behaviour.ex @@ -91,6 +91,9 @@ defmodule Closex.ClientBehaviour do @callback create_sms_activity(map) :: result @callback create_sms_activity(map, opts) :: result + @callback create_email_activity(map) :: result + @callback create_email_activity(map, opts) :: result + @callback find_call_activities(search_term) :: result @callback create_task(id, String.t()) :: result diff --git a/lib/closex/http_client.ex b/lib/closex/http_client.ex index bbedbc5..80a1eec 100644 --- a/lib/closex/http_client.ex +++ b/lib/closex/http_client.ex @@ -166,6 +166,14 @@ defmodule Closex.HTTPClient do |> handle_response end + @doc "Create an email activity in close.io: + https://developer.close.io/#activities-create-an-email-activity" + @impl true + def create_email_activity(payload, opts \\ []) do + post_json("/activity/email/", payload, [], opts) + |> handle_response + end + @doc "list or filter all call activities: https://developer.close.com/#activities-list-or-filter-all-call-activities" @impl true diff --git a/mix.exs b/mix.exs index 7e366d3..3b64b8f 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Closex.Mixfile do def project do [ app: :closex, - version: "1.4.6", + version: "1.4.7", build_path: "_build", config_path: "config/config.exs", deps_path: "deps", diff --git a/mix.lock b/mix.lock index 7e74336..a79836f 100644 --- a/mix.lock +++ b/mix.lock @@ -7,8 +7,8 @@ "exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"}, "exvcr": {:hex, :exvcr, "0.10.3", "1ae3b97560430acfa88ebc737c85b2b7a9dbacd8a2b26789a19718b51ae3522c", [:mix], [{:exactor, "~> 2.2", [hex: :exactor, repo: "hexpm", optional: false]}, {:exjsx, "~> 4.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.0", [hex: :httpoison, repo: "hexpm", optional: true]}, {:httpotion, "~> 3.1", [hex: :httpotion, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:meck, "~> 0.8", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "484d856d5f4f6a1ba3a691b82fcd29c0a46cb56eb6ddfd2b1d7aca1061a46ec0"}, "file_system": {:hex, :file_system, "0.2.6", "fd4dc3af89b9ab1dc8ccbcc214a0e60c41f34be251d9307920748a14bf41f1d3", [:mix], [], "hexpm", "0d50da6b04c58e101a3793b1600f9a03b86e3a8057b192ac1766013d35706fa6"}, - "hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "e0100f8ef7d1124222c11ad362c857d3df7cb5f4204054f9f0f4a728666591fc"}, - "httpoison": {:hex, :httpoison, "1.6.2", "ace7c8d3a361cebccbed19c283c349b3d26991eff73a1eaaa8abae2e3c8089b6", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "aa2c74bd271af34239a3948779612f87df2422c2fdcfdbcec28d9c105f0773fe"}, + "hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, + "httpoison": {:hex, :httpoison, "1.6.2", "ace7c8d3a361cebccbed19c283c349b3d26991eff73a1eaaa8abae2e3c8089b6", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, "idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "4bdd305eb64e18b0273864920695cb18d7a2021f31a11b9c5fbcd9a253f936e2"}, "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"}, "jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm", "fc3499fed7a726995aa659143a248534adc754ebd16ccd437cd93b649a95091f"}, @@ -20,6 +20,6 @@ "mix_test_watch": {:hex, :mix_test_watch, "0.9.0", "c72132a6071261893518fa08e121e911c9358713f62794a90c95db59042af375", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "817dec4a7f6edf260258002f99ac8ffaf7a8f395b27bf2d13ec24018beecec8a"}, "nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm", "5c040b8469c1ff1b10093d3186e2e10dbe483cd73d79ec017993fb3985b8a9b3"}, "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"}, } diff --git a/test/fixtures/vcr_cassettes/create_email_activity.json b/test/fixtures/vcr_cassettes/create_email_activity.json new file mode 100644 index 0000000..ddad203 --- /dev/null +++ b/test/fixtures/vcr_cassettes/create_email_activity.json @@ -0,0 +1,42 @@ +[ + { + "request": { + "body": "{\"attachments\":[],\"bcc\":[],\"body_html\":\"Hi! My name is Karan and I'm your Account Manager.\",\"body_text\":\"Hi! My name is Karan and I'm your Account Manager.\",\"cc\":[],\"contact_id\":\"cont_gwto1MOgh8nNGpCVn5DFJkmMxZYY6oQbh8X9mS5uEXg\",\"created_by\":null,\"created_by_name\":\"Bruce Wayne\",\"date_created\":\"2013-02-01T00:54:51.274000+00:00\",\"direction\":\"outgoing\",\"lead_id\":\"lead_sA6s1qN2hh1eDHipGxVAKvFlWCEG4xglutTXVUgnNkn\",\"sender\":\"morgan@nested.com\",\"status\":\"draft\",\"subject\":\"Welcome to Close!\",\"template_id\":null,\"to\":[\"phil+sample@close.com\"]}", + "headers": { + "Content-Type": "application/json", + "Accept": "application/json" + }, + "method": "post", + "options": { + "basic_auth": [ + "FAKE_CLOSEIO_TOKEN", + "" + ] + }, + "request_body": "", + "url": "https://api.close.com/api/v1/activity/email/" + }, + "response": { + "binary": false, + "body": "{\"body_preview\": \"Hi! My name is Karan and I'm your Account Manager.\", \"attachments\": [], \"in_reply_to_id\": null, \"body_text\": \"Hi! My name is Karan and I'm your Account Manager.\", \"updated_by_name\": \"API User (DO NOT DELETE)\", \"template_name\": null, \"_type\": \"Email\", \"contact_id\": \"cont_gwto1MOgh8nNGpCVn5DFJkmMxZYY6oQbh8X9mS5uEXg\", \"thread_id\": \"acti_IfDYuF3eVQXH6tCg0p5nyn0tCfF3hZtLrSYpTnUdh5P\", \"references\": [], \"message_ids\": [], \"subject\": \"Welcome to Close!\", \"user_id\": \"user_CwSfS8J6DsD6tnAsUwNFKCpS7Tq9LVCgJvHapOcpWyW\", \"to\": [\"phil+sample@close.com\"], \"created_by\": \"user_CwSfS8J6DsD6tnAsUwNFKCpS7Tq9LVCgJvHapOcpWyW\", \"id\": \"acti_GTPmezSVx2Gb0vDSJyB4zKDHKFXv4YXJfgD4UlQL1O5\", \"date_updated\": \"2020-04-15T16:22:22.287000+00:00\", \"followup_sequence_id\": null, \"send_attempts\": [], \"created_by_name\": \"API User (DO NOT DELETE)\", \"user_name\": \"API User (DO NOT DELETE)\", \"opens\": [], \"status\": \"draft\", \"direction\": \"outgoing\", \"updated_by\": \"user_CwSfS8J6DsD6tnAsUwNFKCpS7Tq9LVCgJvHapOcpWyW\", \"sequence_name\": null, \"body_html_quoted\": [{\"html\": \"Hi! My name is Karan and I'm your Account Manager.\", \"expand\": true}], \"envelope\": {\"is_autoreply\": false, \"from\": [{\"email\": \"morgan@nested.com\", \"name\": \"\"}], \"sender\": [{\"email\": \"morgan@nested.com\", \"name\": \"\"}], \"cc\": [], \"bcc\": [], \"to\": [{\"email\": \"phil+sample@close.com\", \"name\": \"\"}], \"date\": \"Fri, 01 Feb 2013 00:54:51 -0000\", \"reply_to\": [], \"in_reply_to\": null, \"message_id\": null, \"subject\": \"Welcome to Close!\"}, \"body_html\": \"Hi! My name is Karan and I'm your Account Manager.\", \"organization_id\": \"orga_CC25dsMNG4KsRpxn2LEwPUVyGRs4poLYFIBsioIK4Oj\", \"sequence_id\": null, \"sequence_subscription_id\": null, \"users\": [], \"need_smtp_credentials\": false, \"body_text_quoted\": [{\"text\": \"Hi! My name is Karan and I'm your Account Manager.\", \"expand\": true}], \"cc\": [], \"lead_id\": \"lead_sA6s1qN2hh1eDHipGxVAKvFlWCEG4xglutTXVUgnNkn\", \"has_reply\": false, \"sender\": \"morgan@nested.com\", \"bcc\": [], \"email_account_id\": null, \"date_sent\": null, \"date_created\": \"2013-02-01T00:54:51.274000+00:00\", \"date_scheduled\": null, \"opens_summary\": null, \"template_id\": null, \"bulk_email_action_id\": null, \"followup_sequence_delay\": null}", + "headers": { + "Content-Type": "application/json", + "Date": "Wed, 15 Apr 2020 16:22:22 GMT", + "Referrer-Policy": "no-referrer-when-downgrade", + "Set-Cookie": "session=; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/", + "Strict-Transport-Security": "max-age=31556926; includeSubDomains", + "Vary": "Accept", + "X-Content-Type-Options": "nosniff", + "X-Frame-Options": "SAMEORIGIN", + "X-Rate-Limit-Limit": "320", + "X-Rate-Limit-Remaining": "319", + "X-Rate-Limit-Reset": "7.789782", + "X-XSS-Protection": "1; mode=block", + "Content-Length": "2186", + "Connection": "keep-alive" + }, + "status_code": 200, + "type": "ok" + } + } +] diff --git a/test/http_client_test.exs b/test/http_client_test.exs index 55d7c43..2eeb5f6 100644 --- a/test/http_client_test.exs +++ b/test/http_client_test.exs @@ -1027,6 +1027,86 @@ defmodule Closex.HTTPClientTest do end end + describe "create_email_activity/1" do + test "creates an email activity" do + test_email = %{ + contact_id: "cont_gwto1MOgh8nNGpCVn5DFJkmMxZYY6oQbh8X9mS5uEXg", + lead_id: "lead_sA6s1qN2hh1eDHipGxVAKvFlWCEG4xglutTXVUgnNkn", + direction: "outgoing", + created_by: nil, + created_by_name: "Bruce Wayne", + date_created: "2013-02-01T00:54:51.274000+00:00", + subject: "Welcome to Close!", + sender: "morgan@nested.com", + to: [ + "phil+sample@close.com" + ], + bcc: [], + cc: [], + status: "draft", + body_text: "Hi! My name is Karan and I'm your Account Manager.", + body_html: "Hi! My name is Karan and I'm your Account Manager.", + attachments: [], + template_id: nil + } + + use_cassette "create_email_activity" do + {:ok, body} = create_email_activity(test_email) + + assert Map.keys(body) == [ + "_type", + "sender", + "users", + "body_text", + "organization_id", + "bcc", + "body_html_quoted", + "created_by", + "created_by_name", + "id", + "date_sent", + "date_created", + "template_id", + "opens_summary", + "user_id", + "cc", + "body_text_quoted", + "date_updated", + "need_smtp_credentials", + "template_name", + "updated_by", + "status", + "message_ids", + "thread_id", + "sequence_subscription_id", + "send_attempts", + "contact_id", + "sequence_id", + "in_reply_to_id", + "opens", + "followup_sequence_id", + "attachments", + "envelope", + "updated_by_name", + "followup_sequence_delay", + "has_reply", + "lead_id", + "references", + "direction", + "bulk_email_action_id", + "body_preview", + "date_scheduled", + "to", + "email_account_id", + "user_name", + "subject", + "sequence_name", + "body_html" + ] + end + end + end + describe "find_call_activities/1" do test "returns a list of call activities" do search_term = "lead_id=lead_ZNjVC99F8sKnz4FWKKCBxIsamaEsasV7kN8aoZCl5zM"