Add forgot-password SMS reset, client/staff user lists, and regenerate Swagger.
Fold follow-up migrations into init, expose GET /users/clients and /users/staff, and refresh seed data. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -29,8 +29,73 @@ const userColumns = `u.id, u.email, u.name, u.first_name, u.last_name, u.usernam
|
||||
const userReturningColumns = `id, email, name, first_name, last_name, username, phone, avatar, social_media, light_mode, role, active, created_at, updated_at`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter user.ListFilter) (user.ListUserDTO, error) {
|
||||
fromClause, whereClause, args := buildListQuery(filter)
|
||||
fromClause, whereClause, args := buildFilteredListQuery(filter, listVisibleRoles...)
|
||||
return r.paginateUsers(ctx, filter, fromClause, whereClause, args)
|
||||
}
|
||||
|
||||
func (r *repository) ListStaff(ctx context.Context, filter user.ListFilter) (user.ListUserDTO, error) {
|
||||
fromClause := ` FROM users u `
|
||||
whereClause, args := buildStaffWhere(filter)
|
||||
|
||||
var count int64
|
||||
countQuery := `SELECT COUNT(*)` + fromClause + whereClause
|
||||
if err := r.client.QueryRow(ctx, countQuery, args...).Scan(&count); err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
}
|
||||
|
||||
offset := (filter.Page - 1) * filter.Limit
|
||||
limitPlaceholder := len(args) + 1
|
||||
offsetPlaceholder := len(args) + 2
|
||||
listQuery := fmt.Sprintf(
|
||||
`SELECT %s, COALESCE((
|
||||
SELECT json_agg(jsonb_build_object(
|
||||
'id', b.id,
|
||||
'uniqueCode', b.unique_code,
|
||||
'name', b.name
|
||||
) ORDER BY b.id)
|
||||
FROM (
|
||||
SELECT DISTINCT ON (b.id) b.id, b.unique_code, b.name
|
||||
FROM branch_user bu
|
||||
JOIN branches b ON b.id = bu.branch_id
|
||||
WHERE bu.user_id = u.id
|
||||
ORDER BY b.id
|
||||
) b
|
||||
), '[]'::json)
|
||||
%s%s ORDER BY u.id LIMIT $%d OFFSET $%d`,
|
||||
userColumns, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
||||
)
|
||||
|
||||
rows, err := r.client.Query(ctx, listQuery, append(args, filter.Limit, offset)...)
|
||||
if err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []user.UserDTO
|
||||
for rows.Next() {
|
||||
item, err := scanStaffUser(rows)
|
||||
if err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
}
|
||||
|
||||
return user.ListUserDTO{Count: count, Users: items}, nil
|
||||
}
|
||||
|
||||
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)))
|
||||
}
|
||||
return " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
func (r *repository) paginateUsers(ctx context.Context, filter user.ListFilter, fromClause, whereClause string, args []interface{}) (user.ListUserDTO, error) {
|
||||
var count int64
|
||||
if err := r.client.QueryRow(ctx, `SELECT COUNT(*) `+fromClause+whereClause, args...).Scan(&count); err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
@@ -65,11 +130,9 @@ func (r *repository) List(ctx context.Context, filter user.ListFilter) (user.Lis
|
||||
return user.ListUserDTO{Count: count, Users: items}, nil
|
||||
}
|
||||
|
||||
func buildListQuery(filter user.ListFilter) (fromClause, whereClause string, args []interface{}) {
|
||||
return buildFilteredListQuery(filter, "")
|
||||
}
|
||||
var listVisibleRoles = []string{"user"}
|
||||
|
||||
func buildFilteredListQuery(filter user.ListFilter, role string) (fromClause, whereClause string, args []interface{}) {
|
||||
func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause, whereClause string, args []interface{}) {
|
||||
fromClause = ` FROM users u `
|
||||
|
||||
var conditions []string
|
||||
@@ -78,10 +141,17 @@ func buildFilteredListQuery(filter user.ListFilter, role string) (fromClause, wh
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`u.username ILIKE $%d`, n))
|
||||
}
|
||||
if role != "" {
|
||||
args = append(args, role)
|
||||
if len(roles) == 1 {
|
||||
args = append(args, roles[0])
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`u.role = $%d`, n))
|
||||
} else if len(roles) > 0 {
|
||||
placeholders := make([]string, len(roles))
|
||||
for i, role := range roles {
|
||||
args = append(args, role)
|
||||
placeholders[i] = fmt.Sprintf("$%d", len(args))
|
||||
}
|
||||
conditions = append(conditions, fmt.Sprintf(`u.role IN (%s)`, strings.Join(placeholders, ", ")))
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
@@ -100,39 +170,7 @@ func likePattern(term string) string {
|
||||
|
||||
func (r *repository) GetByRole(ctx context.Context, role string, filter user.ListFilter) (user.ListUserDTO, error) {
|
||||
fromClause, whereClause, args := buildFilteredListQuery(filter, role)
|
||||
|
||||
var count int64
|
||||
if err := r.client.QueryRow(ctx, `SELECT COUNT(*) `+fromClause+whereClause, args...).Scan(&count); err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
}
|
||||
|
||||
offset := (filter.Page - 1) * filter.Limit
|
||||
limitPlaceholder := len(args) + 1
|
||||
offsetPlaceholder := len(args) + 2
|
||||
listQuery := fmt.Sprintf(
|
||||
`SELECT %s%s%s ORDER BY u.id LIMIT $%d OFFSET $%d `,
|
||||
userColumns, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
||||
)
|
||||
|
||||
rows, err := r.client.Query(ctx, listQuery, append(args, filter.Limit, offset)...)
|
||||
if err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []user.UserDTO
|
||||
for rows.Next() {
|
||||
item, err := scanUser(rows)
|
||||
if err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return user.ListUserDTO{}, err
|
||||
}
|
||||
|
||||
return user.ListUserDTO{Count: count, Users: items}, nil
|
||||
return r.paginateUsers(ctx, filter, fromClause, whereClause, args)
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (user.UserDTO, error) {
|
||||
@@ -222,3 +260,26 @@ func scanUser(row scannable) (user.UserDTO, error) {
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func scanStaffUser(row scannable) (user.UserDTO, error) {
|
||||
var item user.UserDTO
|
||||
var socialMedia []byte
|
||||
var branchesJSON []byte
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.Email, &item.Name, &item.FirstName, &item.LastName, &item.Username, &item.Phone, &item.Avatar,
|
||||
&socialMedia, &item.LightMode, &item.Role, &item.Active, &item.CreatedAt, &item.UpdatedAt, &branchesJSON,
|
||||
); err != nil {
|
||||
return user.UserDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(socialMedia, &item.SocialMedia); err != nil {
|
||||
return user.UserDTO{}, err
|
||||
}
|
||||
if len(branchesJSON) == 0 || string(branchesJSON) == "null" {
|
||||
item.Branches = []user.StaffBranchDTO{}
|
||||
return item, nil
|
||||
}
|
||||
if err := json.Unmarshal(branchesJSON, &item.Branches); err != nil {
|
||||
return user.UserDTO{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user