- 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.
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user