From d56650ae5da258176f5f64aa35950b9ab2dbad9f Mon Sep 17 00:00:00 2001 From: Wiktor Date: Tue, 7 Apr 2026 14:42:45 +0200 Subject: [PATCH] feat: searching on customer list --- app/delivery/web/api/restricted/customer.go | 22 +++++++--- app/repos/customerRepo/customerRepo.go | 43 ++++++++++++++++--- .../customerService/customerService.go | 4 +- app/utils/query/find/find.go | 8 ++-- bruno/api_v1/customer/Customer list.yml | 6 ++- 5 files changed, 63 insertions(+), 20 deletions(-) diff --git a/app/delivery/web/api/restricted/customer.go b/app/delivery/web/api/restricted/customer.go index 7c04b7e..6e1a41c 100644 --- a/app/delivery/web/api/restricted/customer.go +++ b/app/delivery/web/api/restricted/customer.go @@ -70,12 +70,6 @@ func (h *customerHandler) customerData(fc fiber.Ctx) error { } func (h *customerHandler) listCustomers(fc fiber.Ctx) error { - p, filt, err := query_params.ParseFilters[model.Customer](fc, columnMappingListUsers) - if err != nil { - return fc.Status(responseErrors.GetErrorStatus(err)). - JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, err))) - } - user, ok := localeExtractor.GetCustomer(fc) if !ok || user == nil { return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). @@ -86,7 +80,21 @@ func (h *customerHandler) listCustomers(fc fiber.Ctx) error { JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrForbidden))) } - customer, err := h.service.Find(user.LangID, p, filt) + p, filt, err := query_params.ParseFilters[model.Customer](fc, columnMappingListUsers) + if err != nil { + return fc.Status(responseErrors.GetErrorStatus(err)). + JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, err))) + } + + search := fc.Query("search") + if search != "" { + if !user.HasPermission(perms.UserReadAny) { + return fc.Status(fiber.StatusForbidden). + JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrForbidden))) + } + } + + customer, err := h.service.Find(user.LangID, p, filt, search) if err != nil { return fc.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, err))) diff --git a/app/repos/customerRepo/customerRepo.go b/app/repos/customerRepo/customerRepo.go index 668785f..18dea15 100644 --- a/app/repos/customerRepo/customerRepo.go +++ b/app/repos/customerRepo/customerRepo.go @@ -1,6 +1,8 @@ package customerRepo import ( + "strings" + "git.ma-al.com/goc_daniel/b2b/app/db" "git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/utils/query/filters" @@ -11,7 +13,7 @@ type UICustomerRepo interface { Get(id uint) (*model.Customer, error) GetByEmail(email string) (*model.Customer, error) GetByExternalProviderId(provider model.AuthProvider, id string) (*model.Customer, error) - Find(langId uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.UserInList], error) + Find(langId uint, p find.Paging, filt *filters.FiltersList, search string) (*find.Found[model.UserInList], error) Save(customer *model.Customer) error Create(customer *model.Customer) error } @@ -57,17 +59,46 @@ func (repo *CustomerRepo) GetByExternalProviderId(provider model.AuthProvider, i return &customer, err } -func (repo *CustomerRepo) Find(langId uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.UserInList], error) { - found, err := find.Paginate[model.UserInList](langId, p, db.DB. +func (repo *CustomerRepo) Find(langId uint, p find.Paging, filt *filters.FiltersList, search string) (*find.Found[model.UserInList], error) { + + query := db.DB. Table("b2b_customers AS users"). Select(` users.id AS id, users.email AS email, users.first_name AS first_name, users.last_name AS last_name - `). - Scopes(filt.All()...), - ) + `) + + if search != "" { + words := strings.Fields(search) + if len(words) > 5 { + words = words[:5] + } + var conditions []string + var args []interface{} + for _, word := range words { + + conditions = append(conditions, ` + (LOWER(first_name) LIKE ? OR + LOWER(last_name) LIKE ? OR + LOWER(email) LIKE ?) + `) + + for range 3 { + args = append(args, "%"+strings.ToLower(word)+"%") + } + } + + conditionsQuery := strings.Join(conditions, " AND ") + + query = query.Where(conditionsQuery, args...) + + } + + query = query.Scopes(filt.All()...) + + found, err := find.Paginate[model.UserInList](langId, p, query) return &found, err } diff --git a/app/service/customerService/customerService.go b/app/service/customerService/customerService.go index f9f2f4a..bce463d 100644 --- a/app/service/customerService/customerService.go +++ b/app/service/customerService/customerService.go @@ -21,6 +21,6 @@ func (s *CustomerService) GetById(id uint) (*model.Customer, error) { return s.repo.Get(id) } -func (s *CustomerService) Find(langId uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.UserInList], error) { - return s.repo.Find(langId, p, filt) +func (s *CustomerService) Find(langId uint, p find.Paging, filt *filters.FiltersList, search string) (*find.Found[model.UserInList], error) { + return s.repo.Find(langId, p, filt, search) } diff --git a/app/utils/query/find/find.go b/app/utils/query/find/find.go index 57ef813..487c1d1 100644 --- a/app/utils/query/find/find.go +++ b/app/utils/query/find/find.go @@ -42,14 +42,14 @@ func Paginate[T any](langID uint, paging Paging, stmt *gorm.DB) (Found[T], error return Found[T]{}, err } - columnsSpec := GetColumnsSpec[T](langID) + // columnsSpec := GetColumnsSpec[T](langID) return Found[T]{ Items: items, Count: uint(count), - Spec: map[string]interface{}{ - "columns": columnsSpec, - }, + // Spec: map[string]interface{}{ + // "columns": columnsSpec, + // }, }, err } diff --git a/bruno/api_v1/customer/Customer list.yml b/bruno/api_v1/customer/Customer list.yml index 0d5bc26..11c286b 100644 --- a/bruno/api_v1/customer/Customer list.yml +++ b/bruno/api_v1/customer/Customer list.yml @@ -5,7 +5,11 @@ info: http: method: GET - url: "{{bas_url}}/restricted/customer/list" + url: "{{bas_url}}/restricted/customer/list?search=" + params: + - name: search + value: "" + type: query auth: inherit settings: -- 2.49.1