parent
d66322e12d
commit
cfaa5be8f4
@ -0,0 +1,78 @@
|
||||
defmodule Plausible.Billing.Ecto.Feature do
|
||||
@moduledoc """
|
||||
Ecto type representing a feature. Features are cast and stored in the
|
||||
database as strings and loaded as modules, for example: `"props"` is loaded
|
||||
as `Plausible.Billing.Feature.Props`.
|
||||
"""
|
||||
|
||||
use Ecto.Type
|
||||
|
||||
def type, do: :string
|
||||
|
||||
def cast(feature) when is_binary(feature) do
|
||||
found =
|
||||
Enum.find(Plausible.Billing.Feature.list(), fn mod ->
|
||||
Atom.to_string(mod.name()) == feature
|
||||
end)
|
||||
|
||||
if found, do: {:ok, found}, else: :error
|
||||
end
|
||||
|
||||
def cast(mod) when is_atom(mod) do
|
||||
{:ok, mod}
|
||||
end
|
||||
|
||||
def load(feature) when is_binary(feature) do
|
||||
cast(feature)
|
||||
end
|
||||
|
||||
def dump(mod) when is_atom(mod) do
|
||||
{:ok, Atom.to_string(mod.name())}
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Plausible.Billing.Ecto.FeatureList do
|
||||
@moduledoc """
|
||||
Ecto type representing a list of features. This is a proxy for
|
||||
`{:array, Plausible.Billing.Ecto.Feature}` and is required for Kaffy to
|
||||
render the HTML input correctly.
|
||||
"""
|
||||
|
||||
use Ecto.Type
|
||||
|
||||
def type, do: {:array, Plausible.Billing.Ecto.Feature}
|
||||
def cast(list), do: Ecto.Type.cast(type(), list)
|
||||
def load(list), do: Ecto.Type.load(type(), list)
|
||||
def dump(list), do: Ecto.Type.dump(type(), list)
|
||||
|
||||
def render_form(_conn, changeset, form, field, _options) do
|
||||
features = Ecto.Changeset.get_field(changeset, field)
|
||||
|
||||
checkboxes =
|
||||
for mod <- Plausible.Billing.Feature.list() do
|
||||
[
|
||||
{:safe, ~s(<label style="padding-right: 15px;">)},
|
||||
Phoenix.HTML.Tag.tag(
|
||||
:input,
|
||||
name: Phoenix.HTML.Form.input_name(form, field) <> "[]",
|
||||
id: Phoenix.HTML.Form.input_id(form, field, mod.name()),
|
||||
type: "checkbox",
|
||||
value: mod.name(),
|
||||
style: "margin-right: 3px;",
|
||||
checked: mod in features
|
||||
),
|
||||
mod.display_name(),
|
||||
{:safe, ~s(</label>)}
|
||||
]
|
||||
end
|
||||
|
||||
[
|
||||
{:safe, ~s(<div class="form-group">)},
|
||||
Phoenix.HTML.Form.label(form, field),
|
||||
{:safe, ~s(<div class="form-control">)},
|
||||
checkboxes,
|
||||
{:safe, ~s(</div>)},
|
||||
{:safe, ~s(</div>)}
|
||||
]
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,39 @@
|
||||
defmodule Plausible.Billing.Ecto.Limit do
|
||||
@moduledoc """
|
||||
Ecto type representing a limit, that can be either a number or unlimited.
|
||||
Unlimited is dumped to the database as `-1` and loaded as `:unlimited` to
|
||||
keep compatibility with the rest of the codebase.
|
||||
"""
|
||||
|
||||
use Ecto.Type
|
||||
|
||||
def type, do: :integer
|
||||
|
||||
def cast(-1), do: {:ok, :unlimited}
|
||||
def cast(:unlimited), do: {:ok, :unlimited}
|
||||
def cast(other), do: Ecto.Type.cast(:integer, other)
|
||||
|
||||
def load(-1), do: {:ok, :unlimited}
|
||||
def load(other), do: Ecto.Type.load(:integer, other)
|
||||
|
||||
def dump(:unlimited), do: {:ok, -1}
|
||||
def dump(other), do: Ecto.Type.dump(:integer, other)
|
||||
|
||||
def render_form(_conn, changeset, form, field, _options) do
|
||||
{:ok, value} = changeset |> Ecto.Changeset.get_field(field) |> dump()
|
||||
|
||||
[
|
||||
{:safe, ~s(<div class="form-group">)},
|
||||
Phoenix.HTML.Form.label(form, field),
|
||||
Phoenix.HTML.Form.number_input(form, field,
|
||||
class: "form-control",
|
||||
name: "#{form.name}[#{field}]",
|
||||
id: "#{form.name}_#{field}",
|
||||
value: value,
|
||||
min: -1
|
||||
),
|
||||
{:safe, ~s(<p class="help_text">Use -1 for unlimited.</p>)},
|
||||
{:safe, ~s(</div>)}
|
||||
]
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,13 @@
|
||||
defmodule Plausible.Repo.Migrations.AddLimitsToEnterprisePlans do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
alter table(:enterprise_plans) do
|
||||
modify :hourly_api_request_limit, :integer, null: false
|
||||
modify :monthly_pageview_limit, :integer, null: false
|
||||
modify :site_limit, :integer, null: false
|
||||
add :team_member_limit, :integer, null: false, default: -1
|
||||
add :features, {:array, :string}, null: false, default: ["props", "stats_api"]
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,51 @@
|
||||
defmodule Plausible.Billing.EnterprisePlanTest do
|
||||
use Plausible.DataCase
|
||||
alias Plausible.Billing.EnterprisePlan
|
||||
|
||||
test "changeset/2 loads and dumps the list of features" do
|
||||
plan = build(:enterprise_plan, user_id: insert(:user).id)
|
||||
attrs = %{features: ["props", "stats_api"]}
|
||||
|
||||
assert {:ok, enterprise_plan} =
|
||||
plan
|
||||
|> EnterprisePlan.changeset(attrs)
|
||||
|> Plausible.Repo.insert()
|
||||
|
||||
assert %EnterprisePlan{
|
||||
features: [Plausible.Billing.Feature.Props, Plausible.Billing.Feature.StatsAPI]
|
||||
} = enterprise_plan
|
||||
|
||||
assert %EnterprisePlan{
|
||||
features: [Plausible.Billing.Feature.Props, Plausible.Billing.Feature.StatsAPI]
|
||||
} = Plausible.Repo.get(EnterprisePlan, enterprise_plan.id)
|
||||
end
|
||||
|
||||
test "changeset/2 fails when feature does not exist" do
|
||||
plan = build(:enterprise_plan, user_id: insert(:user).id)
|
||||
attrs = %{features: ["ga4_import"]}
|
||||
|
||||
assert {:error, changeset} =
|
||||
plan
|
||||
|> EnterprisePlan.changeset(attrs)
|
||||
|> Plausible.Repo.insert()
|
||||
|
||||
assert {"is invalid", [type: Plausible.Billing.Ecto.FeatureList, validation: :cast]} ==
|
||||
changeset.errors[:features]
|
||||
end
|
||||
|
||||
test "changeset/2 loads and dumps limits" do
|
||||
plan = build(:enterprise_plan, user_id: insert(:user).id)
|
||||
attrs = %{team_member_limit: :unlimited, monthly_pageview_limit: 10_000}
|
||||
|
||||
assert {:ok, enterprise_plan} =
|
||||
plan
|
||||
|> EnterprisePlan.changeset(attrs)
|
||||
|> Plausible.Repo.insert()
|
||||
|
||||
assert %EnterprisePlan{team_member_limit: :unlimited, monthly_pageview_limit: 10_000} =
|
||||
enterprise_plan
|
||||
|
||||
assert %EnterprisePlan{team_member_limit: :unlimited, monthly_pageview_limit: 10_000} =
|
||||
Plausible.Repo.get(EnterprisePlan, enterprise_plan.id)
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue