fix(search): scope global result types
This commit is contained in:
@@ -15,6 +15,13 @@ import (
|
||||
|
||||
const chatwootSearchPerPage = 15
|
||||
|
||||
var chatwootGlobalSearchTypes = []search.SearchResultType{
|
||||
search.ResultTypeConversation,
|
||||
search.ResultTypeContact,
|
||||
search.ResultTypeMessage,
|
||||
search.ResultTypeArticle,
|
||||
}
|
||||
|
||||
// SearchHandler handles global search API endpoints.
|
||||
// Reference: Chatwoot GlobalSearchService — cross-entity search with advanced filtering.
|
||||
type SearchHandler struct {
|
||||
@@ -79,6 +86,7 @@ func (h *SearchHandler) GlobalSearch(c *gin.Context) {
|
||||
|
||||
query := c.Query("q")
|
||||
filter := parseChatwootSearchFilter(c)
|
||||
filter.Types = append([]search.SearchResultType(nil), chatwootGlobalSearchTypes...)
|
||||
|
||||
result, svcErr := h.svc.GlobalSearch(c.Request.Context(), accountID, query, &filter)
|
||||
if svcErr != nil {
|
||||
|
||||
@@ -35,9 +35,10 @@ type mockSearchRepo struct {
|
||||
contactTotal int64
|
||||
contactErr error
|
||||
|
||||
companies []model.Company
|
||||
companyTotal int64
|
||||
companyErr error
|
||||
companies []model.Company
|
||||
companyTotal int64
|
||||
companyErr error
|
||||
companyCalled bool
|
||||
|
||||
articles []model.Article
|
||||
articleTotal int64
|
||||
@@ -58,6 +59,7 @@ func (m *mockSearchRepo) SearchContacts(ctx context.Context, accountID uint, que
|
||||
}
|
||||
|
||||
func (m *mockSearchRepo) SearchCompanies(ctx context.Context, accountID uint, query string, filter *search.SearchFilter) ([]model.Company, int64, error) {
|
||||
m.companyCalled = true
|
||||
return m.companies, m.companyTotal, m.companyErr
|
||||
}
|
||||
|
||||
@@ -176,6 +178,33 @@ func TestSearchHandler_GlobalSearch_WithFilterParams(t *testing.T) {
|
||||
assert.Len(t, payload["conversations"], 1)
|
||||
}
|
||||
|
||||
func TestSearchHandler_GlobalSearch_UsesReferenceResultTypes(t *testing.T) {
|
||||
repo := &mockSearchRepo{
|
||||
companies: []model.Company{{ID: 99, AccountID: 1, Name: "Acme"}},
|
||||
companyTotal: 1,
|
||||
}
|
||||
svc := search.NewSearchService(repo)
|
||||
handler := NewSearchHandler(svc)
|
||||
router := setupSearchHandlerRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/api/v1/accounts/1/search?q=Acme&types=company", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.False(t, repo.companyCalled)
|
||||
|
||||
var body map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
payload, ok := body["payload"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
assert.Contains(t, payload, "conversations")
|
||||
assert.Contains(t, payload, "contacts")
|
||||
assert.Contains(t, payload, "messages")
|
||||
assert.Contains(t, payload, "articles")
|
||||
assert.NotContains(t, payload, "companies")
|
||||
}
|
||||
|
||||
// ========== SearchConversations handler tests ==========
|
||||
|
||||
func TestSearchHandler_SearchConversations_Success(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user