diff --git a/lib/ex_force.ex b/lib/ex_force.ex index e5752c1..4ad024e 100644 --- a/lib/ex_force.ex +++ b/lib/ex_force.ex @@ -376,6 +376,23 @@ defmodule ExForce do ) end + @doc """ + Get recently viewed items + """ + @spec get_recently_viewed_items(client, limit :: integer) :: + {:ok, Enumerable.t()} | {:error, any()} + def get_recently_viewed_items(client, limit) do + case Client.request(client, %Request{ + method: :get, + url: "recent/?limit=#{limit}" + }) do + {:ok, %Response{status: 200, body: []}} -> {:ok, []} + {:ok, %Response{status: 200, body: body}} -> {:ok, SObject.build(body)} + {:ok, %Response{body: body}} -> {:error, body} + {:error, _} = other -> other + end + end + defp stream_next({client, :halt}), do: {:halt, client} defp stream_next({client, {:error, _} = error_tuple}), do: {[error_tuple], {client, :halt}} diff --git a/lib/ex_force/sobject.ex b/lib/ex_force/sobject.ex index e1681b9..e6fc833 100644 --- a/lib/ex_force/sobject.ex +++ b/lib/ex_force/sobject.ex @@ -13,6 +13,12 @@ defmodule ExForce.SObject do @spec build(map) :: t def build(%{"attributes" => %{}} = raw), do: do_build(raw) + def build([%{"attributes" => %{"url" => _, "type" => _}, "Id" => _, "Name" => _} | _] = raw) do + Enum.map(raw, fn val -> + do_build(val) + end) + end + defp do_build(%{"attributes" => %{"type" => type, "url" => url}} = val) do id = url |> String.split("/") |> List.last() %__MODULE__{type: type, id: id, data: do_build_data(val)} diff --git a/test/ex_force_test.exs b/test/ex_force_test.exs index ab8cd95..160047a 100644 --- a/test/ex_force_test.exs +++ b/test/ex_force_test.exs @@ -1202,5 +1202,83 @@ defmodule ExForceTest do ] end + test "get_recently_viewed_items/2 - no recently viewed items", %{bypass: bypass, client: client} do + Bypass.expect_once(bypass, "GET", "/services/data/v53.0/recent/", fn conn -> + %{"limit" => "2"} = URI.decode_query(conn.query_string) + + conn + |> Conn.put_resp_content_type("application/json") + |> Conn.resp(200, """ + [] + """) + end) + + assert ExForce.get_recently_viewed_items(client, 2) == + {:ok, []} + end + + test "get_recently_viewed_items/2", %{bypass: bypass, client: client} do + Bypass.expect_once(bypass, "GET", "/services/data/v53.0/recent/", fn conn -> + %{"limit" => "2"} = URI.decode_query(conn.query_string) + + conn + |> Conn.put_resp_content_type("application/json") + |> Conn.resp(200, """ + [{ + "attributes" : + { + "type" : "Account", + "url" : "/services/data/v53.0/sobjects/Account/a06U000000CelH0IAJ" + }, + "Id" : "a06U000000CelH0IAJ", + "Name" : "Acme" + }, + { + "attributes" : + { + "type" : "Opportunity", + "url" : "/services/data/v53.0/sobjects/Opportunity/a06U000000CelGvIAJ" + }, + "Id" : "a06U000000CelGvIAJ", + "Name" : "Acme - 600 Widgets" + }] + """) + end) + + assert ExForce.get_recently_viewed_items(client, 2) == + {:ok, + [ + %SObject{ + id: "a06U000000CelH0IAJ", + type: "Account", + data: %{ + "Id" => "a06U000000CelH0IAJ", + "Name" => "Acme" + } + }, + %SObject{ + id: "a06U000000CelGvIAJ", + type: "Opportunity", + data: %{"Id" => "a06U000000CelGvIAJ", "Name" => "Acme - 600 Widgets"} + } + ]} + end + + test "get_recently_viewed_items/2 - failure", %{bypass: bypass, client: client} do + Bypass.expect_once(bypass, "GET", "/services/data/v53.0/recent/", fn conn -> + %{"limit" => "2"} = URI.decode_query(conn.query_string) + + conn + |> Conn.put_resp_content_type("application/json") + |> Conn.resp(500, """ + [{ + }] + """) + end) + + assert ExForce.get_recently_viewed_items(client, 2) == + {:error, [%{}]} + end + defp get(client, url), do: Client.request(client, %Request{url: url, method: :get}) end