Search users by username, first name, last name, and full name across all list endpoints.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-18 12:28:21 +05:00
parent fb65d35d3c
commit 71479f4355
6 changed files with 88 additions and 29 deletions

View File

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

View File

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

View File

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

View File

@@ -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, `%`, `\%`)

View File

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

View File

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