feat: searching on customer list #54
@@ -70,12 +70,6 @@ func (h *customerHandler) customerData(fc fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *customerHandler) listCustomers(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)
|
user, ok := localeExtractor.GetCustomer(fc)
|
||||||
if !ok || user == nil {
|
if !ok || user == nil {
|
||||||
return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
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)))
|
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 {
|
if err != nil {
|
||||||
return fc.Status(responseErrors.GetErrorStatus(err)).
|
return fc.Status(responseErrors.GetErrorStatus(err)).
|
||||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, err)))
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, err)))
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package customerRepo
|
package customerRepo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/db"
|
"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/model"
|
||||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/filters"
|
"git.ma-al.com/goc_daniel/b2b/app/utils/query/filters"
|
||||||
@@ -11,7 +13,7 @@ type UICustomerRepo interface {
|
|||||||
Get(id uint) (*model.Customer, error)
|
Get(id uint) (*model.Customer, error)
|
||||||
GetByEmail(email string) (*model.Customer, error)
|
GetByEmail(email string) (*model.Customer, error)
|
||||||
GetByExternalProviderId(provider model.AuthProvider, id 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
|
Save(customer *model.Customer) error
|
||||||
Create(customer *model.Customer) error
|
Create(customer *model.Customer) error
|
||||||
}
|
}
|
||||||
@@ -57,17 +59,46 @@ func (repo *CustomerRepo) GetByExternalProviderId(provider model.AuthProvider, i
|
|||||||
return &customer, err
|
return &customer, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *CustomerRepo) Find(langId uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.UserInList], error) {
|
func (repo *CustomerRepo) Find(langId uint, p find.Paging, filt *filters.FiltersList, search string) (*find.Found[model.UserInList], error) {
|
||||||
found, err := find.Paginate[model.UserInList](langId, p, db.DB.
|
|
||||||
|
query := db.DB.
|
||||||
Table("b2b_customers AS users").
|
Table("b2b_customers AS users").
|
||||||
Select(`
|
Select(`
|
||||||
users.id AS id,
|
users.id AS id,
|
||||||
users.email AS email,
|
users.email AS email,
|
||||||
users.first_name AS first_name,
|
users.first_name AS first_name,
|
||||||
users.last_name AS last_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
|
return &found, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,6 @@ func (s *CustomerService) GetById(id uint) (*model.Customer, error) {
|
|||||||
return s.repo.Get(id)
|
return s.repo.Get(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CustomerService) Find(langId uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.UserInList], error) {
|
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)
|
return s.repo.Find(langId, p, filt, search)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,14 +42,14 @@ func Paginate[T any](langID uint, paging Paging, stmt *gorm.DB) (Found[T], error
|
|||||||
return Found[T]{}, err
|
return Found[T]{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
columnsSpec := GetColumnsSpec[T](langID)
|
// columnsSpec := GetColumnsSpec[T](langID)
|
||||||
|
|
||||||
return Found[T]{
|
return Found[T]{
|
||||||
Items: items,
|
Items: items,
|
||||||
Count: uint(count),
|
Count: uint(count),
|
||||||
Spec: map[string]interface{}{
|
// Spec: map[string]interface{}{
|
||||||
"columns": columnsSpec,
|
// "columns": columnsSpec,
|
||||||
},
|
// },
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,11 @@ info:
|
|||||||
|
|
||||||
http:
|
http:
|
||||||
method: GET
|
method: GET
|
||||||
url: "{{bas_url}}/restricted/customer/list"
|
url: "{{bas_url}}/restricted/customer/list?search="
|
||||||
|
params:
|
||||||
|
- name: search
|
||||||
|
value: ""
|
||||||
|
type: query
|
||||||
auth: inherit
|
auth: inherit
|
||||||
|
|
||||||
settings:
|
settings:
|
||||||
|
|||||||
Reference in New Issue
Block a user