Fixups sites (#3482)

* Revert "Fix sites query invitation left join lookup (#3480)"

This reverts commit 6e6508a359.

* Revert "Revert "Fix sites query invitation left join lookup (#3480)""

This reverts commit f5cc831d7a.

* Revert "Fix sites query invitation left join lookup (#3480)"

This reverts commit 6e6508a359.

* Fix sites listing
This commit is contained in:
hq1 2023-11-02 16:29:01 +01:00 committed by GitHub
parent df44f549d8
commit 08bd10614f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 10 deletions

View File

@ -19,10 +19,10 @@ defmodule Plausible.Sites do
base_query = base_query =
from(s in Plausible.Site, from(s in Plausible.Site,
left_join: sm in assoc(s, :memberships), left_join: sm in assoc(s, :memberships),
on: sm.user_id == ^user.id,
left_join: i in assoc(s, :invitations), left_join: i in assoc(s, :invitations),
where: on: i.email == ^user.email,
(is_nil(i.id) and sm.user_id == ^user.id) or where: not is_nil(i.id) or not is_nil(sm.id),
(not is_nil(i.id) and i.email == ^user.email),
select: %{ select: %{
s s
| list_type: | list_type:

View File

@ -98,24 +98,38 @@ defmodule Plausible.SitesTest do
end end
test "returns invitations and sites" do test "returns invitations and sites" do
user = insert(:user) user = insert(:user, email: "hello@example.com")
%{id: site_id1} = insert(:site, members: [user]) site1 = %{id: site_id1} = insert(:site, members: [user], domain: "one.example.com")
%{id: site_id2} = insert(:site, members: [user]) %{id: site_id2} = insert(:site, members: [user], domain: "two.example.com")
_rogue_site = insert(:site) %{id: site_id4} = insert(:site, members: [user], domain: "four.example.com")
_rogue_site = insert(:site, domain: "rogue.example.com")
insert(:invitation, email: user.email, inviter: build(:user), role: :owner, site: site1)
%{id: site_id3} = %{id: site_id3} =
insert(:site, insert(:site,
domain: "three.example.com",
invitations: [ invitations: [
build(:invitation, email: user.email, inviter: build(:user), role: :viewer) build(:invitation, email: user.email, inviter: build(:user), role: :viewer)
] ]
) )
insert(:invitation, email: "friend@example.com", inviter: user, role: :viewer, site: site1)
insert(:invitation,
site: site1,
inviter: user,
email: "another@example.com"
)
assert %{ assert %{
entries: [ entries: [
%{id: ^site_id3}, %{id: ^site_id1, list_type: "invitation"},
%{id: ^site_id1}, %{id: ^site_id3, list_type: "invitation"},
%{id: ^site_id2} %{id: ^site_id4, list_type: "site"},
%{id: ^site_id2, list_type: "site"}
] ]
} = Sites.list(user, %{}) } = Sites.list(user, %{})
end end