Skip to content

Commit

Permalink
Fix team struct sync and pruning team memberships/invitations on site…
Browse files Browse the repository at this point in the history
… transfer (#4786)

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

* Fix pruning guest memberships and invitations during site transfer
  • Loading branch information
zoldar authored Nov 7, 2024
1 parent 58ad39b commit e1cdbfb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
11 changes: 5 additions & 6 deletions lib/plausible/teams/invitations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
)

Expand Down
5 changes: 1 addition & 4 deletions lib/plausible/teams/memberships.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
19 changes: 6 additions & 13 deletions lib/plausible/teams/team.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

0 comments on commit e1cdbfb

Please sign in to comment.