Include assigned branches on GET user by ID.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
6191b7536c
commit
9c07439089
2 files changed
+24
-22
No files matched your search
@@ -28,6 +28,21 @@ 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, role, active, created_at, updated_at`
|
const userReturningColumns = `id, email, name, first_name, last_name, username, phone, avatar, role, active, created_at, updated_at`
|
||||||
|
|
||||||
|
const userBranchesJSON = `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)`
|
||||||
|
|
||||||
func (r *repository) List(ctx context.Context, filter user.ListFilter) (user.ListUserDTO, error) {
|
func (r *repository) List(ctx context.Context, filter user.ListFilter) (user.ListUserDTO, error) {
|
||||||
fromClause, whereClause, args := buildFilteredListQuery(filter, listVisibleRoles...)
|
fromClause, whereClause, args := buildFilteredListQuery(filter, listVisibleRoles...)
|
||||||
return r.paginateUsers(ctx, filter, fromClause, whereClause, args)
|
return r.paginateUsers(ctx, filter, fromClause, whereClause, args)
|
||||||
@@ -47,22 +62,9 @@ func (r *repository) ListStaff(ctx context.Context, filter user.ListFilter) (use
|
|||||||
limitPlaceholder := len(args) + 1
|
limitPlaceholder := len(args) + 1
|
||||||
offsetPlaceholder := len(args) + 2
|
offsetPlaceholder := len(args) + 2
|
||||||
listQuery := fmt.Sprintf(
|
listQuery := fmt.Sprintf(
|
||||||
`SELECT %s, COALESCE((
|
`SELECT %s, %s
|
||||||
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`,
|
%s%s ORDER BY u.id LIMIT $%d OFFSET $%d`,
|
||||||
userColumns, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
userColumns, userBranchesJSON, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
||||||
)
|
)
|
||||||
|
|
||||||
rows, err := r.client.Query(ctx, listQuery, append(args, filter.Limit, offset)...)
|
rows, err := r.client.Query(ctx, listQuery, append(args, filter.Limit, offset)...)
|
||||||
@@ -186,8 +188,8 @@ func (r *repository) GetByRole(ctx context.Context, role string, filter user.Lis
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *repository) GetByID(ctx context.Context, id int64) (user.UserDTO, error) {
|
func (r *repository) GetByID(ctx context.Context, id int64) (user.UserDTO, error) {
|
||||||
query := fmt.Sprintf(`SELECT %s FROM users u WHERE u.id = $1`, userColumns)
|
query := fmt.Sprintf(`SELECT %s, %s FROM users u WHERE u.id = $1`, userColumns, userBranchesJSON)
|
||||||
item, err := scanUser(r.client.QueryRow(ctx, query, id))
|
item, err := scanStaffUser(r.client.QueryRow(ctx, query, id))
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
return user.UserDTO{}, err
|
return user.UserDTO{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
baseURL = "/users"
|
baseURL = "/users"
|
||||||
fileUploadURL = "/file"
|
fileUploadURL = "/file"
|
||||||
fileURL = "/file/:id"
|
fileURL = "/file/:id"
|
||||||
publicFileURL = "/file/:file_name"
|
publicFileURL = "/file/:file_name"
|
||||||
)
|
)
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
@@ -153,7 +153,7 @@ func (h *handler) listStaff(c *gin.Context) {
|
|||||||
|
|
||||||
// getByID godoc
|
// getByID godoc
|
||||||
// @Summary Get user by ID
|
// @Summary Get user by ID
|
||||||
// @Description Returns a single user
|
// @Description Returns a single user with assigned branches
|
||||||
// @Tags user
|
// @Tags user
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
|
|||||||
Reference in new issue
Block a user