Fix team struct sync and pruning team memberships/invitations on site transfer (#4786)

* Ensure `accept_traffic_until` is set with `trial_expiry_date` for teams

* Fix pruning guest memberships and invitations during site transfer
This commit is contained in:
Adrian Gruntkowski 2024-11-07 11:53:42 +01:00 committed by GitHub
parent 58ad39b5cc
commit e1cdbfb9ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 23 deletions

View File

@ -338,7 +338,8 @@ defmodule Plausible.Teams.Invitations do
|> Enum.unzip()
old_guest_ids = Enum.map(old_guest_invitations, & &1.id)
:ok = prune_guest_invitations(prior_team, ignore_guest_ids: old_guest_ids)
Repo.delete_all(from gi in Teams.GuestInvitation, where: gi.id in ^old_guest_ids)
:ok = prune_guest_invitations(prior_team)
{_old_team_memberships, old_guest_memberships} =
site.guest_memberships
@ -364,7 +365,8 @@ defmodule Plausible.Teams.Invitations do
|> Enum.unzip()
old_guest_ids = Enum.map(old_guest_memberships, & &1.id)
:ok = Teams.Memberships.prune_guests(prior_team, ignore_guest_ids: old_guest_ids)
Repo.delete_all(from gm in Teams.GuestMembership, where: gm.id in ^old_guest_ids)
:ok = Teams.Memberships.prune_guests(prior_team)
{:ok, prior_owner} = Teams.Sites.get_owner(prior_team)
@ -386,14 +388,11 @@ defmodule Plausible.Teams.Invitations do
:ok
end
def prune_guest_invitations(team, opts \\ []) do
ignore_guest_ids = Keyword.get(opts, :ignore_guest_ids, [])
def prune_guest_invitations(team) do
guest_query =
from(
gi in Teams.GuestInvitation,
where: gi.team_invitation_id == parent_as(:team_invitation).id,
where: gi.id not in ^ignore_guest_ids,
select: true
)

View File

@ -94,14 +94,11 @@ defmodule Plausible.Teams.Memberships do
end
end
def prune_guests(team, opts \\ []) do
ignore_guest_ids = Keyword.get(opts, :ignore_guest_ids, [])
def prune_guests(team) do
guest_query =
from(
gm in Teams.GuestMembership,
where: gm.team_membership_id == parent_as(:team_membership).id,
where: gm.id not in ^ignore_guest_ids,
select: true
)

View File

@ -41,21 +41,14 @@ defmodule Plausible.Teams.Team do
end
def changeset(name, today \\ Date.utc_today()) do
trial_expiry_date =
if ee?() do
Date.shift(today, day: 30)
else
Date.shift(today, year: 100)
end
%__MODULE__{}
|> cast(%{name: name}, [:name])
|> validate_required(:name)
|> put_change(:trial_expiry_date, trial_expiry_date)
|> start_trial(today)
end
def start_trial(team) do
trial_expiry = trial_expiry()
def start_trial(team, today \\ Date.utc_today()) do
trial_expiry = trial_expiry(today)
change(team,
trial_expiry_date: trial_expiry,
@ -69,11 +62,11 @@ defmodule Plausible.Teams.Team do
Map.from_struct(grace_period)
end
defp trial_expiry() do
defp trial_expiry(today) do
on_ee do
Date.utc_today() |> Date.shift(day: 30)
Date.shift(today, day: 30)
else
Date.utc_today() |> Date.shift(year: 100)
Date.shift(today, year: 100)
end
end
end