Enhance customer search functionality by adding filters for full name, email, and role.

This commit is contained in:
Mazyar
2026-06-01 10:40:37 +03:30
parent cc6c0e1660
commit 1be07bcb71
3 changed files with 44 additions and 6 deletions
+34 -2
View File
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"time"
"tm/pkg/logger"
orm "tm/pkg/mongo"
@@ -370,7 +372,30 @@ func (r *customerRepository) Search(ctx context.Context, form *SearchCustomersFo
filter := bson.M{}
if form.Search != nil {
filter["$text"] = bson.M{"$search": *form.Search}
search := strings.TrimSpace(*form.Search)
if search != "" {
filter["$text"] = bson.M{"$search": search}
}
}
if form.FullName != nil {
fullName := strings.TrimSpace(*form.FullName)
if fullName != "" {
filter["full_name"] = bson.M{
"$regex": regexp.QuoteMeta(fullName),
"$options": "i",
}
}
}
if form.Email != nil {
email := strings.TrimSpace(*form.Email)
if email != "" {
filter["email"] = bson.M{
"$regex": regexp.QuoteMeta(email),
"$options": "i",
}
}
}
if form.Type != nil {
@@ -381,8 +406,15 @@ func (r *customerRepository) Search(ctx context.Context, form *SearchCustomersFo
filter["status"] = *form.Status
}
if form.Role != nil {
filter["role"] = *form.Role
}
if form.CompanyID != nil {
filter["company_id"] = *form.CompanyID
companyID := strings.TrimSpace(*form.CompanyID)
if companyID != "" {
filter["companies"] = companyID
}
}
mongoPagination, err := orm.BuildListPagination(