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:
guwandykandCursor committed 2026-08-18 12:28:21 +05:00
1 parent fb65d35d3c
commit 71479f4355
6 files changed
+88 -29

No files matched your search

+22 -4
View File
@@ -5324,7 +5324,13 @@ const docTemplate = `{
"parameters": [ "parameters": [
{ {
"type": "string", "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", "name": "username",
"in": "query" "in": "query"
}, },
@@ -5421,7 +5427,7 @@ const docTemplate = `{
"BearerAuth": [] "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": [ "produces": [
"application/json" "application/json"
], ],
@@ -5432,7 +5438,13 @@ const docTemplate = `{
"parameters": [ "parameters": [
{ {
"type": "string", "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", "name": "username",
"in": "query" "in": "query"
}, },
@@ -5491,7 +5503,13 @@ const docTemplate = `{
"parameters": [ "parameters": [
{ {
"type": "string", "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", "name": "username",
"in": "query" "in": "query"
}, },
+22 -4
View File
@@ -5318,7 +5318,13 @@
"parameters": [ "parameters": [
{ {
"type": "string", "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", "name": "username",
"in": "query" "in": "query"
}, },
@@ -5415,7 +5421,7 @@
"BearerAuth": [] "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": [ "produces": [
"application/json" "application/json"
], ],
@@ -5426,7 +5432,13 @@
"parameters": [ "parameters": [
{ {
"type": "string", "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", "name": "username",
"in": "query" "in": "query"
}, },
@@ -5485,7 +5497,13 @@
"parameters": [ "parameters": [
{ {
"type": "string", "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", "name": "username",
"in": "query" "in": "query"
}, },
+16 -4
View File
@@ -4655,7 +4655,11 @@ paths:
get: get:
description: Returns paginated users with role user, with optional filters description: Returns paginated users with role user, with optional filters
parameters: 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 in: query
name: username name: username
type: string type: string
@@ -4827,9 +4831,13 @@ paths:
- user - user
/users/clients: /users/clients:
get: 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: 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 in: query
name: username name: username
type: string type: string
@@ -4868,7 +4876,11 @@ paths:
description: Returns paginated users assigned to branches (from branch_user), description: Returns paginated users assigned to branches (from branch_user),
including each user's branches. including each user's branches.
parameters: 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 in: query
name: username name: username
type: string type: string
+15 -9
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{}) { func buildStaffWhere(filter user.ListFilter) (whereClause string, args []interface{}) {
conditions := []string{`EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`} conditions := []string{`EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`}
if filter.Username != "" { conditions, args = appendNameSearch(conditions, args, filter.Search)
args = append(args, likePattern(filter.Username))
conditions = append(conditions, fmt.Sprintf(`u.username ILIKE $%d`, len(args)))
}
return " WHERE " + strings.Join(conditions, " AND "), args return " WHERE " + strings.Join(conditions, " AND "), args
} }
@@ -136,11 +133,7 @@ func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause
fromClause = ` FROM users u ` fromClause = ` FROM users u `
var conditions []string var conditions []string
if filter.Username != "" { conditions, args = appendNameSearch(conditions, args, filter.Search)
args = append(args, likePattern(filter.Username))
n := len(args)
conditions = append(conditions, fmt.Sprintf(`u.username ILIKE $%d`, n))
}
if len(roles) == 1 { if len(roles) == 1 {
args = append(args, roles[0]) args = append(args, roles[0])
n := len(args) n := len(args)
@@ -161,6 +154,19 @@ func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause
return fromClause, " WHERE " + strings.Join(conditions, " AND "), args 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 { func likePattern(term string) string {
term = strings.ReplaceAll(term, `\`, `\\`) term = strings.ReplaceAll(term, `\`, `\\`)
term = strings.ReplaceAll(term, `%`, `\%`) term = strings.ReplaceAll(term, `%`, `\%`)
+3 -3
View File
@@ -56,9 +56,9 @@ type UpdateUserDTO struct {
} }
type ListFilter struct { type ListFilter struct {
Username string Search string
Page int Page int
Limit int Limit int
} }
type ListUserDTO struct { type ListUserDTO struct {
+10 -5
View File
@@ -62,7 +62,8 @@ func (h *handler) Register(router *gin.RouterGroup) {
// @Tags user // @Tags user
// @Produce json // @Produce json
// @Security BearerAuth // @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 page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10) // @Param limit query int false "Items per page" default(10)
// @Success 200 {object} appresult.AppSuccess // @Success 200 {object} appresult.AppSuccess
@@ -92,7 +93,8 @@ func (h *handler) list(c *gin.Context) {
// @Tags user // @Tags user
// @Produce json // @Produce json
// @Security BearerAuth // @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 page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10) // @Param limit query int false "Items per page" default(10)
// @Success 200 {object} appresult.AppSuccess // @Success 200 {object} appresult.AppSuccess
@@ -122,7 +124,8 @@ func (h *handler) listClients(c *gin.Context) {
// @Tags user // @Tags user
// @Produce json // @Produce json
// @Security BearerAuth // @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 page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10) // @Param limit query int false "Items per page" default(10)
// @Success 200 {object} appresult.AppSuccess // @Success 200 {object} appresult.AppSuccess
@@ -316,8 +319,10 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
filter.Limit = limit filter.Limit = limit
} }
if raw := strings.TrimSpace(c.Query("username")); raw != "" { if raw := strings.TrimSpace(c.Query("search")); raw != "" {
filter.Username = raw filter.Search = raw
} else if raw := strings.TrimSpace(c.Query("username")); raw != "" {
filter.Search = raw
} }
return filter, true return filter, true