Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading