From 55e9dc36ae8afa580de6b8280d5c687fb10df998 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Tue, 18 Apr 2023 13:05:03 +0200 Subject: [PATCH] Unique steps --- lib/plausible/funnel.ex | 16 ++++++++++++++++ lib/plausible/funnels.ex | 10 ++++------ .../migrations/20230417095029_init_funnels.exs | 2 ++ test/plausible/funnels_test.exs | 16 ++++++++++++---- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/plausible/funnel.ex b/lib/plausible/funnel.ex index 90ed23f1e..7a42024d3 100644 --- a/lib/plausible/funnel.ex +++ b/lib/plausible/funnel.ex @@ -1,8 +1,10 @@ defmodule Plausible.Funnel do use Ecto.Schema + import Ecto.Changeset defmodule Step do use Ecto.Schema + import Ecto.Changeset @type t() :: %__MODULE__{} schema "funnel_steps" do @@ -11,6 +13,14 @@ defmodule Plausible.Funnel do belongs_to :goal, Plausible.Goal timestamps() end + + def changeset(step, attrs \\ %{}) do + step + |> cast(attrs, [:step_order, :goal_id]) + |> unique_constraint(:goal, + name: :funnel_steps_goal_id_funnel_id_index + ) + end end @type t() :: %__MODULE__{} @@ -26,4 +36,10 @@ defmodule Plausible.Funnel do has_many :goals, through: [:steps, :goal] timestamps() end + + def changeset(funnel, attrs \\ %{}) do + funnel + |> cast(attrs, [:name]) + |> cast_assoc(:steps, with: &Step.changeset/2) + end end diff --git a/lib/plausible/funnels.ex b/lib/plausible/funnels.ex index a52938865..9ac14fb1e 100644 --- a/lib/plausible/funnels.ex +++ b/lib/plausible/funnels.ex @@ -5,7 +5,6 @@ defmodule Plausible.Funnels do alias Plausible.Repo alias Plausible.ClickhouseRepo - import Ecto.Changeset import Ecto.Query def create(site, name, goals, id \\ 5) when is_list(goals) do @@ -19,13 +18,12 @@ defmodule Plausible.Funnels do } end) - change(%Funnel{ + %Funnel{ site_id: site.id, - name: name, id: id - }) - |> put_assoc(:steps, steps) - |> Repo.insert!() + } + |> Funnel.changeset(%{name: name, steps: steps}) + |> Repo.insert() end def get(%Plausible.Site{id: site_id}, by) do diff --git a/priv/repo/migrations/20230417095029_init_funnels.exs b/priv/repo/migrations/20230417095029_init_funnels.exs index b6eb14e85..dda7e7fb5 100644 --- a/priv/repo/migrations/20230417095029_init_funnels.exs +++ b/priv/repo/migrations/20230417095029_init_funnels.exs @@ -14,5 +14,7 @@ defmodule Plausible.Repo.Migrations.InitFunnels do add :step_order, :integer, null: false timestamps() end + + create unique_index(:funnel_steps, [:goal_id, :funnel_id]) end end diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs index a6b635b64..b4dca59b8 100644 --- a/test/plausible/funnels_test.exs +++ b/test/plausible/funnels_test.exs @@ -15,7 +15,7 @@ defmodule Plausible.FunnelsTest do end test "create and store a funnel given a set of goals", %{site: site, goals: [g1, g2, g3]} do - funnel = + {:ok, funnel} = Funnels.create( site, "From blog to signup and purchase", @@ -36,7 +36,7 @@ defmodule Plausible.FunnelsTest do end test "retrieve a funnel by id and site, get steps in order", %{site: site, goals: [g1, g2, g3]} do - funnel = + {:ok, funnel} = Funnels.create( site, "Lorem ipsum", @@ -55,14 +55,22 @@ defmodule Plausible.FunnelsTest do test "a funnel can be deleted" do end - test "a goal can only appear once in a funnel" do + test "a goal can only appear once in a funnel", %{goals: [g1 | _], site: site} do + {:error, changeset} = + Funnels.create( + site, + "Lorem ipsum", + [g1, g1], + nil + ) + |> IO.inspect(label: :err) end test "funnels can be listed per site" do end test "funnels can be evaluated per site within a time range", %{site: site, goals: goals} do - funnel = + {:ok, funnel} = Funnels.create( site, "From blog to signup and purchase",