From f5ac923f8629e51cb51e20237856463cb0b38f38 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Thu, 14 Nov 2024 11:18:59 +0100 Subject: [PATCH] Dedup code based on read team schemas switching --- lib/plausible/sites.ex | 1 - lib/plausible/teams/adapter.ex | 38 +++++++++++ lib/plausible/teams/adapter/read/billing.ex | 65 ++++++------------- lib/plausible/teams/adapter/read/ownership.ex | 58 +++++++---------- lib/plausible/teams/adapter/read/sites.ex | 32 +++++---- 5 files changed, 96 insertions(+), 98 deletions(-) create mode 100644 lib/plausible/teams/adapter.ex diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.ex index bfe750833438..d6be09a274f5 100644 --- a/lib/plausible/sites.ex +++ b/lib/plausible/sites.ex @@ -6,7 +6,6 @@ defmodule Plausible.Sites do import Ecto.Query alias Plausible.Auth - alias Plausible.Billing.Quota alias Plausible.Repo alias Plausible.Site alias Plausible.Site.SharedLink diff --git a/lib/plausible/teams/adapter.ex b/lib/plausible/teams/adapter.ex new file mode 100644 index 000000000000..63333f5e71b7 --- /dev/null +++ b/lib/plausible/teams/adapter.ex @@ -0,0 +1,38 @@ +defmodule Plausible.Teams.Adapter do + @moduledoc """ + Commonly used teams-transition functions + """ + alias Plausible.Teams + + defmacro __using__(_) do + quote do + alias Plausible.Teams + import Teams.Adapter + end + end + + def team_or_user(user) do + switch( + user, + team_fn: &Function.identity/1, + user_fn: &Function.identity/1 + ) + end + + def switch(user, opts \\ []) do + team_fn = Keyword.fetch!(opts, :team_fn) + user_fn = Keyword.fetch!(opts, :user_fn) + + if Teams.read_team_schemas?(user) do + team = + case Teams.get_by_owner(user) do + {:ok, team} -> team + {:error, _} -> nil + end + + team_fn.(team) + else + user_fn.(user) + end + end +end diff --git a/lib/plausible/teams/adapter/read/billing.ex b/lib/plausible/teams/adapter/read/billing.ex index 2e9284574441..6a58bd842b53 100644 --- a/lib/plausible/teams/adapter/read/billing.ex +++ b/lib/plausible/teams/adapter/read/billing.ex @@ -2,61 +2,36 @@ defmodule Plausible.Teams.Adapter.Read.Billing do @moduledoc """ Transition adapter for new schema reads """ - alias Plausible.Teams + use Plausible.Teams.Adapter def check_needs_to_upgrade(user) do - if Teams.read_team_schemas?(user) do - team = - case Teams.get_by_owner(user) do - {:ok, team} -> team - {:error, _} -> nil - end - - Teams.Billing.check_needs_to_upgrade(team) - else - Plausible.Billing.check_needs_to_upgrade(user) - end + switch( + user, + team_fn: &Teams.Billing.check_needs_to_upgrade/1, + user_fn: &Plausible.Billing.check_needs_to_upgrade/1 + ) end def site_limit(user) do - if Teams.read_team_schemas?(user) do - team = - case Teams.get_by_owner(user) do - {:ok, team} -> team - {:error, _} -> nil - end - - Teams.Billing.site_limit(team) - else - Plausible.Billing.Quota.Limits.site_limit(user) - end + switch( + user, + team_fn: &Teams.Billing.site_limit/1, + user_fn: &Plausible.Billing.Quota.Limits.site_limit/1 + ) end def ensure_can_add_new_site(user) do - if Teams.read_team_schemas?(user) do - team = - case Teams.get_by_owner(user) do - {:ok, team} -> team - {:error, _} -> nil - end - - Teams.Billing.ensure_can_add_new_site(team) - else - Plausible.Billing.Quota.ensure_can_add_new_site(user) - end + switch( + user, + team_fn: &Teams.Billing.ensure_can_add_new_site/1, + user_fn: &Plausible.Billing.Quota.ensure_can_add_new_site/1 + ) end def site_usage(user) do - if Teams.read_team_schemas?(user) do - team = - case Teams.get_by_owner(user) do - {:ok, team} -> team - {:error, _} -> nil - end - - Teams.Billing.site_usage(team) - else - Plausible.Billing.Quota.Usage.site_usage(user) - end + switch(user, + team_fn: &Teams.Billing.site_usage/1, + user_fn: &Plausible.Billing.Quota.Usage.site_usage/1 + ) end end diff --git a/lib/plausible/teams/adapter/read/ownership.ex b/lib/plausible/teams/adapter/read/ownership.ex index 4a04ace2fc1c..35f691e31d53 100644 --- a/lib/plausible/teams/adapter/read/ownership.ex +++ b/lib/plausible/teams/adapter/read/ownership.ex @@ -3,59 +3,47 @@ defmodule Plausible.Teams.Adapter.Read.Ownership do Transition adapter for new schema reads """ use Plausible + use Plausible.Teams.Adapter alias Plausible.Site alias Plausible.Auth - alias Plausible.Teams alias Plausible.Site.Memberships.Invitations def ensure_can_take_ownership(site, user) do - if Teams.read_team_schemas?(user) do - team = - case Teams.get_by_owner(user) do - {:ok, team} -> team - {:error, _} -> nil - end - - Teams.Invitations.ensure_can_take_ownership(site, team) - else - Invitations.ensure_can_take_ownership(site, user) - end + switch( + user, + team_fn: &Teams.Invitations.ensure_can_take_ownership(site, &1), + user_fn: &Invitations.ensure_can_take_ownership(site, &1) + ) end def has_sites?(user) do - if Teams.read_team_schemas?(user) do - Teams.Users.has_sites?(user, include_pending?: true) - else - Site.Memberships.any_or_pending?(user) - end + switch( + user, + team_fn: fn _ -> Teams.Users.has_sites?(user, include_pending?: true) end, + user_fn: &Site.Memberships.any_or_pending?/1 + ) end def owns_sites?(user, sites) do - if Teams.read_team_schemas?(user) do - Teams.Users.owns_sites?(user, include_pending?: true) - else - Enum.any?(sites.entries, fn site -> - length(site.invitations) > 0 && List.first(site.invitations).role == :owner - end) || - Auth.user_owns_sites?(user) - end + switch( + user, + team_fn: fn _ -> Teams.Users.owns_sites?(user, include_pending?: true) end, + user_fn: fn user -> + Enum.any?(sites.entries, fn site -> + length(site.invitations) > 0 && List.first(site.invitations).role == :owner + end) || + Auth.user_owns_sites?(user) + end + ) end on_ee do def check_feature_access(site, new_owner) do - user_or_team = - if Teams.read_team_schemas?(new_owner) do - case Teams.get_by_owner(new_owner) do - {:ok, team} -> team - {:error, _} -> nil - end - else - new_owner - end + team_or_user = team_or_user(new_owner) missing_features = Plausible.Billing.Quota.Usage.features_usage(nil, [site.id]) - |> Enum.filter(&(&1.check_availability(user_or_team) != :ok)) + |> Enum.filter(&(&1.check_availability(team_or_user) != :ok)) if missing_features == [] do :ok diff --git a/lib/plausible/teams/adapter/read/sites.ex b/lib/plausible/teams/adapter/read/sites.ex index 76ec43022a30..b38e9eeb4208 100644 --- a/lib/plausible/teams/adapter/read/sites.ex +++ b/lib/plausible/teams/adapter/read/sites.ex @@ -5,30 +5,29 @@ defmodule Plausible.Teams.Adapter.Read.Sites do import Ecto.Query - alias Plausible.Auth alias Plausible.Repo alias Plausible.Site - alias Plausible.Teams + use Plausible.Teams.Adapter def list(user, pagination_params, opts \\ []) do - if Plausible.Teams.read_team_schemas?(user) do - Plausible.Teams.Sites.list(user, pagination_params, opts) - else - old_list(user, pagination_params, opts) - end + switch( + user, + team_fn: fn _ -> Plausible.Teams.Sites.list(user, pagination_params, opts) end, + user_fn: fn _ -> old_list(user, pagination_params, opts) end + ) end def list_with_invitations(user, pagination_params, opts \\ []) do - if Plausible.Teams.read_team_schemas?(user) do - Plausible.Teams.Sites.list_with_invitations(user, pagination_params, opts) - else - old_list_with_invitations(user, pagination_params, opts) - end + switch( + user, + team_fn: fn _ -> + Plausible.Teams.Sites.list_with_invitations(user, pagination_params, opts) + end, + user_fn: fn _ -> old_list_with_invitations(user, pagination_params, opts) end + ) end - @type list_opt() :: {:filter_by_domain, String.t()} - @spec old_list(Auth.User.t(), map(), [list_opt()]) :: Scrivener.Page.t() - def old_list(user, pagination_params, opts \\ []) do + defp old_list(user, pagination_params, opts) do domain_filter = Keyword.get(opts, :filter_by_domain) from(s in Site, @@ -60,8 +59,7 @@ defmodule Plausible.Teams.Adapter.Read.Sites do |> Repo.paginate(pagination_params) end - @spec old_list_with_invitations(Auth.User.t(), map(), [list_opt()]) :: Scrivener.Page.t() - def old_list_with_invitations(user, pagination_params, opts \\ []) do + defp old_list_with_invitations(user, pagination_params, opts) do domain_filter = Keyword.get(opts, :filter_by_domain) result =