Bring back search by member email and name in Sites/Plans CRM (#4907)

* Bring back search by member email and name in Sites CRM

* Bring back search by owner name and email for enterprise plans CRM

* Try fixing the workaround to work with release

* Fix formatting

* Fix for CE
This commit is contained in:
Adrian Gruntkowski 2024-12-16 15:08:05 +01:00 committed by GitHub
parent 1f0b9e9cf3
commit 8b6f317d1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 4 deletions

View File

@ -29,8 +29,22 @@ defmodule Plausible.Billing.EnterprisePlanAdmin do
]
end
def custom_index_query(_conn, _schema, query) do
from(r in query, preload: [team: :owner])
def custom_index_query(conn, _schema, query) do
search =
(conn.params["custom_search"] || "")
|> String.trim()
|> String.replace("%", "\%")
|> String.replace("_", "\_")
search_term = "%#{search}%"
from(r in query,
inner_join: t in assoc(r, :team),
inner_join: o in assoc(t, :owner),
or_where: ilike(r.paddle_plan_id, ^search_term),
or_where: ilike(o.email, ^search_term) or ilike(o.name, ^search_term),
preload: [team: {t, owner: o}]
)
end
def custom_show_query(_conn, _schema, query) do

View File

@ -6,6 +6,9 @@ defmodule Plausible.CrmExtensions do
use Plausible
on_ee do
# Kaffy uses String.to_existing_atom when listing params
@custom_search :custom_search
def javascripts(%{assigns: %{context: "auth", resource: "user", entry: %{} = user}}) do
[
Phoenix.HTML.raw("""
@ -45,6 +48,36 @@ defmodule Plausible.CrmExtensions do
]
end
def javascripts(%{assigns: %{context: context}})
when context in ["sites", "billing"] do
[
Phoenix.HTML.raw("""
<script type="text/javascript">
(() => {
const publicField = document.querySelector("#kaffy-search-field")
const searchForm = document.querySelector("#kaffy-filters-form")
const searchField = document.querySelector("#kaffy-filter-search")
if (publicField && searchForm && searchField) {
publicField.name = "#{@custom_search}"
searchField.name = "#{@custom_search}"
const params = new URLSearchParams(window.location.search)
publicField.value = params.get("#{@custom_search}")
const searchInput = document.createElement("input")
searchInput.name = "search"
searchInput.type = "hidden"
searchInput.value = ""
searchForm.appendChild(searchInput)
}
})()
</script>
""")
]
end
def javascripts(%{assigns: %{context: "billing", resource: "enterprise_plan", changeset: %{}}}) do
[
Phoenix.HTML.raw("""

View File

@ -13,10 +13,32 @@ defmodule Plausible.SiteAdmin do
]
end
def custom_index_query(_conn, _schema, query) do
def custom_index_query(conn, _schema, query) do
search =
(conn.params["custom_search"] || "")
|> String.trim()
|> String.replace("%", "\%")
|> String.replace("_", "\_")
search_term = "%#{search}%"
member_query =
from s in Plausible.Site,
left_join: gm in assoc(s, :guest_memberships),
left_join: tm in assoc(gm, :team_membership),
left_join: u in assoc(tm, :user),
where: s.id == parent_as(:site).id,
where: ilike(u.email, ^search_term) or ilike(u.name, ^search_term),
select: 1
from(r in query,
as: :site,
inner_join: o in assoc(r, :owner),
preload: [owner: o, team: [team_memberships: :user]]
preload: [owner: o, team: [team_memberships: :user]],
or_where: ilike(r.domain, ^search_term),
or_where: ilike(o.email, ^search_term),
or_where: ilike(o.name, ^search_term),
or_where: exists(member_query)
)
end