Unique steps

pull/2859/head
Adam Rutkowski 4 years ago
parent 0dc30d2680
commit 55e9dc36ae

@ -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

@ -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

@ -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

@ -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",

Loading…
Cancel
Save