Skip to content

Commit

Permalink
Get rid of memberships prosthetic for `Teams.Sites.list_with_invitati…
Browse files Browse the repository at this point in the history
…ons` (#4764)

* Get rid of memberships prosthetic for `Teams.Sites.list_with_invitations`

* Fix query in newly introduced teams backfill script
  • Loading branch information
zoldar authored Nov 5, 2024
1 parent af5eb72 commit 1e1a4ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/plausible/data_migration/backfill_teams.ex
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ defmodule Plausible.DataMigration.BackfillTeams do
gm in Teams.GuestMembership,
inner_join: tm in assoc(gm, :team_membership),
inner_join: s in assoc(gm, :site),
where: tm.team_id != s.id
where: tm.team_id != s.team_id
)
|> @repo.all()

Expand Down
38 changes: 29 additions & 9 deletions lib/plausible/teams/sites.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ defmodule Plausible.Teams.Sites do
site_id: s.id,
entry_type: "site",
invitation_id: 0,
invitation_role: nil,
role: tm.role,
transfer_id: 0
}

Expand All @@ -148,7 +148,17 @@ defmodule Plausible.Teams.Sites do
site_id: s.id,
entry_type: "site",
invitation_id: 0,
invitation_role: nil,
role:
fragment(
"""
CASE
WHEN ? = 'editor' THEN 'admin'
ELSE ?
END
""",
gm.role,
gm.role
),
transfer_id: 0
}
)
Expand All @@ -175,7 +185,7 @@ defmodule Plausible.Teams.Sites do
site_id: s.id,
entry_type: "invitation",
invitation_id: ti.id,
invitation_role:
role:
fragment(
"""
CASE
Expand Down Expand Up @@ -207,7 +217,7 @@ defmodule Plausible.Teams.Sites do
site_id: s.id,
entry_type: "invitation",
invitation_id: 0,
invitation_role: "owner",
role: "owner",
transfer_id: st.id
}

Expand Down Expand Up @@ -244,11 +254,18 @@ defmodule Plausible.Teams.Sites do
:entry_type
),
pinned_at: selected_as(up.pinned_at, :pinned_at),
memberships: [
%Plausible.Site.Membership{
role: type(u.role, ^@role_type),
site_id: s.id,
site: s
}
],
invitations: [
%Plausible.Auth.Invitation{
invitation_id: coalesce(ti.invitation_id, st.transfer_id),
email: coalesce(ti.email, st.email),
role: type(coalesce(u.invitation_role, "owner"), ^@role_type),
role: type(u.role, ^@role_type),
site_id: s.id,
site: s
}
Expand All @@ -263,10 +280,13 @@ defmodule Plausible.Teams.Sites do
|> maybe_filter_by_domain(domain_filter)
|> Repo.paginate(pagination_params)
|> Map.update!(:entries, fn entries ->
# temporary prosthetic, when switching to the new model eventually,
# we'll have to preload memberships from new schemas
membership_query = from sm in Site.Membership, where: sm.user_id == ^user.id
Repo.preload(entries, memberships: membership_query)
Enum.map(entries, fn
%{invitation: [%{invitation_id: nil}]} = entry ->
%{entry | invitations: []}

entry ->
entry
end)
end)
end

Expand Down

0 comments on commit 1e1a4ab

Please sign in to comment.