From ae971f15566c18cdf437cf5ce63dd7b162a65ea1 Mon Sep 17 00:00:00 2001 From: Hoang Date: Tue, 7 Dec 2021 14:28:52 +0700 Subject: [PATCH] add update response viError --- cache/role.go | 2 + config/config.go | 20 +++++++++ internal/error.go | 32 ++++++++++++++ model/query.go | 2 +- model/role_request.go | 5 ++- model/user_request.go | 34 +++++++++------ permission/handle.go | 8 ++-- role/handle.go | 8 ++-- user/db.go | 27 ------------ user/handle.go | 97 +++++++++++++++++++++++++------------------ usermngmt.go | 13 ++++++ 11 files changed, 157 insertions(+), 91 deletions(-) create mode 100644 config/config.go create mode 100644 internal/error.go diff --git a/cache/role.go b/cache/role.go index 7f08d5e..5e603e7 100644 --- a/cache/role.go +++ b/cache/role.go @@ -3,6 +3,7 @@ package cache import ( "context" "encoding/json" + "log" "sync" "github.com/Selly-Modules/logger" @@ -46,6 +47,7 @@ func Roles() { }) return } + log.Println("role", entry) }(value) } diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..e17739b --- /dev/null +++ b/config/config.go @@ -0,0 +1,20 @@ +package config + +type Configuration struct { + EmailIsUnique bool + PhoneNumberIsUnique bool +} + +var ( + c *Configuration +) + +// Set ... +func Set(instance *Configuration) { + c = instance +} + +// GetInstance ... +func GetInstance() *Configuration { + return c +} diff --git a/internal/error.go b/internal/error.go new file mode 100644 index 0000000..0798b4d --- /dev/null +++ b/internal/error.go @@ -0,0 +1,32 @@ +package internal + +const ( + // Invalid + + ErrorInvalidName = "tên không hợp lệ" + ErrorInvalidPhoneNumber = "số điện thoại không hợp lệ" + ErrorInvalidEmail = "email không hợp lệ" + ErrorInvalidPassword = "mật khẩu không hợp lệ" + ErrorInvalidStatus = "trạng thái không hợp lệ" + ErrorInvalidRole = "vai trò không hợp lệ" + ErrorInvalidOldPassword = "mật khẩu cũ không hợp lệ" + ErrorInvalidNewPassword = "mật khẩu mới không hợp lệ" + ErrorInvalidPermission = "quyền không hợp lệ" + ErrorInvalidUser = "người dùng không hợp lệ" + ErrorInvalidAvatar = "ảnh đại diện không hợp lệ" + + // NotFound + + ErrorNotFoundPermission = "quyền không tồn tại" + ErrorNotFoundRole = "vai trò không tồn tại" + ErrorNotFoundUser = "người dùng không tồn tại" + + // AlreadyExisted + + ErrorAlreadyExistedPhoneNumber = "số điện thoại đã tồn tại" + ErrorAlreadyExistedEmail = "email đã tồn tại" + + // Incorrect + + ErrorIncorrectPassword = "mật khẩu không chính xác" +) diff --git a/model/query.go b/model/query.go index 84413f3..e3256cb 100644 --- a/model/query.go +++ b/model/query.go @@ -72,7 +72,7 @@ func (q *CommonQuery) SetDefaultLimit() { // AssignOther ... func (q *CommonQuery) AssignOther(cond bson.M) { - // Query fields in other object + // Query fields in other object if len(q.Other) > 0 { for key, value := range q.Other { cond["other."+key] = value diff --git a/model/role_request.go b/model/role_request.go index 2aacc70..29d4ef2 100644 --- a/model/role_request.go +++ b/model/role_request.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/Selly-Modules/logger" + "github.com/Selly-Modules/usermngmt/internal" ) // RoleCreateOptions ... @@ -34,7 +35,7 @@ func (co RoleCreateOptions) Validate() error { logger.Error("usermngmt - Role - Create: no name data", logger.LogData{ "payload": co, }) - return errors.New("no name data") + return errors.New(internal.ErrorInvalidName) } return nil @@ -47,7 +48,7 @@ func (co RoleUpdateOptions) Validate() error { logger.Error("usermngmt - Role - Update: no name data", logger.LogData{ "payload": co, }) - return errors.New("no name data") + return errors.New(internal.ErrorInvalidName) } return nil diff --git a/model/user_request.go b/model/user_request.go index 883fd39..a99c314 100644 --- a/model/user_request.go +++ b/model/user_request.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/Selly-Modules/logger" + "github.com/Selly-Modules/usermngmt/internal" ) // UserCreateOptions ... @@ -58,7 +59,7 @@ func (co UserCreateOptions) Validate() error { logger.Error("usermngmt - User - Create: no name data", logger.LogData{ "payload": co, }) - return errors.New("no name data") + return errors.New(internal.ErrorInvalidName) } // Phone @@ -66,7 +67,7 @@ func (co UserCreateOptions) Validate() error { logger.Error("usermngmt - User - Create: no phone data", logger.LogData{ "payload": co, }) - return errors.New("no phone data") + return errors.New(internal.ErrorInvalidPhoneNumber) } // Email @@ -74,7 +75,7 @@ func (co UserCreateOptions) Validate() error { logger.Error("usermngmt - User - Create: no email data", logger.LogData{ "payload": co, }) - return errors.New("no email data") + return errors.New(internal.ErrorInvalidEmail) } // Password @@ -82,7 +83,7 @@ func (co UserCreateOptions) Validate() error { logger.Error("usermngmt - User - Create: no password data", logger.LogData{ "payload": co, }) - return errors.New("no password data") + return errors.New(internal.ErrorInvalidPassword) } // Status @@ -90,7 +91,7 @@ func (co UserCreateOptions) Validate() error { logger.Error("usermngmt - User - Create: no status data", logger.LogData{ "payload": co, }) - return errors.New("no status data") + return errors.New(internal.ErrorInvalidStatus) } // RoleID @@ -98,7 +99,7 @@ func (co UserCreateOptions) Validate() error { logger.Error("usermngmt - User - Create: no roleID data", logger.LogData{ "payload": co, }) - return errors.New("no role id data") + return errors.New(internal.ErrorInvalidRole) } return nil @@ -111,7 +112,7 @@ func (uo UserUpdateOptions) Validate() error { logger.Error("usermngmt - User - Update: no name data", logger.LogData{ "payload": uo, }) - return errors.New("no name data") + return errors.New(internal.ErrorInvalidName) } // Phone @@ -119,7 +120,7 @@ func (uo UserUpdateOptions) Validate() error { logger.Error("usermngmt - User - Update: no phone data", logger.LogData{ "payload": uo, }) - return errors.New("no phone data") + return errors.New(internal.ErrorInvalidPhoneNumber) } // Email @@ -127,7 +128,7 @@ func (uo UserUpdateOptions) Validate() error { logger.Error("usermngmt - User - Update: no email data", logger.LogData{ "payload": uo, }) - return errors.New("no email data") + return errors.New(internal.ErrorInvalidEmail) } // RoleID @@ -135,7 +136,7 @@ func (uo UserUpdateOptions) Validate() error { logger.Error("usermngmt - User - Update: no roleID data", logger.LogData{ "payload": uo, }) - return errors.New("no role id data") + return errors.New(internal.ErrorInvalidRole) } return nil @@ -144,11 +145,18 @@ func (uo UserUpdateOptions) Validate() error { // Validate ... func (co ChangePasswordOptions) Validate() error { // OldPassword, NewPassword - if co.OldPassword == "" || co.NewPassword == "" { - logger.Error("usermngmt - User - ChangePassword: old or new password cannot be empty", logger.LogData{ + if co.OldPassword == "" { + logger.Error("usermngmt - User - ChangePassword: no old password data", logger.LogData{ "payload": co, }) - return errors.New("old or new password cannot be empty") + return errors.New(internal.ErrorInvalidOldPassword) + } + + if co.NewPassword == "" { + logger.Error("usermngmt - User - ChangePassword: no new password data", logger.LogData{ + "payload": co, + }) + return errors.New(internal.ErrorInvalidNewPassword) } return nil diff --git a/permission/handle.go b/permission/handle.go index 662c1a2..d64c193 100644 --- a/permission/handle.go +++ b/permission/handle.go @@ -63,10 +63,10 @@ func Update(permissionID string, payload model.PermissionUpdateOptions) error { // Find permissionID exists or not id, isValid := mongodb.NewIDFromString(permissionID) if !isValid { - return errors.New("invalid permission id data") + return errors.New(internal.ErrorInvalidPermission) } if !isPermissionIDExisted(ctx, id) { - return errors.New("permission not found") + return errors.New(internal.ErrorNotFoundPermission) } // Setup condition @@ -103,10 +103,10 @@ func Delete(permissionID string) error { // Find permissionID exists or not id, isValid := mongodb.NewIDFromString(permissionID) if !isValid { - return errors.New("invalid permission id data") + return errors.New(internal.ErrorInvalidPermission) } if !isPermissionIDExisted(ctx, id) { - return errors.New("permission not found") + return errors.New(internal.ErrorNotFoundPermission) } // Delete diff --git a/role/handle.go b/role/handle.go index f9d06d3..098f510 100644 --- a/role/handle.go +++ b/role/handle.go @@ -62,12 +62,12 @@ func Update(roleID string, payload model.RoleUpdateOptions) error { // Validate roleID id, isValid := mongodb.NewIDFromString(roleID) if !isValid { - return errors.New("invalid role id data") + return errors.New(internal.ErrorInvalidRole) } // Find roleID exists or not if !isRoleIDExisted(ctx, id) { - return errors.New("role not found") + return errors.New(internal.ErrorNotFoundRole) } // Setup condition @@ -154,12 +154,12 @@ func FindRole(roleID string) (r model.Role, err error) { // Find role exists or not id, isValid := mongodb.NewIDFromString(roleID) if !isValid { - err = errors.New("invalid role id data") + err = errors.New(internal.ErrorInvalidRole) return } role, _ := findByID(ctx, id) if role.ID.IsZero() { - err = errors.New("role not found") + err = errors.New(internal.ErrorNotFoundRole) return } diff --git a/user/db.go b/user/db.go index a467222..0339f20 100644 --- a/user/db.go +++ b/user/db.go @@ -12,33 +12,6 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -func isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool { - var ( - col = database.GetUserCol() - ) - // Find - cond := bson.M{ - "deleted": false, - "$or": []bson.M{ - { - "phone": phone, - }, - { - "email": email, - }, - }, - } - total, err := col.CountDocuments(ctx, cond) - if err != nil { - logger.Error("usermngmt - User - CountDocuments", logger.LogData{ - "condition": cond, - "err": err.Error(), - }) - return true - } - return total != 0 -} - func isPhoneNumberExisted(ctx context.Context, phone string) bool { var ( col = database.GetUserCol() diff --git a/user/handle.go b/user/handle.go index 88a2e5a..3dd5083 100644 --- a/user/handle.go +++ b/user/handle.go @@ -8,6 +8,7 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/usermngmt/cache" + "github.com/Selly-Modules/usermngmt/config" "github.com/Selly-Modules/usermngmt/internal" "github.com/Selly-Modules/usermngmt/model" "github.com/thoas/go-funk" @@ -29,18 +30,26 @@ func Create(payload model.UserCreateOptions) (result string, err error) { // Find roleID exists or not roleID, isValid := mongodb.NewIDFromString(payload.RoleID) if !isValid { - err = errors.New("invalid role id data") + err = errors.New(internal.ErrorInvalidRole) return } if !isRoleExisted(ctx, roleID) { - err = errors.New("role id does not exist") + err = errors.New(internal.ErrorNotFoundRole) return } // Find phone number,email exists or not - if isPhoneNumberOrEmailExisted(ctx, payload.Phone, payload.Email) { - err = errors.New("phone number or email already existed") - return + if config.GetInstance().PhoneNumberIsUnique { + if isPhoneNumberExisted(ctx, payload.Phone) { + err = errors.New(internal.ErrorAlreadyExistedPhoneNumber) + return + } + } + if config.GetInstance().EmailIsUnique { + if isEmailExisted(ctx, payload.Email) { + err = errors.New(internal.ErrorAlreadyExistedEmail) + return + } } // New user data from payload @@ -85,12 +94,12 @@ func FindUser(userID string) (r model.User, err error) { // Find user exists or not id, isValid := mongodb.NewIDFromString(userID) if !isValid { - err = errors.New("invalid user id data") + err = errors.New(internal.ErrorInvalidUser) return } user, _ := findByID(ctx, id) if user.ID.IsZero() { - err = errors.New("user not found") + err = errors.New(internal.ErrorNotFoundUser) return } @@ -106,12 +115,12 @@ func FindUserByEmail(email string) (r model.User, err error) { // Find user exists or not if email == "" { - err = errors.New("invalid email data") + err = errors.New(internal.ErrorInvalidEmail) return } user, _ := findOneByCondition(ctx, bson.M{"email": email}) if user.ID.IsZero() { - err = errors.New("user not found") + err = errors.New(internal.ErrorNotFoundUser) return } @@ -128,12 +137,12 @@ func GetHashedPassword(userID string) (result string, err error) { // Find user exists or not id, isValid := mongodb.NewIDFromString(userID) if !isValid { - err = errors.New("invalid email data") + err = errors.New(internal.ErrorInvalidUser) return } user, _ := findByID(ctx, id) if user.ID.IsZero() { - err = errors.New("user not found") + err = errors.New(internal.ErrorNotFoundUser) return } @@ -244,31 +253,35 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error { // Find roleID exists or not roleID, isValid := mongodb.NewIDFromString(payload.RoleID) if !isValid { - return errors.New("invalid role id data") + return errors.New(internal.ErrorInvalidRole) } if !isRoleExisted(ctx, roleID) { - return errors.New("role id does not exist") + return errors.New(internal.ErrorNotFoundRole) } // Find user exists or not id, isValid := mongodb.NewIDFromString(userID) if !isValid { - return errors.New("invalid user id data") + return errors.New(internal.ErrorInvalidUser) } user, _ := findByID(ctx, id) if user.ID.IsZero() { - return errors.New("user not found") + return errors.New(internal.ErrorNotFoundUser) } // Find phone number,email exists or not - if user.Phone != payload.Phone { - if isPhoneNumberExisted(ctx, payload.Phone) { - return errors.New("phone number already existed") + if config.GetInstance().PhoneNumberIsUnique { + if user.Phone != payload.Phone { + if isPhoneNumberExisted(ctx, payload.Phone) { + return errors.New(internal.ErrorAlreadyExistedPhoneNumber) + } } } - if user.Email != payload.Email { - if isEmailExisted(ctx, payload.Email) { - return errors.New("email already existed") + if config.GetInstance().EmailIsUnique { + if user.Email != payload.Email { + if isEmailExisted(ctx, payload.Email) { + return errors.New(internal.ErrorAlreadyExistedEmail) + } } } @@ -320,19 +333,19 @@ func ChangeUserPassword(userID string, opt model.ChangePasswordOptions) error { "payload": opt, "userID": userID, }) - return errors.New("invalid user id data") + return errors.New(internal.ErrorInvalidUser) } // Find user id, _ := mongodb.NewIDFromString(userID) user, _ := findByID(ctx, id) if user.ID.IsZero() { - return errors.New("user not found") + return errors.New(internal.ErrorNotFoundUser) } // Check old password if isValid := internal.CheckPasswordHash(opt.OldPassword, user.HashedPassword); !isValid { - return errors.New("the password is incorrect") + return errors.New(internal.ErrorIncorrectPassword) } // Update password @@ -357,19 +370,19 @@ func ResetUserPassword(userID string, password string) error { // Validate Password if password == "" { - return errors.New("password cannot be empty") + return errors.New(internal.ErrorInvalidPassword) } // Validate userID if _, isValid := mongodb.NewIDFromString(userID); !isValid { - return errors.New("invalid user id data") + return errors.New(internal.ErrorInvalidUser) } // Find user id, _ := mongodb.NewIDFromString(userID) user, _ := findByID(ctx, id) if user.ID.IsZero() { - return errors.New("user not found") + return errors.New(internal.ErrorNotFoundUser) } // Update password @@ -395,10 +408,10 @@ func ChangeUserStatus(userID, newStatus string) error { // Validate userID id, isValid := mongodb.NewIDFromString(userID) if !isValid { - return errors.New("invalid user id data") + return errors.New(internal.ErrorInvalidUser) } if user, _ := findByID(ctx, id); user.ID.IsZero() { - return errors.New("user not found") + return errors.New(internal.ErrorNotFoundUser) } // Update status @@ -423,10 +436,10 @@ func ChangeAllUsersStatus(roleID, status string) error { // Validate roleID id, isValid := mongodb.NewIDFromString(roleID) if !isValid { - return errors.New("invalid role id data") + return errors.New(internal.ErrorInvalidRole) } if !isRoleExisted(ctx, id) { - return errors.New("role not found") + return errors.New(internal.ErrorNotFoundRole) } // Setup condition @@ -457,8 +470,12 @@ func LoginWithEmailAndPassword(email, password string) (result model.User, err e ) // Validate email, password - if email == "" || password == "" { - err = errors.New("email or password cannot be empty") + if email == "" { + err = errors.New(internal.ErrorInvalidEmail) + return + } + if password == "" { + err = errors.New(internal.ErrorInvalidPassword) return } @@ -468,13 +485,13 @@ func LoginWithEmailAndPassword(email, password string) (result model.User, err e "deleted": false, }) if user.ID.IsZero() { - err = errors.New("user not found") + err = errors.New(internal.ErrorNotFoundUser) return } // Check Password if !internal.CheckPasswordHash(password, user.HashedPassword) { - err = errors.New("the password is incorrect") + err = errors.New(internal.ErrorIncorrectPassword) return } @@ -541,17 +558,17 @@ func UpdateAvatar(userID string, avatar interface{}) error { ) if avatar == nil { - return errors.New("no avatar data") + return errors.New(internal.ErrorInvalidAvatar) } // Find user exists or not id, isValid := mongodb.NewIDFromString(userID) if !isValid { - return errors.New("invalid role id data") + return errors.New(internal.ErrorInvalidUser) } user, _ := findByID(ctx, id) if user.ID.IsZero() { - return errors.New("user not found") + return errors.New(internal.ErrorNotFoundUser) } // Setup condition @@ -584,11 +601,11 @@ func Delete(userID string) error { // Find user exists or not id, isValid := mongodb.NewIDFromString(userID) if !isValid { - return errors.New("invalid role id data") + return errors.New(internal.ErrorInvalidUser) } user, _ := findByID(ctx, id) if user.ID.IsZero() { - return errors.New("user not found") + return errors.New(internal.ErrorNotFoundUser) } // Setup condition diff --git a/usermngmt.go b/usermngmt.go index 5c70e5e..c714d19 100644 --- a/usermngmt.go +++ b/usermngmt.go @@ -7,6 +7,7 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/mongodb" "github.com/Selly-Modules/usermngmt/cache" + configMoudle "github.com/Selly-Modules/usermngmt/config" "github.com/Selly-Modules/usermngmt/database" "github.com/Selly-Modules/usermngmt/internal" ) @@ -22,6 +23,12 @@ type Config struct { MongoDB MongoDBConfig // Table prefix, each service has its own prefix TablePrefix string + + // Email is unique + EmailIsUnique bool + + // phone number is unique + PhoneNumberIsUnique bool } // Service ... @@ -61,6 +68,12 @@ func Init(config Config) (*Service, error) { // Set database database.Set(db, config.TablePrefix) + // Set config module + configMoudle.Set(&configMoudle.Configuration{ + EmailIsUnique: config.EmailIsUnique, + PhoneNumberIsUnique: config.PhoneNumberIsUnique, + }) + // Init cache cache.Init()