From 71479f4355f1e4cb7363cbc939cdf5c51eadd176 Mon Sep 17 00:00:00 2001 From: GuwandykA Date: Tue, 18 Aug 2026 12:28:21 +0500 Subject: [PATCH] Search users by username, first name, last name, and full name across all list endpoints. Co-authored-by: Cursor --- docs/docs.go | 26 +++++++++++++++++++++---- docs/swagger.json | 26 +++++++++++++++++++++---- docs/swagger.yaml | 20 +++++++++++++++---- internal/identity/user/db/postgresql.go | 24 ++++++++++++++--------- internal/identity/user/dto.go | 6 +++--- internal/identity/user/handler.go | 15 +++++++++----- 6 files changed, 88 insertions(+), 29 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 3aa7f37..0d07cd9 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -5324,7 +5324,13 @@ const docTemplate = `{ "parameters": [ { "type": "string", - "description": "Filter by username", + "description": "Search by username, first name, or last name", + "name": "search", + "in": "query" + }, + { + "type": "string", + "description": "Alias for search", "name": "username", "in": "query" }, @@ -5421,7 +5427,7 @@ const docTemplate = `{ "BearerAuth": [] } ], - "description": "Returns paginated users from the users table with role user", + "description": "Returns paginated client users (role user, not assigned to any branch)", "produces": [ "application/json" ], @@ -5432,7 +5438,13 @@ const docTemplate = `{ "parameters": [ { "type": "string", - "description": "Filter by username", + "description": "Search by username, first name, or last name", + "name": "search", + "in": "query" + }, + { + "type": "string", + "description": "Alias for search", "name": "username", "in": "query" }, @@ -5491,7 +5503,13 @@ const docTemplate = `{ "parameters": [ { "type": "string", - "description": "Filter by username", + "description": "Search by username, first name, or last name", + "name": "search", + "in": "query" + }, + { + "type": "string", + "description": "Alias for search", "name": "username", "in": "query" }, diff --git a/docs/swagger.json b/docs/swagger.json index 00fd2fd..2427031 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -5318,7 +5318,13 @@ "parameters": [ { "type": "string", - "description": "Filter by username", + "description": "Search by username, first name, or last name", + "name": "search", + "in": "query" + }, + { + "type": "string", + "description": "Alias for search", "name": "username", "in": "query" }, @@ -5415,7 +5421,7 @@ "BearerAuth": [] } ], - "description": "Returns paginated users from the users table with role user", + "description": "Returns paginated client users (role user, not assigned to any branch)", "produces": [ "application/json" ], @@ -5426,7 +5432,13 @@ "parameters": [ { "type": "string", - "description": "Filter by username", + "description": "Search by username, first name, or last name", + "name": "search", + "in": "query" + }, + { + "type": "string", + "description": "Alias for search", "name": "username", "in": "query" }, @@ -5485,7 +5497,13 @@ "parameters": [ { "type": "string", - "description": "Filter by username", + "description": "Search by username, first name, or last name", + "name": "search", + "in": "query" + }, + { + "type": "string", + "description": "Alias for search", "name": "username", "in": "query" }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index fafe0f9..54ec512 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -4655,7 +4655,11 @@ paths: get: description: Returns paginated users with role user, with optional filters parameters: - - description: Filter by username + - description: Search by username, first name, or last name + in: query + name: search + type: string + - description: Alias for search in: query name: username type: string @@ -4827,9 +4831,13 @@ paths: - user /users/clients: get: - description: Returns paginated users from the users table with role user + description: Returns paginated client users (role user, not assigned to any branch) parameters: - - description: Filter by username + - description: Search by username, first name, or last name + in: query + name: search + type: string + - description: Alias for search in: query name: username type: string @@ -4868,7 +4876,11 @@ paths: description: Returns paginated users assigned to branches (from branch_user), including each user's branches. parameters: - - description: Filter by username + - description: Search by username, first name, or last name + in: query + name: search + type: string + - description: Alias for search in: query name: username type: string diff --git a/internal/identity/user/db/postgresql.go b/internal/identity/user/db/postgresql.go index 71eef7e..3b74f0d 100644 --- a/internal/identity/user/db/postgresql.go +++ b/internal/identity/user/db/postgresql.go @@ -88,10 +88,7 @@ func (r *repository) ListStaff(ctx context.Context, filter user.ListFilter) (use func buildStaffWhere(filter user.ListFilter) (whereClause string, args []interface{}) { conditions := []string{`EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`} - if filter.Username != "" { - args = append(args, likePattern(filter.Username)) - conditions = append(conditions, fmt.Sprintf(`u.username ILIKE $%d`, len(args))) - } + conditions, args = appendNameSearch(conditions, args, filter.Search) return " WHERE " + strings.Join(conditions, " AND "), args } @@ -136,11 +133,7 @@ func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause fromClause = ` FROM users u ` var conditions []string - if filter.Username != "" { - args = append(args, likePattern(filter.Username)) - n := len(args) - conditions = append(conditions, fmt.Sprintf(`u.username ILIKE $%d`, n)) - } + conditions, args = appendNameSearch(conditions, args, filter.Search) if len(roles) == 1 { args = append(args, roles[0]) n := len(args) @@ -161,6 +154,19 @@ func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause return fromClause, " WHERE " + strings.Join(conditions, " AND "), args } +func appendNameSearch(conditions []string, args []interface{}, term string) ([]string, []interface{}) { + if term == "" { + return conditions, args + } + args = append(args, likePattern(term)) + n := len(args) + conditions = append(conditions, fmt.Sprintf( + `(u.username ILIKE $%d OR u.first_name ILIKE $%d OR u.last_name ILIKE $%d OR CONCAT_WS(' ', u.first_name, u.last_name) ILIKE $%d)`, + n, n, n, n, + )) + return conditions, args +} + func likePattern(term string) string { term = strings.ReplaceAll(term, `\`, `\\`) term = strings.ReplaceAll(term, `%`, `\%`) diff --git a/internal/identity/user/dto.go b/internal/identity/user/dto.go index f9c8257..c1ff2dc 100644 --- a/internal/identity/user/dto.go +++ b/internal/identity/user/dto.go @@ -56,9 +56,9 @@ type UpdateUserDTO struct { } type ListFilter struct { - Username string - Page int - Limit int + Search string + Page int + Limit int } type ListUserDTO struct { diff --git a/internal/identity/user/handler.go b/internal/identity/user/handler.go index 5a8d3bc..3b8c355 100644 --- a/internal/identity/user/handler.go +++ b/internal/identity/user/handler.go @@ -62,7 +62,8 @@ func (h *handler) Register(router *gin.RouterGroup) { // @Tags user // @Produce json // @Security BearerAuth -// @Param username query string false "Filter by username" +// @Param search query string false "Search by username, first name, or last name" +// @Param username query string false "Alias for search" // @Param page query int false "Page number" default(1) // @Param limit query int false "Items per page" default(10) // @Success 200 {object} appresult.AppSuccess @@ -92,7 +93,8 @@ func (h *handler) list(c *gin.Context) { // @Tags user // @Produce json // @Security BearerAuth -// @Param username query string false "Filter by username" +// @Param search query string false "Search by username, first name, or last name" +// @Param username query string false "Alias for search" // @Param page query int false "Page number" default(1) // @Param limit query int false "Items per page" default(10) // @Success 200 {object} appresult.AppSuccess @@ -122,7 +124,8 @@ func (h *handler) listClients(c *gin.Context) { // @Tags user // @Produce json // @Security BearerAuth -// @Param username query string false "Filter by username" +// @Param search query string false "Search by username, first name, or last name" +// @Param username query string false "Alias for search" // @Param page query int false "Page number" default(1) // @Param limit query int false "Items per page" default(10) // @Success 200 {object} appresult.AppSuccess @@ -316,8 +319,10 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) { filter.Limit = limit } - if raw := strings.TrimSpace(c.Query("username")); raw != "" { - filter.Username = raw + if raw := strings.TrimSpace(c.Query("search")); raw != "" { + filter.Search = raw + } else if raw := strings.TrimSpace(c.Query("username")); raw != "" { + filter.Search = raw } return filter, true