Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Rework team creation constraints (#5003)
Adrian Gruntkowski339dd89a988f
diff --git a/lib/plausible/teams.ex b/lib/plausible/teams.exindex 39f21a5376..375464edf5 100644--- a/lib/plausible/teams.ex+++ b/lib/plausible/teams.ex@@ -6,7 +6,7 @@ defmodule Plausible.Teams doimport Ecto.Queryalias __MODULE__- alias Plausible.Auth.GracePeriod+ alias Plausible.Authalias Plausible.Repouse Plausible@@ -26,7 +26,7 @@ defmodule Plausible.Teams doend@spec get_owner(Teams.Team.t()) ::- {:ok, Plausible.Auth.User.t()} | {:error, :no_owner | :multiple_owners}+ {:ok, Auth.User.t()} | {:error, :no_owner | :multiple_owners}def get_owner(team) docase Repo.preload(team, :owner).owner donil -> {:error, :no_owner}@@ -143,6 +143,7 @@ defmodule Plausible.Teams doIf the user has a non-guest membership other than owner, `:no_team` erroris returned."""+ @spec get_or_create(Auth.User.t()) :: {:ok, Teams.Team.t()} | {:error, :multiple_teams}def get_or_create(user) dowith {:error, :no_team} <- get_by_owner(user) docase create_my_team(user) do@@ -155,6 +156,8 @@ defmodule Plausible.Teams doendend+ @spec get_by_owner(Auth.User.t() | pos_integer()) ::+ {:ok, Teams.Team.t()} | {:error, :no_team | :multiple_teams}def get_by_owner(user_id) when is_integer(user_id) doresult =from(tm in Teams.Membership,@@ -163,18 +166,21 @@ defmodule Plausible.Teams doselect: t,order_by: t.id)- |> Repo.one()+ |> Repo.all()case result do- nil ->+ [] ->{:error, :no_team}- team ->+ [team] ->{:ok, team}++ _teams ->+ {:error, :multiple_teams}endend- def get_by_owner(%Plausible.Auth.User{} = user) do+ def get_by_owner(%Auth.User{} = user) doget_by_owner(user.id)end@@ -193,25 +199,25 @@ defmodule Plausible.Teams dodef start_grace_period(team) doteam- |> GracePeriod.start_changeset()+ |> Teams.GracePeriod.start_changeset()|> Repo.update!()enddef start_manual_lock_grace_period(team) doteam- |> GracePeriod.start_manual_lock_changeset()+ |> Teams.GracePeriod.start_manual_lock_changeset()|> Repo.update!()enddef end_grace_period(team) doteam- |> GracePeriod.end_changeset()+ |> Teams.GracePeriod.end_changeset()|> Repo.update!()enddef remove_grace_period(team) doteam- |> GracePeriod.remove_changeset()+ |> Teams.GracePeriod.remove_changeset()|> Repo.update!()end@@ -289,7 +295,7 @@ defmodule Plausible.Teams docase result do{:ok, invitations} ->Enum.each(invitations, fn invitation ->- invitee = Plausible.Auth.find_user_by(email: invitation.email)+ invitee = Auth.find_user_by(email: invitation.email)Teams.Invitations.InviteToTeam.send_invitation_email(invitation, invitee)end)@@ -311,11 +317,13 @@ defmodule Plausible.Teams doteam_membership =team|> Teams.Membership.changeset(user, :owner)+ |> Ecto.Changeset.put_change(:is_autocreated, true)|> Ecto.Changeset.put_change(:inserted_at, user.inserted_at)|> Ecto.Changeset.put_change(:updated_at, user.updated_at)|> Repo.insert!(on_conflict: :nothing,- conflict_target: {:unsafe_fragment, "(user_id) WHERE role != 'guest'"}+ conflict_target:+ {:unsafe_fragment, "(user_id) WHERE role = 'owner' and is_autocreated = true"})if team_membership.id dodiff --git a/lib/plausible/teams/billing.ex b/lib/plausible/teams/billing.exindex dc0e939b8b..44cdb92274 100644--- a/lib/plausible/teams/billing.ex+++ b/lib/plausible/teams/billing.ex@@ -149,7 +149,7 @@ defmodule Plausible.Teams.Billing dotrial_over? and not subscription_active? ->{:needs_to_upgrade, :no_active_subscription}- Plausible.Auth.GracePeriod.expired?(team) ->+ Teams.GracePeriod.expired?(team) ->{:needs_to_upgrade, :grace_period_ended}true ->diff --git a/lib/plausible/auth/grace_period.ex b/lib/plausible/teams/grace_period.exsimilarity index 81%rename from lib/plausible/auth/grace_period.exrename to lib/plausible/teams/grace_period.exindex d709039cc0..c75d24df1d 100644--- a/lib/plausible/auth/grace_period.ex+++ b/lib/plausible/teams/grace_period.ex@@ -1,9 +1,9 @@-defmodule Plausible.Auth.GracePeriod do+defmodule Plausible.Teams.GracePeriod do@moduledoc """This embedded schema stores information about the account locking graceperiod.- Users are given this 7-day grace period to upgrade their account after+ Teams are given this 7-day grace period to upgrade their account afteroutgrowing their subscriptions. The actual account locking happens inbackground with `Plausible.Workers.LockSites`.@@ -29,7 +29,7 @@ defmodule Plausible.Auth.GracePeriod do@spec start_changeset(Teams.Team.t()) :: Ecto.Changeset.t()@doc """- Starts a account locking grace period of 7 days by changing the User struct.+ Starts a account locking grace period of 7 days by changing the Team struct."""def start_changeset(%Teams.Team{} = team) dograce_period = %__MODULE__{@@ -43,7 +43,7 @@ defmodule Plausible.Auth.GracePeriod do@spec start_manual_lock_changeset(Teams.Team.t()) :: Ecto.Changeset.t()@doc """- Starts a manual account locking grace period by changing the User struct.+ Starts a manual account locking grace period by changing the Team struct.Manual locking means the grace period can only be removed manually from theCRM."""@@ -59,7 +59,7 @@ defmodule Plausible.Auth.GracePeriod do@spec end_changeset(Teams.Team.t()) :: Ecto.Changeset.t()@doc """- Ends an existing grace period by `setting users.grace_period.is_over` to true.+ Ends an existing grace period by setting `teams.grace_period.is_over` to true.This means the grace period has expired."""def end_changeset(%Teams.Team{} = team) do@@ -68,7 +68,7 @@ defmodule Plausible.Auth.GracePeriod do@spec remove_changeset(Teams.Team.t()) :: Ecto.Changeset.t()@doc """- Removes the grace period from the User completely.+ Removes the grace period from the Team completely."""def remove_changeset(%Teams.Team{} = team) doEcto.Changeset.change(team, grace_period: nil)@@ -76,8 +76,8 @@ defmodule Plausible.Auth.GracePeriod do@spec active?(Teams.Team.t() | nil) :: boolean()@doc """- Returns whether the grace period is still active for a User. Defaults to- false if the user is nil or there is no grace period.+ Returns whether the grace period is still active for a Team. Defaults to+ false if the team is nil or there is no grace period."""def active?(team)@@ -93,8 +93,8 @@ defmodule Plausible.Auth.GracePeriod do@spec expired?(Teams.Team.t() | nil) :: boolean()@doc """- Returns whether the grace period has already expired for a User. Defaults to- false if the user is nil or there is no grace period.+ Returns whether the grace period has already expired for a Team. Defaults to+ false if the team is nil or there is no grace period."""def expired?(team) doif team && team.grace_period, do: !active?(team), else: falsediff --git a/lib/plausible/teams/membership.ex b/lib/plausible/teams/membership.exindex efb4f7def3..bf610f0d9a 100644--- a/lib/plausible/teams/membership.ex+++ b/lib/plausible/teams/membership.ex@@ -13,6 +13,7 @@ defmodule Plausible.Teams.Membership doschema "team_memberships" dofield :role, Ecto.Enum, values: @roles+ field :is_autocreated, :boolean, default: falsebelongs_to :user, Plausible.Auth.Userbelongs_to :team, Plausible.Teams.Team@@ -30,6 +31,5 @@ defmodule Plausible.Teams.Membership do|> put_change(:role, role)|> put_assoc(:team, team)|> put_assoc(:user, user)- |> unique_constraint(:user_id, name: :one_team_per_user)endenddiff --git a/lib/plausible/teams/team.ex b/lib/plausible/teams/team.exindex d17e337f08..ef4fe82b16 100644--- a/lib/plausible/teams/team.ex+++ b/lib/plausible/teams/team.ex@@ -29,7 +29,7 @@ defmodule Plausible.Teams.Team dofield :setup_complete, :boolean, default: falsefield :setup_at, :naive_datetime- embeds_one :grace_period, Plausible.Auth.GracePeriod, on_replace: :update+ embeds_one :grace_period, Plausible.Teams.GracePeriod, on_replace: :updatehas_many :sites, Plausible.Sitehas_many :team_memberships, Plausible.Teams.Membershipdiff --git a/lib/plausible_web/templates/layout/_notice.html.heex b/lib/plausible_web/templates/layout/_notice.html.heexindex aff8e486a2..faf826bf89 100644--- a/lib/plausible_web/templates/layout/_notice.html.heex+++ b/lib/plausible_web/templates/layout/_notice.html.heex@@ -4,12 +4,12 @@<div :if={assigns[:my_team]} class="flex flex-col gap-y-2"><Notice.active_grace_period- :if={Plausible.Auth.GracePeriod.active?(@my_team)}+ :if={Plausible.Teams.GracePeriod.active?(@my_team)}enterprise?={Plausible.Teams.Billing.enterprise_configured?(@my_team)}grace_period_end={grace_period_end(@my_team)}/>- <Notice.dashboard_locked :if={Plausible.Auth.GracePeriod.expired?(@my_team)} />+ <Notice.dashboard_locked :if={Plausible.Teams.GracePeriod.expired?(@my_team)} /><Notice.subscription_cancelled subscription={@my_team.subscription} />diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exsindex 5cc1da0162..2a8b135271 100644--- a/test/plausible/billing/billing_test.exs+++ b/test/plausible/billing/billing_test.exs@@ -412,8 +412,8 @@ defmodule Plausible.BillingTest doassert Repo.reload!(api_key).hourly_request_limit == 10_000end- test "if user's grace period has ended, upgrading will unlock sites and remove grace period" do- grace_period = %Plausible.Auth.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}+ test "if teams's grace period has ended, upgrading will unlock sites and remove grace period" do+ grace_period = %Plausible.Teams.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}user = new_user(team: [grace_period: grace_period])subscribe_to_growth_plan(user)diff --git a/test/plausible/billing/site_locker_test.exs b/test/plausible/billing/site_locker_test.exsindex a0f97f0adf..70977d5862 100644--- a/test/plausible/billing/site_locker_test.exs+++ b/test/plausible/billing/site_locker_test.exs@@ -46,8 +46,8 @@ defmodule Plausible.Billing.SiteLockerTest dorefute Repo.reload!(site).lockedend- test "does not lock user who has an active subscription and is on grace period" do- grace_period = %Plausible.Auth.GracePeriod{end_date: Timex.shift(Timex.today(), days: 1)}+ test "does not lock team which has an active subscription and is on grace period" do+ grace_period = %Plausible.Teams.GracePeriod{end_date: Timex.shift(Timex.today(), days: 1)}user =new_user(team: [grace_period: grace_period])@@ -77,8 +77,8 @@ defmodule Plausible.Billing.SiteLockerTest doassert Repo.reload!(site).lockedend- test "locks all sites if user has active subscription but grace period has ended" do- grace_period = %Plausible.Auth.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}+ test "locks all sites if team has active subscription but grace period has ended" do+ grace_period = %Plausible.Teams.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}user = new_user(team: [grace_period: grace_period])subscribe_to_plan(user, "123")site = new_site(owner: user)@@ -90,7 +90,7 @@ defmodule Plausible.Billing.SiteLockerTest doendtest "sends email if grace period has ended" do- grace_period = %Plausible.Auth.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}+ grace_period = %Plausible.Teams.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}user = new_user(team: [grace_period: grace_period])subscribe_to_plan(user, "123")new_site(owner: user)@@ -105,7 +105,7 @@ defmodule Plausible.Billing.SiteLockerTest doendtest "does not send grace period email if site is already locked" do- grace_period = %Plausible.Auth.GracePeriod{+ grace_period = %Plausible.Teams.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1),is_over: false}diff --git a/test/plausible/auth/grace_period_test.exs b/test/plausible/teams/grace_period_test.exssimilarity index 64%rename from test/plausible/auth/grace_period_test.exsrename to test/plausible/teams/grace_period_test.exsindex b3e32f6055..2f7116e510 100644--- a/test/plausible/auth/grace_period_test.exs+++ b/test/plausible/teams/grace_period_test.exs@@ -1,4 +1,4 @@-defmodule Plausible.Auth.GracePeriodTest do+defmodule Plausible.Teams.GracePeriodTest douse Plausible.DataCase, async: trueuse Plausible.Teams.Test@@ -7,16 +7,16 @@ defmodule Plausible.Auth.GracePeriodTest donew_user(trial_expiry_date: Date.utc_today(), team: [grace_period: nil])|> team_of()- refute Plausible.Auth.GracePeriod.active?(without_grace_period)+ refute Plausible.Teams.GracePeriod.active?(without_grace_period)without_team = nil- refute Plausible.Auth.GracePeriod.active?(without_team)+ refute Plausible.Teams.GracePeriod.active?(without_team)endtest "active?/1 returns false when grace period is expired" doyesterday = Date.add(Date.utc_today(), -1)- grace_period = %Plausible.Auth.GracePeriod{+ grace_period = %Plausible.Teams.GracePeriod{end_date: yesterday,is_over: false}@@ -25,13 +25,13 @@ defmodule Plausible.Auth.GracePeriodTest donew_user(trial_expiry_date: Date.utc_today(), team: [grace_period: grace_period])|> team_of()- refute Plausible.Auth.GracePeriod.active?(team)+ refute Plausible.Teams.GracePeriod.active?(team)endtest "active?/1 returns true when grace period is still active" dotomorrow = Date.add(Date.utc_today(), 1)- grace_period = %Plausible.Auth.GracePeriod{+ grace_period = %Plausible.Teams.GracePeriod{end_date: tomorrow,is_over: false}@@ -40,7 +40,7 @@ defmodule Plausible.Auth.GracePeriodTest donew_user(trial_expiry_date: Date.utc_today(), team: [grace_period: grace_period])|> team_of()- assert Plausible.Auth.GracePeriod.active?(team)+ assert Plausible.Teams.GracePeriod.active?(team)endtest "expired?/1 returns false when grace period cannot be telled" do@@ -48,16 +48,16 @@ defmodule Plausible.Auth.GracePeriodTest donew_user(trial_expiry_date: Date.utc_today(), team: [grace_period: nil])|> team_of()- refute Plausible.Auth.GracePeriod.expired?(without_grace_period)+ refute Plausible.Teams.GracePeriod.expired?(without_grace_period)without_team = nil- refute Plausible.Auth.GracePeriod.expired?(without_team)+ refute Plausible.Teams.GracePeriod.expired?(without_team)endtest "expired?/1 returns true when grace period is expired" doyesterday = Date.add(Date.utc_today(), -1)- grace_period = %Plausible.Auth.GracePeriod{+ grace_period = %Plausible.Teams.GracePeriod{end_date: yesterday,is_over: true}@@ -66,13 +66,13 @@ defmodule Plausible.Auth.GracePeriodTest donew_user(trial_expiry_date: Date.utc_today(), team: [grace_period: grace_period])|> team_of()- assert Plausible.Auth.GracePeriod.expired?(team)+ assert Plausible.Teams.GracePeriod.expired?(team)endtest "expired?/1 returns false when grace period is still active" dotomorrow = Date.add(Date.utc_today(), 1)- grace_period = %Plausible.Auth.GracePeriod{+ grace_period = %Plausible.Teams.GracePeriod{end_date: tomorrow,is_over: false}@@ -81,36 +81,36 @@ defmodule Plausible.Auth.GracePeriodTest donew_user(trial_expiry_date: Date.utc_today(), team: [grace_period: grace_period])|> team_of()- refute Plausible.Auth.GracePeriod.expired?(team)+ refute Plausible.Teams.GracePeriod.expired?(team)endtest "start_manual_lock_changeset/1 creates an active grace period" doteam = new_user(trial_expiry_date: Date.utc_today()) |> team_of()- changeset = Plausible.Auth.GracePeriod.start_manual_lock_changeset(team)+ changeset = Plausible.Teams.GracePeriod.start_manual_lock_changeset(team)team = Ecto.Changeset.apply_changes(changeset)- assert Plausible.Auth.GracePeriod.active?(team)- refute Plausible.Auth.GracePeriod.expired?(team)+ assert Plausible.Teams.GracePeriod.active?(team)+ refute Plausible.Teams.GracePeriod.expired?(team)endtest "start_changeset/1 creates an active grace period" doteam = new_user(trial_expiry_date: Date.utc_today()) |> team_of()- changeset = Plausible.Auth.GracePeriod.start_changeset(team)+ changeset = Plausible.Teams.GracePeriod.start_changeset(team)team = Ecto.Changeset.apply_changes(changeset)- assert Plausible.Auth.GracePeriod.active?(team)- refute Plausible.Auth.GracePeriod.expired?(team)+ assert Plausible.Teams.GracePeriod.active?(team)+ refute Plausible.Teams.GracePeriod.expired?(team)endtest "remove_changeset/1 removes the active grace period" doteam = new_user(trial_expiry_date: Date.utc_today()) |> team_of()- start_changeset = Plausible.Auth.GracePeriod.start_changeset(team)+ start_changeset = Plausible.Teams.GracePeriod.start_changeset(team)team = Ecto.Changeset.apply_changes(start_changeset)- remove_changeset = Plausible.Auth.GracePeriod.remove_changeset(team)+ remove_changeset = Plausible.Teams.GracePeriod.remove_changeset(team)team = Ecto.Changeset.apply_changes(remove_changeset)- refute Plausible.Auth.GracePeriod.active?(team)- refute Plausible.Auth.GracePeriod.expired?(team)+ refute Plausible.Teams.GracePeriod.active?(team)+ refute Plausible.Teams.GracePeriod.expired?(team)endenddiff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exsindex 744213639f..fc3e53cc53 100644--- a/test/workers/check_usage_test.exs+++ b/test/workers/check_usage_test.exs@@ -296,7 +296,7 @@ defmodule Plausible.Workers.CheckUsageTest do)CheckUsage.perform(nil, usage_stub)- assert user |> team_of() |> Repo.reload() |> Plausible.Auth.GracePeriod.active?()+ assert user |> team_of() |> Repo.reload() |> Plausible.Teams.GracePeriod.active?()usage_stub =Plausible.Teams.Billing@@ -308,7 +308,7 @@ defmodule Plausible.Workers.CheckUsageTest doend)CheckUsage.perform(nil, usage_stub)- refute user |> team_of() |> Repo.reload() |> Plausible.Auth.GracePeriod.active?()+ refute user |> team_of() |> Repo.reload() |> Plausible.Teams.GracePeriod.active?()endendend@@ -457,7 +457,7 @@ defmodule Plausible.Workers.CheckUsageTest do)CheckUsage.perform(nil, usage_stub)- assert user |> team_of() |> Repo.reload() |> Plausible.Auth.GracePeriod.active?()+ assert user |> team_of() |> Repo.reload() |> Plausible.Teams.GracePeriod.active?()endendend