From 6180e2ad72b782ee33753b242fe9f1111b3d7ec0 Mon Sep 17 00:00:00 2001 From: alessandrak Date: Fri, 23 Feb 2024 09:20:39 -0300 Subject: [PATCH 1/2] feat: add endpoint to update user name --- backend/internal/controller/http/v1/users.go | 35 +++++++++++++++++++ backend/internal/usecase/interfaces.go | 1 + backend/internal/usecase/mocks/mocks.go | 14 ++++++++ .../internal/usecase/repo/user_postgres.go | 10 ++++++ backend/internal/usecase/users.go | 7 ++++ 5 files changed, 67 insertions(+) diff --git a/backend/internal/controller/http/v1/users.go b/backend/internal/controller/http/v1/users.go index 7461f61..ff19c80 100644 --- a/backend/internal/controller/http/v1/users.go +++ b/backend/internal/controller/http/v1/users.go @@ -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) } } } @@ -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", + }) +} \ No newline at end of file diff --git a/backend/internal/usecase/interfaces.go b/backend/internal/usecase/interfaces.go index 5cf9fc1..58bf1d8 100644 --- a/backend/internal/usecase/interfaces.go +++ b/backend/internal/usecase/interfaces.go @@ -21,6 +21,7 @@ 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 } // User -. diff --git a/backend/internal/usecase/mocks/mocks.go b/backend/internal/usecase/mocks/mocks.go index 7385411..5902f39 100644 --- a/backend/internal/usecase/mocks/mocks.go +++ b/backend/internal/usecase/mocks/mocks.go @@ -140,6 +140,20 @@ func (mr *MockUserRepoMockRecorder) GetUserByToken(token interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserByToken", reflect.TypeOf((*MockUserRepo)(nil).GetUserByToken), token) } +// UpdateName mocks base method. +func (m *MockUserRepo) UpdateName(id, name string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateName", id, name) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateName indicates an expected call of UpdateName. +func (mr *MockUserRepoMockRecorder) UpdateName(id, name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateName", reflect.TypeOf((*MockUserRepo)(nil).UpdateName), id, name) +} + // UpdateToken mocks base method. func (m *MockUserRepo) UpdateToken(id, token string) error { m.ctrl.T.Helper() diff --git a/backend/internal/usecase/repo/user_postgres.go b/backend/internal/usecase/repo/user_postgres.go index 71d4eb3..8f6568c 100644 --- a/backend/internal/usecase/repo/user_postgres.go +++ b/backend/internal/usecase/repo/user_postgres.go @@ -170,4 +170,14 @@ 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 } \ No newline at end of file diff --git a/backend/internal/usecase/users.go b/backend/internal/usecase/users.go index 9561e54..80208c6 100644 --- a/backend/internal/usecase/users.go +++ b/backend/internal/usecase/users.go @@ -120,3 +120,10 @@ 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 +} \ No newline at end of file From 7b5bda98d17dd47fa9d138ca1bc1121e291c2ed9 Mon Sep 17 00:00:00 2001 From: alessandrak Date: Mon, 26 Feb 2024 11:36:25 -0300 Subject: [PATCH 2/2] fix: admin field on permissions --- .../controller/http/v1/role_permissions.go | 16 +++++++----- backend/internal/controller/http/v1/router.go | 2 +- backend/internal/usecase/interfaces.go | 2 ++ backend/internal/usecase/mocks/mocks.go | 17 +++++++++++- .../internal/usecase/repo/role_postgres.go | 3 +++ .../internal/usecase/repo/user_postgres.go | 26 ++++++++++++++++++- backend/internal/usecase/role.go | 14 +--------- backend/internal/usecase/users.go | 11 +++++++- 8 files changed, 67 insertions(+), 24 deletions(-) diff --git a/backend/internal/controller/http/v1/role_permissions.go b/backend/internal/controller/http/v1/role_permissions.go index e6011dc..bd1c270 100644 --- a/backend/internal/controller/http/v1/role_permissions.go +++ b/backend/internal/controller/http/v1/role_permissions.go @@ -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") { @@ -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) diff --git a/backend/internal/controller/http/v1/router.go b/backend/internal/controller/http/v1/router.go index e83ee5d..0597f68 100644 --- a/backend/internal/controller/http/v1/router.go +++ b/backend/internal/controller/http/v1/router.go @@ -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) diff --git a/backend/internal/usecase/interfaces.go b/backend/internal/usecase/interfaces.go index 58bf1d8..5b4c8b3 100644 --- a/backend/internal/usecase/interfaces.go +++ b/backend/internal/usecase/interfaces.go @@ -22,6 +22,7 @@ type ( GetProfile(token string) (entity.UserResponse, error) GetSuperAdminUsers() ([]entity.UserResponse, error) UpdateName(id string, name string) error + IsUserSuperAdmin(id string) (bool, error) } // User -. @@ -29,6 +30,7 @@ type ( Detail(email string) (entity.User, error) CreateUser(user entity.User) error Autentication(name string, password string) (User, error) + } // Wallet -. diff --git a/backend/internal/usecase/mocks/mocks.go b/backend/internal/usecase/mocks/mocks.go index 5902f39..8faf58b 100644 --- a/backend/internal/usecase/mocks/mocks.go +++ b/backend/internal/usecase/mocks/mocks.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: internal/usecase/interfaces.go +// Source: usecase/interfaces.go // Package mocks is a generated GoMock package. package mocks @@ -140,6 +140,21 @@ func (mr *MockUserRepoMockRecorder) GetUserByToken(token interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserByToken", reflect.TypeOf((*MockUserRepo)(nil).GetUserByToken), token) } +// IsUserSuperAdmin mocks base method. +func (m *MockUserRepo) IsUserSuperAdmin(id string) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsUserSuperAdmin", id) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// IsUserSuperAdmin indicates an expected call of IsUserSuperAdmin. +func (mr *MockUserRepoMockRecorder) IsUserSuperAdmin(id interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsUserSuperAdmin", reflect.TypeOf((*MockUserRepo)(nil).IsUserSuperAdmin), id) +} + // UpdateName mocks base method. func (m *MockUserRepo) UpdateName(id, name string) error { m.ctrl.T.Helper() diff --git a/backend/internal/usecase/repo/role_postgres.go b/backend/internal/usecase/repo/role_postgres.go index 112ca5c..49c7d45 100644 --- a/backend/internal/usecase/repo/role_postgres.go +++ b/backend/internal/usecase/repo/role_postgres.go @@ -129,3 +129,6 @@ func (r RoleRepo) GetSuperAdminRole() (entity.Role, error) { return role, nil } + + + diff --git a/backend/internal/usecase/repo/user_postgres.go b/backend/internal/usecase/repo/user_postgres.go index 8f6568c..2cd4860 100644 --- a/backend/internal/usecase/repo/user_postgres.go +++ b/backend/internal/usecase/repo/user_postgres.go @@ -1,6 +1,7 @@ package repo import ( + "database/sql" "errors" "fmt" @@ -180,4 +181,27 @@ func (r UserRepo) UpdateName(id string, name string) error { return fmt.Errorf("UserRepo - UpdateName - db.Exec: %w", err) } return nil -} \ No newline at end of file +} + +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 +} diff --git a/backend/internal/usecase/role.go b/backend/internal/usecase/role.go index 0b67f25..92dedce 100644 --- a/backend/internal/usecase/role.go +++ b/backend/internal/usecase/role.go @@ -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 -} +} \ No newline at end of file diff --git a/backend/internal/usecase/users.go b/backend/internal/usecase/users.go index 80208c6..e7afb34 100644 --- a/backend/internal/usecase/users.go +++ b/backend/internal/usecase/users.go @@ -126,4 +126,13 @@ func (uc *UserUseCase) UpdateName(userID string, name string) error { return err } return nil -} \ No newline at end of file +} + +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 +}