Merge branch 'main' of ssh://git.ma-al.com:8822/goc_daniel/b2b into translate

This commit is contained in:
2026-04-08 10:09:40 +02:00
5 changed files with 63 additions and 20 deletions

View File

@@ -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)))

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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
}