Skip to content

Commit

Permalink
Bring back search by member email and name in Sites/Plans CRM (#4907)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zoldar authored Dec 16, 2024
1 parent 1f0b9e9 commit 8b6f317
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
18 changes: 16 additions & 2 deletions lib/plausible/billing/enterprise_plan_admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions lib/plausible/crm_extensions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
Expand Down Expand Up @@ -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("""
Expand Down
26 changes: 24 additions & 2 deletions lib/plausible/site/admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8b6f317

Please sign in to comment.