From 2a9682aea20b90bd939d7e2fe6d0415f2d2eedf9 Mon Sep 17 00:00:00 2001 From: Mazyar Date: Mon, 22 Jun 2026 22:16:36 +0330 Subject: [PATCH] Enhance company repository search functionality - Updated the `Search` method in the `companyRepository` to improve search capabilities by allowing regex-based filtering on `name`, `email`, and `phone` fields. - Added logic to handle numeric search queries for `employee_count`, enhancing the search flexibility. - Implemented input sanitization to trim whitespace from search queries, ensuring cleaner search inputs. This update improves the search functionality within the company repository, providing more robust and flexible search options for users. --- internal/company/repository.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/internal/company/repository.go b/internal/company/repository.go index 85ec6dc..c511d58 100644 --- a/internal/company/repository.go +++ b/internal/company/repository.go @@ -3,6 +3,9 @@ package company import ( "context" "errors" + "regexp" + "strconv" + "strings" "time" "tm/pkg/logger" orm "tm/pkg/mongo" @@ -210,7 +213,19 @@ func (r *companyRepository) Search(ctx context.Context, form *SearchForm, pagina filter := bson.M{} if form.Search != nil { - filter["$text"] = bson.M{"$search": *form.Search} + search := strings.TrimSpace(*form.Search) + if search != "" { + pattern := regexp.QuoteMeta(search) + orConditions := []bson.M{ + {"name": bson.M{"$regex": pattern, "$options": "i"}}, + {"email": bson.M{"$regex": pattern, "$options": "i"}}, + {"phone": bson.M{"$regex": pattern, "$options": "i"}}, + } + if employeeCount, err := strconv.Atoi(search); err == nil { + orConditions = append(orConditions, bson.M{"employee_count": employeeCount}) + } + filter["$or"] = orConditions + } } if form.Type != nil {