Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow super admin update user names #124

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions backend/internal/controller/http/v1/role_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
)

type rolePermissions struct {
rolePermissionUseCase usecase.RolePermissionUseCase
roleUseCase usecase.RoleUseCase
messengerController HTTPControllerMessenger
l *logger.Logger
rolePermissionUseCase usecase.RolePermissionUseCase
roleUseCase usecase.RoleUseCase
userUseCase usecase.UserUseCase
messengerController HTTPControllerMessenger
l *logger.Logger
}

func newRolePermissionsRoutes(handler *gin.RouterGroup, rolePermissionUseCase usecase.RolePermissionUseCase, roleUseCase usecase.RoleUseCase, messengerController HTTPControllerMessenger, l *logger.Logger) {
r := &rolePermissions{rolePermissionUseCase, roleUseCase, messengerController, l}
func newRolePermissionsRoutes(handler *gin.RouterGroup, rolePermissionUseCase usecase.RolePermissionUseCase, roleUseCase usecase.RoleUseCase, userUseCase usecase.UserUseCase, messengerController HTTPControllerMessenger, l *logger.Logger) {
r := &rolePermissions{rolePermissionUseCase, roleUseCase, userUseCase, messengerController, l}

h := handler.Group("/role-permissions")
{
Expand Down Expand Up @@ -58,7 +59,8 @@ func (r *rolePermissions) userPermissions(c *gin.Context) {
}

res.Permissions = rolePermissions
res.Admin, err = r.roleUseCase.IsUserSuperAdmin(token)

res.Admin, err = r.userUseCase.IsUserSuperAdmin(token)
if err != nil {
r.l.Error(err, "http - v1 - list user permissions - IsUserSuperAdmin")
errorResponse(c, http.StatusInternalServerError, "database problems", err)
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/controller/http/v1/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewRouter(
newWalletsRoutes(groupV1, walletUseCase, messengerController, authUseCase, logger)
newAssetsRoutes(groupV1, walletUseCase, assetUseCase, messengerController, authUseCase, logUc, rolePermissionUc, logger, profanityF)
newRoleRoutes(groupV1, roleUseCase, messengerController, logger, profanityF)
newRolePermissionsRoutes(groupV1, rolePermissionUc, roleUseCase, messengerController, logger)
newRolePermissionsRoutes(groupV1, rolePermissionUc, roleUseCase, userUseCase, messengerController, logger)
newVaultCategoryRoutes(groupV1, messengerController, authUseCase, vaultCategoryUc, logger, profanityF)
newVaultRoutes(groupV1, messengerController, authUseCase, vaultUc, vaultCategoryUc, walletUseCase, assetUseCase, logger, profanityF)
newContractRoutes(groupV1, messengerController, authUseCase, contractUc, vaultUc, assetUseCase, userUseCase, logger)
Expand Down
35 changes: 35 additions & 0 deletions backend/internal/controller/http/v1/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func newUserRoutes(handler *gin.RouterGroup, t usecase.UserUseCase, a usecase.Au
{
allowedRoute.GET("/approve-new-accounts", r.detail)
allowedRoute.POST("/edit-users-role", r.editUsersRole)
allowedRoute.PUT("/:id/update-name", r.updateName)
}
}
}
Expand Down Expand Up @@ -275,3 +276,37 @@ func (r *usersRoutes) getProfile(c *gin.Context) {
// @Router /users [get]
func (r *usersRoutes) forgetPassword(c *gin.Context) {
}



type updateNameRequest struct {
Name string `json:"name" binding:"required"`
}

// @Summary Update user name
// @Description Update user name
// @ID updateName
// @Tags user
// @Accept json
// @Produce json
// @Success 200 {object}
// @Failure 500 {object} response
// @Router /users/{id}/update-name [put]
func (r *usersRoutes) updateName(c *gin.Context) {
var req updateNameRequest
if err := c.ShouldBindJSON(&req); err != nil {
r.l.Error(err, "http - v1 - editUserRole - ShouldBindJSON")
errorResponse(c, http.StatusBadRequest, "invalid request body", err)
return
}
userID := c.Param("id")
if err := r.t.UpdateName(userID, req.Name); err != nil {
r.l.Error(err, "http - v1 - updateName - UpdateName")
errorResponse(c, http.StatusInternalServerError, "database problems", err)
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Name updated sucessfully",
})
}
3 changes: 3 additions & 0 deletions backend/internal/usecase/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ type (
EditUsersRole(id_user string, id_role string) error
GetProfile(token string) (entity.UserResponse, error)
GetSuperAdminUsers() ([]entity.UserResponse, error)
UpdateName(id string, name string) error
IsUserSuperAdmin(id string) (bool, error)
}

// User -.
User interface {
Detail(email string) (entity.User, error)
CreateUser(user entity.User) error
Autentication(name string, password string) (User, error)

}

// Wallet -.
Expand Down
31 changes: 30 additions & 1 deletion backend/internal/usecase/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/internal/usecase/repo/role_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,6 @@ func (r RoleRepo) GetSuperAdminRole() (entity.Role, error) {

return role, nil
}



36 changes: 35 additions & 1 deletion backend/internal/usecase/repo/user_postgres.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repo

import (
"database/sql"
"errors"
"fmt"

Expand Down Expand Up @@ -170,4 +171,37 @@ func (r UserRepo) GetSuperAdminUsers() ([]entity.UserResponse, error) {
}

return entities, nil
}
}

func (r UserRepo) UpdateName(id string, name string) error {
// insert into if not exists if exist replace
stmt := `UPDATE UserAccount SET name=$2 WHERE id = $1`
_, err := r.Db.Exec(stmt, id, name)
if err != nil {
return fmt.Errorf("UserRepo - UpdateName - db.Exec: %w", err)
}
return nil
}

func (r UserRepo) IsUserSuperAdmin(token string) (bool, error) {
query := `SELECT EXISTS (
SELECT *
FROM UserAccount u
JOIN role r ON u.role_id = r.id
WHERE r.admin = $1 AND u.token = $2
) AS is_super_admin;`

row := r.Db.QueryRow(query, 1, token)

var isSuperAdmin bool

err := row.Scan(&isSuperAdmin)
if err != nil {
if err == sql.ErrNoRows {
return false, fmt.Errorf("UserRepo - IsUserSuperAdmin - not found")
}
return false, fmt.Errorf("UserRepo - IsUserSuperAdmin - row.Scan: %w", err)
}

return isSuperAdmin, nil
}
14 changes: 1 addition & 13 deletions backend/internal/usecase/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,4 @@ func (useCase *RoleUseCase) GetSuperAdminRole() (entity.Role, error) {
return entity.Role{}, fmt.Errorf("RoleUseCase - GetSuperAdminRole - uc.repo.GetSuperAdminRole: %w", err)
}
return role, nil
}


func (useCase *RoleUseCase) IsUserSuperAdmin(token string) (bool, error) {
role, err := useCase.repo.GetSuperAdminRole()
if err != nil {
return false, fmt.Errorf("RoleUseCase - GetSuperAdminRole - uc.repo.GetSuperAdminRole: %w", err)
}
if (role == entity.Role{}) {
return false, nil
}
return true, nil
}
}
16 changes: 16 additions & 0 deletions backend/internal/usecase/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,19 @@ func (uc *UserUseCase) GetSuperAdminUsers() ([]entity.UserResponse, error) {
return users, nil
}

func (uc *UserUseCase) UpdateName(userID string, name string) error {
err := uc.repo.UpdateName(userID, name)
if err != nil {
return err
}
return nil
}

func (useCase *UserUseCase) IsUserSuperAdmin(token string) (bool, error) {
value, err := useCase.repo.IsUserSuperAdmin(token)
if err != nil {
return false, fmt.Errorf("UserUseCase - IsUserSuperAdmin - uc.repo.IsUserSuperAdmin: %w", err)
}

return value, nil
}
Loading