Add goal type to JSON response

This commit is contained in:
Uku Taht 2021-12-14 11:39:26 +02:00
parent 6365c6956b
commit 66575f497b
2 changed files with 17 additions and 1 deletions

View File

@ -1,8 +1,22 @@
defimpl Jason.Encoder, for: Plausible.Goal do
def encode(value, opts) do
goal_type =
cond do
value.event_name -> :event
value.page_path -> :page
end
value
|> Map.put(:goal_type, goal_type)
|> Map.take([:id, :domain, :goal_type, :event_name, :page_path])
|> Jason.Encode.map(opts)
end
end
defmodule Plausible.Goal do defmodule Plausible.Goal do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@derive {Jason.Encoder, only: [:id, :domain, :event_name, :page_path]}
schema "goals" do schema "goals" do
field :domain, :string field :domain, :string
field :event_name, :string field :event_name, :string

View File

@ -204,6 +204,7 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
}) })
res = json_response(conn, 200) res = json_response(conn, 200)
assert res["goal_type"] == "event"
assert res["event_name"] == "Signup" assert res["event_name"] == "Signup"
end end
@ -216,6 +217,7 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
}) })
res = json_response(conn, 200) res = json_response(conn, 200)
assert res["goal_type"] == "page"
assert res["page_path"] == "/signup" assert res["page_path"] == "/signup"
end end