Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396)
* Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>pull/3400/head r2023.10.05-202
parent
d608fec903
commit
0007c0c108
@ -0,0 +1,77 @@
|
||||
defmodule Plausible.Plugins.API.Goals do
|
||||
@moduledoc """
|
||||
Plugins API context module for Goals.
|
||||
All high level Goal operations should be implemented here.
|
||||
"""
|
||||
|
||||
import Ecto.Query
|
||||
import Plausible.Plugins.API.Pagination
|
||||
|
||||
alias Plausible.Repo
|
||||
alias PlausibleWeb.Plugins.API.Schemas.Goal.CreateRequest
|
||||
|
||||
@type create_request() ::
|
||||
CreateRequest.CustomEvent.t()
|
||||
| CreateRequest.Revenue.t()
|
||||
| CreateRequest.Pageview.t()
|
||||
|
||||
@spec create(
|
||||
Plausible.Site.t(),
|
||||
create_request() | list(create_request())
|
||||
) :: {:ok, list(Plausible.Goal.t())} | {:error, Ecto.Changeset.t()}
|
||||
def create(site, goal_or_goals) do
|
||||
Repo.transaction(fn -> find_or_create(site, goal_or_goals) end)
|
||||
end
|
||||
|
||||
@spec get_goals(Plausible.Site.t(), map()) :: {:ok, Paginator.Page.t()}
|
||||
def get_goals(site, params) do
|
||||
query = Plausible.Goals.for_site_query(site, preload_funnels?: true)
|
||||
|
||||
{:ok, paginate(query, params, cursor_fields: [{:id, :desc}])}
|
||||
end
|
||||
|
||||
@spec get(Plausible.Site.t(), pos_integer()) :: nil | Plausible.Goal.t()
|
||||
def get(site, id) when is_integer(id) do
|
||||
site
|
||||
|> get_query()
|
||||
|> where([g], g.id == ^id)
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
defp get_query(site) do
|
||||
from g in Plausible.Goal,
|
||||
where: g.site_id == ^site.id,
|
||||
order_by: [desc: g.id],
|
||||
left_join: assoc(g, :funnels),
|
||||
group_by: g.id,
|
||||
preload: [:funnels]
|
||||
end
|
||||
|
||||
defp convert_to_create_params(%CreateRequest.CustomEvent{goal: %{event_name: event_name}}) do
|
||||
%{"goal_type" => "event", "event_name" => event_name}
|
||||
end
|
||||
|
||||
defp convert_to_create_params(%CreateRequest.Revenue{
|
||||
goal: %{event_name: event_name, currency: currency}
|
||||
}) do
|
||||
%{"goal_type" => "event", "event_name" => event_name, "currency" => currency}
|
||||
end
|
||||
|
||||
defp convert_to_create_params(%CreateRequest.Pageview{goal: %{path: page_path}}) do
|
||||
%{"goal_type" => "page", "page_path" => page_path}
|
||||
end
|
||||
|
||||
defp find_or_create(site, goal_or_goals) do
|
||||
goal_or_goals
|
||||
|> List.wrap()
|
||||
|> Enum.map(fn goal ->
|
||||
case Plausible.Goals.find_or_create(site, convert_to_create_params(goal)) do
|
||||
{:ok, goal} ->
|
||||
goal
|
||||
|
||||
{:error, changeset} ->
|
||||
Repo.rollback(changeset)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
@ -1,6 +1,6 @@
|
||||
defmodule Plausible.Plugins.API.Context.SharedLinks do
|
||||
defmodule Plausible.Plugins.API.SharedLinks do
|
||||
@moduledoc """
|
||||
Plugins API Context module for Shared Links.
|
||||
Plugins API context module for Shared Links.
|
||||
All high level Shared Links operations should be implemented here.
|
||||
"""
|
||||
import Ecto.Query
|
||||
@ -0,0 +1,160 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Controllers.Goals do
|
||||
@moduledoc """
|
||||
Controller for the Goal resource under Plugins API
|
||||
"""
|
||||
use PlausibleWeb, :plugins_api_controller
|
||||
|
||||
operation(:index,
|
||||
summary: "Retrieve Goals",
|
||||
parameters: [
|
||||
limit: [in: :query, type: :integer, description: "Maximum entries per page", example: 10],
|
||||
after: [
|
||||
in: :query,
|
||||
type: :string,
|
||||
description: "Cursor value to seek after - generated internally"
|
||||
],
|
||||
before: [
|
||||
in: :query,
|
||||
type: :string,
|
||||
description: "Cursor value to seek before - generated internally"
|
||||
]
|
||||
],
|
||||
responses: %{
|
||||
ok: {"Goals response", "application/json", Schemas.Goal.ListResponse},
|
||||
unauthorized: {"Unauthorized", "application/json", Schemas.Unauthorized}
|
||||
}
|
||||
)
|
||||
|
||||
operation(:create,
|
||||
id: "Goal.GetOrCreate",
|
||||
summary: "Get or create Goal",
|
||||
request_body: {"Goal params", "application/json", Schemas.Goal.CreateRequest},
|
||||
responses: %{
|
||||
created:
|
||||
{"Goal", "application/json",
|
||||
%OpenApiSpex.Schema{
|
||||
oneOf: [Schemas.Goal.ListResponse, Schemas.Goal]
|
||||
}},
|
||||
unauthorized: {"Unauthorized", "application/json", Schemas.Unauthorized},
|
||||
unprocessable_entity:
|
||||
{"Unprocessable entity", "application/json", Schemas.UnprocessableEntity}
|
||||
}
|
||||
)
|
||||
|
||||
@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
||||
def create(
|
||||
%{private: %{open_api_spex: %{body_params: %{goal: _} = goal}}} = conn,
|
||||
_params
|
||||
) do
|
||||
site = conn.assigns.authorized_site
|
||||
|
||||
case API.Goals.create(site, goal) do
|
||||
{:ok, [goal]} ->
|
||||
conn
|
||||
|> put_view(Views.Goal)
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", goals_url(base_uri(), :get, goal.id))
|
||||
|> render("goal.json", goal: goal, authorized_site: site)
|
||||
|
||||
{:error, changeset} ->
|
||||
Errors.error(conn, 422, changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def create(
|
||||
%{private: %{open_api_spex: %{body_params: %{goals: goals}}}} = conn,
|
||||
_params
|
||||
) do
|
||||
site = conn.assigns.authorized_site
|
||||
|
||||
case API.Goals.create(site, goals) do
|
||||
{:ok, goals} ->
|
||||
location_headers = Enum.map(goals, &{"location", goals_url(base_uri(), :get, &1.id)})
|
||||
|
||||
conn
|
||||
|> prepend_resp_headers(location_headers)
|
||||
|> put_view(Views.Goal)
|
||||
|> put_status(:created)
|
||||
|> render("index.json", goals: goals, authorized_site: site)
|
||||
|
||||
{:error, changeset} ->
|
||||
Errors.error(conn, 422, changeset)
|
||||
end
|
||||
end
|
||||
|
||||
@spec index(Plug.Conn.t(), %{}) :: Plug.Conn.t()
|
||||
def index(conn, _params) do
|
||||
{:ok, pagination} = API.Goals.get_goals(conn.assigns.authorized_site, conn.query_params)
|
||||
|
||||
conn
|
||||
|> put_view(Views.Goal)
|
||||
|> render("index.json", %{pagination: pagination})
|
||||
end
|
||||
|
||||
operation(:get,
|
||||
summary: "Retrieve Goal by ID",
|
||||
parameters: [
|
||||
id: [
|
||||
in: :path,
|
||||
type: :integer,
|
||||
description: "Goal ID",
|
||||
example: 123,
|
||||
required: true
|
||||
]
|
||||
],
|
||||
responses: %{
|
||||
ok: {"Goal", "application/json", Schemas.Goal},
|
||||
not_found: {"NotFound", "application/json", Schemas.NotFound},
|
||||
unauthorized: {"Unauthorized", "application/json", Schemas.Unauthorized},
|
||||
unprocessable_entity:
|
||||
{"Unprocessable entity", "application/json", Schemas.UnprocessableEntity}
|
||||
}
|
||||
)
|
||||
|
||||
@spec get(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
||||
def get(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _params) do
|
||||
site = conn.assigns.authorized_site
|
||||
|
||||
case API.Goals.get(site, id) do
|
||||
nil ->
|
||||
conn
|
||||
|> put_view(Views.Error)
|
||||
|> put_status(:not_found)
|
||||
|> render("404.json")
|
||||
|
||||
goal ->
|
||||
conn
|
||||
|> put_view(Views.Goal)
|
||||
|> put_status(:ok)
|
||||
|> render("goal.json", goal: goal, authorized_site: site)
|
||||
end
|
||||
end
|
||||
|
||||
operation(:delete,
|
||||
summary: "Delete Goal by ID",
|
||||
parameters: [
|
||||
id: [
|
||||
in: :path,
|
||||
type: :integer,
|
||||
description: "Goal ID",
|
||||
example: 123,
|
||||
required: true
|
||||
]
|
||||
],
|
||||
responses: %{
|
||||
no_content: {"NoContent", nil, nil},
|
||||
unauthorized: {"Unauthorized", "application/json", Schemas.Unauthorized}
|
||||
}
|
||||
)
|
||||
|
||||
@spec delete(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
||||
def delete(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _params) do
|
||||
case Plausible.Goals.delete(id, conn.assigns.authorized_site) do
|
||||
:ok ->
|
||||
send_resp(conn, :no_content, "")
|
||||
|
||||
{:error, :not_found} ->
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,32 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Goal
|
||||
"""
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "Goal",
|
||||
description: "Goal object",
|
||||
type: :object,
|
||||
discriminator: %OpenApiSpex.Discriminator{
|
||||
propertyName: "goal_type",
|
||||
mapping: %{
|
||||
"Goal.CustomEvent" => Schemas.Goal.CustomEvent,
|
||||
"Goal.Pageview" => Schemas.Goal.Pageview,
|
||||
"Goal.Revenue" => Schemas.Goal.Revenue
|
||||
}
|
||||
},
|
||||
oneOf: [
|
||||
Schemas.Goal.CustomEvent,
|
||||
Schemas.Goal.Revenue,
|
||||
Schemas.Goal.Pageview
|
||||
],
|
||||
example: %{
|
||||
goal_type: "Goal.Revenue",
|
||||
goal: %{
|
||||
currency: "EUR",
|
||||
event_name: "Purchase"
|
||||
}
|
||||
}
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,38 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.CreateRequest do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Goal creation request
|
||||
"""
|
||||
use Plausible.Funnel
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "Goal.CreateRequest",
|
||||
description: "Goal creation params",
|
||||
type: :object,
|
||||
oneOf: [
|
||||
%Schema{
|
||||
title: "Goal.CreateRequest.BulkGetOrCreate",
|
||||
type: :object,
|
||||
description: "Bulk goal creation request",
|
||||
required: [:goals],
|
||||
properties: %{
|
||||
goals: %Schema{
|
||||
type: :array,
|
||||
minItems: 1,
|
||||
maxItems: Funnel.max_steps(),
|
||||
items: %Schema{
|
||||
oneOf: [
|
||||
Schemas.Goal.CreateRequest.CustomEvent,
|
||||
Schemas.Goal.CreateRequest.Revenue,
|
||||
Schemas.Goal.CreateRequest.Pageview
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Schemas.Goal.CreateRequest.CustomEvent,
|
||||
Schemas.Goal.CreateRequest.Revenue,
|
||||
Schemas.Goal.CreateRequest.Pageview
|
||||
]
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,34 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.CreateRequest.CustomEvent do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Custom Event Goal creation request
|
||||
"""
|
||||
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "Goal.CreateRequest.CustomEvent",
|
||||
description: "Custom Event Goal creation params",
|
||||
type: :object,
|
||||
required: [:goal, :goal_type],
|
||||
properties: %{
|
||||
goal_type: %Schema{
|
||||
type: :string,
|
||||
enum: ["Goal.CustomEvent"],
|
||||
default: "Goal.CustomEvent"
|
||||
},
|
||||
goal: %Schema{
|
||||
type: :object,
|
||||
required: [:event_name],
|
||||
properties: %{
|
||||
event_name: %Schema{type: :string}
|
||||
}
|
||||
}
|
||||
},
|
||||
example: %{
|
||||
goal_type: "Goal.CustomEvent",
|
||||
goal: %{
|
||||
event_name: "Signup"
|
||||
}
|
||||
}
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,34 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.CreateRequest.Pageview do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Pageview Goal creation request
|
||||
"""
|
||||
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "Goal.CreateRequest.Pageview",
|
||||
description: "Pageview Goal creation params",
|
||||
type: :object,
|
||||
required: [:goal, :goal_type],
|
||||
properties: %{
|
||||
goal_type: %Schema{
|
||||
type: :string,
|
||||
enum: ["Goal.Pageview"],
|
||||
default: "Goal.Pageview"
|
||||
},
|
||||
goal: %Schema{
|
||||
type: :object,
|
||||
required: [:path],
|
||||
properties: %{
|
||||
path: %Schema{type: :string}
|
||||
}
|
||||
}
|
||||
},
|
||||
example: %{
|
||||
goal_type: "Goal.Pageview",
|
||||
goal: %{
|
||||
path: "/checkout"
|
||||
}
|
||||
}
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,36 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.CreateRequest.Revenue do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Custom Event Goal creation request
|
||||
"""
|
||||
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "Goal.CreateRequest.Revenue",
|
||||
description: "Revenue Goal creation params",
|
||||
type: :object,
|
||||
required: [:goal, :goal_type],
|
||||
properties: %{
|
||||
goal_type: %Schema{
|
||||
type: :string,
|
||||
enum: ["Goal.Revenue"],
|
||||
default: "Goal.Revenue"
|
||||
},
|
||||
goal: %Schema{
|
||||
type: :object,
|
||||
required: [:event_name, :currency],
|
||||
properties: %{
|
||||
event_name: %Schema{type: :string},
|
||||
currency: %Schema{type: :string}
|
||||
}
|
||||
}
|
||||
},
|
||||
example: %{
|
||||
goal_type: "Goal.Revenue",
|
||||
goal: %{
|
||||
event_name: "Purchase",
|
||||
currency: "EUR"
|
||||
}
|
||||
}
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,30 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.CustomEvent do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Custom Event Goal object
|
||||
"""
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
description: "Custom Event Goal object",
|
||||
title: "Goal.CustomEvent",
|
||||
type: :object,
|
||||
allOf: [
|
||||
Schemas.Goal.Type,
|
||||
%Schema{
|
||||
type: :object,
|
||||
required: [:goal],
|
||||
properties: %{
|
||||
goal: %Schema{
|
||||
type: :object,
|
||||
required: [:id, :display_name, :event_name],
|
||||
properties: %{
|
||||
id: %Schema{type: :integer, description: "Goal ID", readOnly: true},
|
||||
display_name: %Schema{type: :string, description: "Display name", readOnly: true},
|
||||
event_name: %Schema{type: :string, description: "Event Name"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,29 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.ListResponse do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Goals list response
|
||||
"""
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "Goal.ListResponse",
|
||||
description: "Goals list response",
|
||||
type: :object,
|
||||
required: [:goals, :meta],
|
||||
properties: %{
|
||||
goals: %Schema{
|
||||
items: Schemas.Goal,
|
||||
type: :array
|
||||
},
|
||||
meta: %OpenApiSpex.Schema{
|
||||
required: [:pagination],
|
||||
properties: %{
|
||||
pagination: %OpenApiSpex.Reference{
|
||||
"$ref": "#/components/schemas/PaginationMetadata"
|
||||
}
|
||||
},
|
||||
type: :object,
|
||||
items: Schemas.PaginationMetadata
|
||||
}
|
||||
}
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,30 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.Pageview do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Pageview Goal object
|
||||
"""
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
description: "Pageview Goal object",
|
||||
title: "Goal.Pageview",
|
||||
type: :object,
|
||||
allOf: [
|
||||
Schemas.Goal.Type,
|
||||
%Schema{
|
||||
type: :object,
|
||||
required: [:goal],
|
||||
properties: %{
|
||||
goal: %Schema{
|
||||
type: :object,
|
||||
required: [:id, :display_name, :path],
|
||||
properties: %{
|
||||
id: %Schema{type: :integer, description: "Goal ID", readOnly: true},
|
||||
display_name: %Schema{type: :string, description: "Display name", readOnly: true},
|
||||
path: %Schema{type: :string, description: "Page Path"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,31 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.Revenue do
|
||||
@moduledoc """
|
||||
OpenAPI schema for Revenue Goal object
|
||||
"""
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
description: "Revenue Goal object",
|
||||
title: "Goal.Revenue",
|
||||
type: :object,
|
||||
allOf: [
|
||||
Schemas.Goal.Type,
|
||||
%Schema{
|
||||
type: :object,
|
||||
required: [:goal],
|
||||
properties: %{
|
||||
goal: %Schema{
|
||||
type: :object,
|
||||
required: [:id, :display_name, :event_name, :currency],
|
||||
properties: %{
|
||||
id: %Schema{type: :integer, description: "Goal ID", readOnly: true},
|
||||
display_name: %Schema{type: :string, description: "Display name", readOnly: true},
|
||||
event_name: %Schema{type: :string, description: "Event Name"},
|
||||
currency: %Schema{type: :string, description: "Currency"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,18 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Schemas.Goal.Type do
|
||||
@moduledoc """
|
||||
OpenAPI schema for common Goal Type
|
||||
|
||||
Future-proof: funnels etc.
|
||||
"""
|
||||
use PlausibleWeb, :open_api_schema
|
||||
|
||||
OpenApiSpex.schema(%{
|
||||
title: "Goal.Type",
|
||||
description: "Properties common to all Goals",
|
||||
type: :object,
|
||||
properties: %{
|
||||
goal_type: %Schema{type: :string}
|
||||
},
|
||||
required: [:goal_type]
|
||||
})
|
||||
end
|
||||
@ -0,0 +1,70 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Views.Goal do
|
||||
@moduledoc """
|
||||
View for rendering Goals in the Plugins API
|
||||
"""
|
||||
|
||||
use PlausibleWeb, :plugins_api_view
|
||||
|
||||
def render("index.json", %{
|
||||
pagination: %{entries: goals, metadata: metadata},
|
||||
authorized_site: site,
|
||||
conn: conn
|
||||
}) do
|
||||
%{
|
||||
goals: render_many(goals, __MODULE__, "goal.json", authorized_site: site),
|
||||
meta: render_metadata_links(metadata, :goals_url, :index, conn.query_params)
|
||||
}
|
||||
end
|
||||
|
||||
def render("index.json", %{
|
||||
goals: goals,
|
||||
authorized_site: site,
|
||||
conn: conn
|
||||
}) do
|
||||
%{
|
||||
goals: render_many(goals, __MODULE__, "goal.json", authorized_site: site),
|
||||
meta: render_metadata_links(%{}, :goals_url, :index, conn.query_params)
|
||||
}
|
||||
end
|
||||
|
||||
def render("goal.json", %{
|
||||
goal: %{event_name: nil} = pageview
|
||||
}) do
|
||||
%{
|
||||
goal_type: "Goal.Pageview",
|
||||
goal: %{
|
||||
id: pageview.id,
|
||||
display_name: to_string(pageview),
|
||||
path: pageview.page_path
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def render("goal.json", %{
|
||||
goal: %{page_path: nil, currency: nil} = custom_event
|
||||
}) do
|
||||
%{
|
||||
goal_type: "Goal.CustomEvent",
|
||||
goal: %{
|
||||
id: custom_event.id,
|
||||
display_name: to_string(custom_event),
|
||||
event_name: custom_event.event_name
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def render("goal.json", %{
|
||||
goal: %{page_path: nil, currency: currency} = revenue_goal
|
||||
})
|
||||
when is_atom(currency) do
|
||||
%{
|
||||
goal_type: "Goal.Revenue",
|
||||
goal: %{
|
||||
id: revenue_goal.id,
|
||||
display_name: to_string(revenue_goal),
|
||||
event_name: revenue_goal.event_name,
|
||||
currency: revenue_goal.currency
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,546 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Controllers.GoalsTest do
|
||||
use PlausibleWeb.PluginsAPICase, async: true
|
||||
alias PlausibleWeb.Plugins.API.Schemas
|
||||
|
||||
describe "examples" do
|
||||
test "Goal.CreateRequest.Revenue" do
|
||||
assert_schema(
|
||||
Schemas.Goal.CreateRequest.Revenue.schema().example,
|
||||
"Goal.CreateRequest.Revenue",
|
||||
spec()
|
||||
)
|
||||
end
|
||||
|
||||
test "Goal.CreateRequest.Pageview" do
|
||||
assert_schema(
|
||||
Schemas.Goal.CreateRequest.Pageview.schema().example,
|
||||
"Goal.CreateRequest.Pageview",
|
||||
spec()
|
||||
)
|
||||
end
|
||||
|
||||
test "Goal.CreateRequest.CustomEvent" do
|
||||
assert_schema(
|
||||
Schemas.Goal.CreateRequest.CustomEvent.schema().example,
|
||||
"Goal.CreateRequest.CustomEvent",
|
||||
spec()
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "unauthorized calls" do
|
||||
for {method, url} <- [
|
||||
{:get, Routes.goals_url(base_uri(), :index)},
|
||||
{:get, Routes.goals_url(base_uri(), :get, 1)},
|
||||
{:put, Routes.goals_url(base_uri(), :create, %{})},
|
||||
{:delete, Routes.goals_url(base_uri(), :delete, 1)}
|
||||
] do
|
||||
test "unauthorized call: #{method} #{url}", %{conn: conn} do
|
||||
conn
|
||||
|> unquote(method)(unquote(url))
|
||||
|> json_response(401)
|
||||
|> assert_schema("UnauthorizedError", spec())
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "put /goals - create a single goal" do
|
||||
test "validates input according to the schema", %{conn: conn, token: token, site: site} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put(url, %{goal_type: "Goal.SomeTypo", goal: %{event_name: "Signup"}})
|
||||
|> json_response(422)
|
||||
|> assert_schema("UnprocessableEntityError", spec())
|
||||
end
|
||||
|
||||
test "creates a custom event goal", %{conn: conn, token: token, site: site} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
payload = %{goal_type: "Goal.CustomEvent", goal: %{event_name: "Signup"}}
|
||||
|
||||
assert_request_schema(payload, "Goal.CreateRequest.CustomEvent", spec())
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put(url, payload)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal", spec())
|
||||
|> assert_schema("Goal.CustomEvent", spec())
|
||||
|
||||
[location] = get_resp_header(conn, "location")
|
||||
|
||||
assert location ==
|
||||
Routes.goals_url(base_uri(), :get, resp.goal.id)
|
||||
|
||||
assert [%{event_name: "Signup"}] = Plausible.Goals.for_site(site)
|
||||
end
|
||||
|
||||
test "creates a revenue goal", %{conn: conn, token: token, site: site} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
payload = %{
|
||||
goal_type: "Goal.Revenue",
|
||||
goal: %{event_name: "Purchase", currency: "EUR"}
|
||||
}
|
||||
|
||||
assert_request_schema(payload, "Goal.CreateRequest.Revenue", spec())
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put(url, payload)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal", spec())
|
||||
|> assert_schema("Goal.Revenue", spec())
|
||||
|
||||
[location] = get_resp_header(conn, "location")
|
||||
|
||||
assert location ==
|
||||
Routes.goals_url(base_uri(), :get, resp.goal.id)
|
||||
|
||||
assert [%{event_name: "Purchase", currency: :EUR}] = Plausible.Goals.for_site(site)
|
||||
end
|
||||
|
||||
test "fails to create a revenue goal with unknown currency", %{
|
||||
conn: conn,
|
||||
token: token,
|
||||
site: site
|
||||
} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
payload = %{
|
||||
goal_type: "Goal.Revenue",
|
||||
goal: %{event_name: "Purchase", currency: "DFJKHJESFHYU"}
|
||||
}
|
||||
|
||||
assert_request_schema(payload, "Goal.CreateRequest.Revenue", spec())
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put(url, payload)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> json_response(422)
|
||||
|> assert_schema("UnprocessableEntityError", spec())
|
||||
|
||||
assert [%{detail: "currency: is invalid"}] = resp.errors
|
||||
end
|
||||
|
||||
test "edge case - revenue goal exists under the same name and different currency", %{
|
||||
conn: conn,
|
||||
token: token,
|
||||
site: site
|
||||
} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
{:ok, _} = Plausible.Goals.create(site, %{"event_name" => "Purchase", "currency" => "USD"})
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put(url, %{goal_type: "Goal.Revenue", goal: %{event_name: "Purchase", currency: "EUR"}})
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> json_response(422)
|
||||
|> assert_schema("UnprocessableEntityError", spec())
|
||||
|
||||
assert [%{detail: "event_name: 'Purchase' (with currency: USD) has already been taken"}] =
|
||||
resp.errors
|
||||
end
|
||||
|
||||
test "creates a pageview goal", %{conn: conn, token: token, site: site} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
payload = %{goal_type: "Goal.Pageview", goal: %{path: "/checkout"}}
|
||||
assert_request_schema(payload, "Goal.CreateRequest.Pageview", spec())
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put(url, payload)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal", spec())
|
||||
|> assert_schema("Goal.Pageview", spec())
|
||||
|
||||
[location] = get_resp_header(conn, "location")
|
||||
|
||||
assert location ==
|
||||
Routes.goals_url(base_uri(), :get, resp.goal.id)
|
||||
|
||||
assert [%{page_path: "/checkout"}] = Plausible.Goals.for_site(site)
|
||||
end
|
||||
|
||||
test "is idempotent", %{conn: conn, token: token, site: site} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
initial_conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|
||||
resp1 =
|
||||
initial_conn
|
||||
|> put(url, %{goal_type: "Goal.Pageview", goal: %{path: "/checkout"}})
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal.Pageview", spec())
|
||||
|
||||
assert initial_conn
|
||||
|> put(url, %{goal_type: "Goal.Pageview", goal: %{path: "/checkout"}})
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal.Pageview", spec()) == resp1
|
||||
end
|
||||
end
|
||||
|
||||
describe "put /goals - bulk creation" do
|
||||
test "creates a goal of each type", %{conn: conn, token: token, site: site} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
payload = %{
|
||||
goals: [
|
||||
%{
|
||||
goal_type: "Goal.CustomEvent",
|
||||
goal: %{event_name: "Signup"}
|
||||
},
|
||||
%{
|
||||
goal_type: "Goal.Revenue",
|
||||
goal: %{event_name: "Purchase", currency: "EUR"}
|
||||
},
|
||||
%{
|
||||
goal_type: "Goal.Pageview",
|
||||
goal: %{path: "/checkout"}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
assert_request_schema(payload, "Goal.CreateRequest.BulkGetOrCreate", spec())
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put(url, payload)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
|
||||
[l1, l2, l3] = get_resp_header(conn, "location")
|
||||
|
||||
assert l1 == Routes.goals_url(base_uri(), :get, Enum.at(resp.goals, 0).goal.id)
|
||||
assert l2 == Routes.goals_url(base_uri(), :get, Enum.at(resp.goals, 1).goal.id)
|
||||
assert l3 == Routes.goals_url(base_uri(), :get, Enum.at(resp.goals, 2).goal.id)
|
||||
|
||||
assert Enum.count(resp.goals) == 3
|
||||
|
||||
assert [
|
||||
%{page_path: "/checkout"},
|
||||
%{event_name: "Purchase", currency: :EUR},
|
||||
%{event_name: "Signup"}
|
||||
] = Plausible.Goals.for_site(site)
|
||||
end
|
||||
|
||||
test "no more than 8 goals can be created in bulk", %{conn: conn, token: token, site: site} do
|
||||
# if this test fails due to implementation change, consider what to do with the pagination meta
|
||||
# object returned in the response and also revise how funnels are created based on a list of goals
|
||||
# - the funnels creation endpoint will likely reuse this schema's constraints
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
payload =
|
||||
Enum.map(1..9, fn i ->
|
||||
%{goal_type: "Goal.CustomEvent", goal: %{event_name: "Bulk Goal ##{i}"}}
|
||||
end)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> put(url, %{
|
||||
goals: payload
|
||||
})
|
||||
|> json_response(422)
|
||||
|> assert_schema("UnprocessableEntityError", spec())
|
||||
|
||||
assert %Schemas.Error{
|
||||
detail: "Array length 9 is larger than maxItems: 8"
|
||||
} in resp.errors
|
||||
end
|
||||
|
||||
test "is idempotent", %{conn: conn, token: token, site: site} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
initial_conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|
||||
payload = [
|
||||
%{
|
||||
goal_type: "Goal.CustomEvent",
|
||||
goal: %{event_name: "Signup"}
|
||||
},
|
||||
%{
|
||||
goal_type: "Goal.Revenue",
|
||||
goal: %{event_name: "Purchase", currency: "EUR"}
|
||||
},
|
||||
%{
|
||||
goal_type: "Goal.Pageview",
|
||||
goal: %{path: "/checkout"}
|
||||
}
|
||||
]
|
||||
|
||||
initial_conn
|
||||
|> put(url, %{goals: payload})
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
|
||||
initial_conn
|
||||
|> put(url, %{goals: payload})
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
end
|
||||
|
||||
test "edge case - revenue goals exist under the same name and different currency", %{
|
||||
conn: conn,
|
||||
token: token,
|
||||
site: site
|
||||
} do
|
||||
url = Routes.goals_url(base_uri(), :create)
|
||||
|
||||
initial_conn =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|
||||
payload1 = [%{goal_type: "Goal.Revenue", goal: %{event_name: "Purchase", currency: "EUR"}}]
|
||||
payload2 = [%{goal_type: "Goal.Revenue", goal: %{event_name: "Purchase", currency: "USD"}}]
|
||||
|
||||
initial_conn
|
||||
|> put(url, %{goals: payload1})
|
||||
|> json_response(201)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
|
||||
resp =
|
||||
initial_conn
|
||||
|> put(url, %{goals: payload2})
|
||||
|> json_response(422)
|
||||
|> assert_schema("UnprocessableEntityError", spec())
|
||||
|
||||
assert [%{detail: "event_name: 'Purchase' (with currency: EUR) has already been taken"}] =
|
||||
resp.errors
|
||||
end
|
||||
end
|
||||
|
||||
describe "get /goals/:id" do
|
||||
test "validates input out of the box", %{conn: conn, token: token, site: site} do
|
||||
url = Routes.goals_url(base_uri(), :get, "hello")
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> get(url)
|
||||
|> json_response(422)
|
||||
|> assert_schema("UnprocessableEntityError", spec())
|
||||
|
||||
assert %{errors: [%{detail: "Invalid integer. Got: string"}]} = resp
|
||||
end
|
||||
|
||||
test "retrieves revenue goal by ID", %{conn: conn, site: site, token: token} do
|
||||
{:ok, goal} =
|
||||
Plausible.Goals.create(site, %{"event_name" => "Purchase", "currency" => "EUR"})
|
||||
|
||||
url = Routes.goals_url(base_uri(), :get, goal.id)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> get(url)
|
||||
|> json_response(200)
|
||||
|> assert_schema("Goal", spec())
|
||||
|> assert_schema("Goal.Revenue", spec())
|
||||
|
||||
assert resp.goal.id == goal.id
|
||||
assert resp.goal_type == "Goal.Revenue"
|
||||
assert resp.goal.display_name == "Purchase (EUR)"
|
||||
end
|
||||
|
||||
test "retrieves pageview goal by ID", %{conn: conn, site: site, token: token} do
|
||||
{:ok, goal} = Plausible.Goals.create(site, %{"page_path" => "/checkout"})
|
||||
|
||||
url = Routes.goals_url(base_uri(), :get, goal.id)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> get(url)
|
||||
|> json_response(200)
|
||||
|> assert_schema("Goal", spec())
|
||||
|> assert_schema("Goal.Pageview", spec())
|
||||
|
||||
assert resp.goal.id == goal.id
|
||||
assert resp.goal_type == "Goal.Pageview"
|
||||
assert resp.goal.display_name == "Visit /checkout"
|
||||
end
|
||||
|
||||
test "retrieves custom event goal by ID", %{conn: conn, site: site, token: token} do
|
||||
{:ok, goal} = Plausible.Goals.create(site, %{"event_name" => "Signup"})
|
||||
|
||||
url = Routes.goals_url(base_uri(), :get, goal.id)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> get(url)
|
||||
|> json_response(200)
|
||||
|> assert_schema("Goal", spec())
|
||||
|> assert_schema("Goal.CustomEvent", spec())
|
||||
|
||||
assert resp.goal.id == goal.id
|
||||
assert resp.goal_type == "Goal.CustomEvent"
|
||||
assert resp.goal.display_name == "Signup"
|
||||
end
|
||||
end
|
||||
|
||||
describe "get /goals" do
|
||||
test "returns an empty goals list if there's none", %{
|
||||
conn: conn,
|
||||
token: token,
|
||||
site: site
|
||||
} do
|
||||
url = Routes.goals_url(base_uri(), :index)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> get(url)
|
||||
|> json_response(200)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
|
||||
assert resp.goals == []
|
||||
assert resp.meta.pagination.has_next_page == false
|
||||
assert resp.meta.pagination.has_prev_page == false
|
||||
assert resp.meta.pagination.links == %{}
|
||||
end
|
||||
|
||||
test "returns a list of goals of each possible goal type", %{
|
||||
conn: conn,
|
||||
site: site,
|
||||
token: token
|
||||
} do
|
||||
{:ok, g1} = Plausible.Goals.create(site, %{"event_name" => "Signup"})
|
||||
{:ok, g2} = Plausible.Goals.create(site, %{"event_name" => "Purchase", "currency" => "EUR"})
|
||||
{:ok, g3} = Plausible.Goals.create(site, %{"page_path" => "/checkout"})
|
||||
|
||||
url = Routes.goals_url(base_uri(), :index)
|
||||
|
||||
resp =
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> get(url)
|
||||
|> json_response(200)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
|
||||
assert [checkout, purchase, signup] = resp.goals
|
||||
assert checkout.goal.display_name == "Visit /checkout"
|
||||
assert checkout.goal.path == "/checkout"
|
||||
assert checkout.goal.id == g3.id
|
||||
assert checkout.goal_type == "Goal.Pageview"
|
||||
|
||||
assert purchase.goal.display_name == "Purchase (EUR)"
|
||||
assert purchase.goal.currency == "EUR"
|
||||
assert purchase.goal.event_name == "Purchase"
|
||||
assert purchase.goal.id == g2.id
|
||||
assert purchase.goal_type == "Goal.Revenue"
|
||||
|
||||
assert signup.goal.display_name == "Signup"
|
||||
assert signup.goal.event_name == "Signup"
|
||||
assert signup.goal.id == g1.id
|
||||
assert signup.goal_type == "Goal.CustomEvent"
|
||||
end
|
||||
|
||||
test "returns a list of goals with pagination", %{conn: conn, site: site, token: token} do
|
||||
for i <- 1..5 do
|
||||
insert(:goal, site: site, event_name: "Goal #{i}")
|
||||
end
|
||||
|
||||
url = Routes.goals_url(base_uri(), :index, limit: 2)
|
||||
|
||||
initial_conn = authenticate(conn, site.domain, token)
|
||||
|
||||
page1 =
|
||||
initial_conn
|
||||
|> get(url)
|
||||
|> json_response(200)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
|
||||
assert [%{goal: %{event_name: "Goal 5"}}, %{goal: %{event_name: "Goal 4"}}] = page1.goals
|
||||
assert page1.meta.pagination.has_next_page == true
|
||||
assert page1.meta.pagination.has_prev_page == false
|
||||
assert page1.meta.pagination.links.next
|
||||
refute page1.meta.pagination.links[:prev]
|
||||
|
||||
page2 =
|
||||
initial_conn
|
||||
|> get(page1.meta.pagination.links.next.url)
|
||||
|> json_response(200)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
|
||||
assert [%{goal: %{event_name: "Goal 3"}}, %{goal: %{event_name: "Goal 2"}}] = page2.goals
|
||||
|
||||
assert page2.meta.pagination.has_next_page == true
|
||||
assert page2.meta.pagination.has_prev_page == true
|
||||
assert page2.meta.pagination.links.next
|
||||
assert page2.meta.pagination.links.prev
|
||||
|
||||
assert ^page1 =
|
||||
initial_conn
|
||||
|> get(page2.meta.pagination.links.prev.url)
|
||||
|> json_response(200)
|
||||
|> assert_schema("Goal.ListResponse", spec())
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete /goals/:id" do
|
||||
test "deletes goal by id", %{conn: conn, site: site, token: token} do
|
||||
{:ok, %{id: goal_id}} =
|
||||
Plausible.Goals.create(site, %{"event_name" => "Purchase", "currency" => "USD"})
|
||||
|
||||
url = Routes.goals_url(base_uri(), :delete, goal_id)
|
||||
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> delete(url)
|
||||
|> response(204)
|
||||
end
|
||||
|
||||
test "is idempotent", %{conn: conn, site: site, token: token} do
|
||||
url = Routes.goals_url(base_uri(), :delete, 666)
|
||||
|
||||
conn
|
||||
|> authenticate(site.domain, token)
|
||||
|> delete(url)
|
||||
|> response(204)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue