From 83b7cd49dd458717ce75fdd440453a2b4aa68ed2 Mon Sep 17 00:00:00 2001 From: Wiktor Date: Mon, 13 Apr 2026 14:43:18 +0200 Subject: [PATCH] feat: lookup by id in customer search --- app/delivery/web/api/restricted/customer.go | 13 +-- app/repos/customerRepo/customerRepo.go | 93 ++------------------- 2 files changed, 8 insertions(+), 98 deletions(-) diff --git a/app/delivery/web/api/restricted/customer.go b/app/delivery/web/api/restricted/customer.go index 6e1a41c..e280d4f 100644 --- a/app/delivery/web/api/restricted/customer.go +++ b/app/delivery/web/api/restricted/customer.go @@ -3,6 +3,7 @@ package restricted import ( "strconv" + "git.ma-al.com/goc_daniel/b2b/app/delivery/middleware" "git.ma-al.com/goc_daniel/b2b/app/delivery/middleware/perms" "git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/service/customerService" @@ -30,7 +31,7 @@ func CustomerHandlerRoutes(r fiber.Router) fiber.Router { handler := NewCustomerHandler() r.Get("", handler.customerData) - r.Get("/list", handler.listCustomers) + r.Get("/list", middleware.Require(perms.UserReadAny), handler.listCustomers) return r } @@ -75,10 +76,6 @@ func (h *customerHandler) listCustomers(fc fiber.Ctx) error { return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute))) } - if !user.HasPermission(perms.UserReadAny) { - return fc.Status(fiber.StatusForbidden). - JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrForbidden))) - } p, filt, err := query_params.ParseFilters[model.Customer](fc, columnMappingListUsers) if err != nil { @@ -87,12 +84,6 @@ func (h *customerHandler) listCustomers(fc fiber.Ctx) error { } 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 { diff --git a/app/repos/customerRepo/customerRepo.go b/app/repos/customerRepo/customerRepo.go index 18dea15..8e55063 100644 --- a/app/repos/customerRepo/customerRepo.go +++ b/app/repos/customerRepo/customerRepo.go @@ -1,6 +1,7 @@ package customerRepo import ( + "fmt" "strings" "git.ma-al.com/goc_daniel/b2b/app/db" @@ -80,13 +81,16 @@ func (repo *CustomerRepo) Find(langId uint, p find.Paging, filt *filters.Filters for _, word := range words { conditions = append(conditions, ` - (LOWER(first_name) LIKE ? OR + ( + id = ? OR + LOWER(first_name) LIKE ? OR LOWER(last_name) LIKE ? OR LOWER(email) LIKE ?) `) + args = append(args, strings.ToLower(word)) for range 3 { - args = append(args, "%"+strings.ToLower(word)+"%") + args = append(args, fmt.Sprintf("%%%s%%", strings.ToLower(word))) } } @@ -110,88 +114,3 @@ func (repo *CustomerRepo) Save(customer *model.Customer) error { func (repo *CustomerRepo) Create(customer *model.Customer) error { return db.DB.Create(customer).Error } - -// func (repo *CustomerRepo) Search( -// customerId uint, -// partnerCode string, -// p find.Paging, -// filt *filters.FiltersList, -// search string, -// ) (found find.Found[model.UserInList], err error) { -// words := strings.Fields(search) -// if len(words) > 5 { -// words = words[:5] -// } - -// query := ctx.DB(). -// Model(&model.Customer{}). -// Select("customer.id AS id, customer.first_name as first_name, customer.last_name as last_name, customer.phone_number AS phone_number, customer.email AS email, count(distinct investment_plan_contract.id) as iiplan_purchases, count(distinct `order`.id) as single_purchases, entity.name as entity_name"). -// Where("customer.id <> ?", customerId). -// Where("(customer.id IN (SELECT id FROM customer WHERE partner_code IN (WITH RECURSIVE partners AS (SELECT code AS dst FROM partner WHERE code = ? UNION SELECT code FROM partner JOIN partners ON partners.dst = partner.superior_code) SELECT dst FROM partners)) OR customer.recommender_code = ?)", partnerCode, partnerCode). -// Scopes(view.CustomerListQuery()) - -// var conditions []string -// var args []interface{} -// for _, word := range words { - -// conditions = append(conditions, ` -// (LOWER(first_name) LIKE ? OR -// LOWER(last_name) LIKE ? OR -// phone_number LIKE ? OR -// LOWER(email) LIKE ?) -// `) - -// for i := 0; i < 4; i++ { -// args = append(args, "%"+strings.ToLower(word)+"%") -// } -// } - -// finalQuery := strings.Join(conditions, " AND ") - -// query = query.Where(finalQuery, args...). -// Scopes(filt.All()...) - -// found, err = find.Paginate[V](ctx, p, query) - -// return found, errs.Recorded(span, err) -// } - -// func (repo *ListRepo) ListUsers(id_lang uint, p find.Paging, filt *filters.FiltersList) (find.Found[model.UserInList], error) { -// var list []model.UserInList -// var total int64 - -// query := db.Get(). -// 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, -// users.role AS role -// `) - -// // Apply all filters -// if filt != nil { -// filt.ApplyAll(query) -// } - -// // run counter first as query is without limit and offset -// err := query.Count(&total).Error -// if err != nil { -// return find.Found[model.UserInList]{}, err -// } - -// err = query. -// Order("users.id DESC"). -// Limit(p.Limit()). -// Offset(p.Offset()). -// Find(&list).Error -// if err != nil { -// return find.Found[model.UserInList]{}, err -// } - -// return find.Found[model.UserInList]{ -// Items: list, -// Count: uint(total), -// }, nil -// }