feat(crm): route crm search through meilisearch
This commit is contained in:
@@ -280,6 +280,38 @@ func (r *SearchRepo) searchContactsInternal(ctx context.Context, accountID uint,
|
||||
return contacts, total, err
|
||||
}
|
||||
|
||||
// SearchCompanies searches companies with advanced filters.
|
||||
func (r *SearchRepo) SearchCompanies(ctx context.Context, accountID uint, query string, filter *search.SearchFilter) ([]model.Company, int64, error) {
|
||||
repoFilter := searchFilterToRepo(filter)
|
||||
return r.searchCompaniesInternal(ctx, accountID, query, repoFilter)
|
||||
}
|
||||
|
||||
func (r *SearchRepo) searchCompaniesInternal(ctx context.Context, accountID uint, query string, filter *RepoSearchFilter) ([]model.Company, int64, error) {
|
||||
var companies []model.Company
|
||||
var total int64
|
||||
|
||||
q := r.db.WithContext(ctx).Model(&model.Company{}).Where("account_id = ?", accountID)
|
||||
if query != "" {
|
||||
if filter.IsTrigram() {
|
||||
q = q.Where("name % ? OR description % ? OR domain % ? OR website_url % ?", query, query, query, query)
|
||||
} else {
|
||||
likeQuery := "%" + query + "%"
|
||||
q = q.Where("LOWER(name) LIKE LOWER(?) OR LOWER(description) LIKE LOWER(?) OR LOWER(domain) LIKE LOWER(?) OR LOWER(website_url) LIKE LOWER(?)",
|
||||
likeQuery, likeQuery, likeQuery, likeQuery)
|
||||
}
|
||||
}
|
||||
|
||||
q = applyDateRangeFilter(q, filter)
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, fmt.Errorf("search companies count: %w", err)
|
||||
}
|
||||
|
||||
err := q.Offset(filter.Offset()).Limit(filter.PerPage).
|
||||
Order(filter.OrderClause() + ", id DESC").
|
||||
Find(&companies).Error
|
||||
return companies, total, err
|
||||
}
|
||||
|
||||
// SearchArticles searches knowledge base articles with advanced filters.
|
||||
// This method satisfies search.SearchRepoInterface by accepting *search.SearchFilter
|
||||
// and converting it to *RepoSearchFilter internally.
|
||||
@@ -470,4 +502,4 @@ func applyDateRangeFilter(q *gorm.DB, filter *RepoSearchFilter) *gorm.DB {
|
||||
}
|
||||
|
||||
return q
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user