From b06431b9b994c26e71e2c36b072ff6591088bb51 Mon Sep 17 00:00:00 2001 From: Hoang Date: Mon, 8 Nov 2021 15:34:20 +0700 Subject: [PATCH] fix comment --- action_create.go | 54 ++++++++++++++++++-------------------- constant.go | 1 + db.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ helper.go | 41 ----------------------------- model.go | 30 ++++++++++++++------- validate.go | 14 +++++----- 6 files changed, 122 insertions(+), 86 deletions(-) create mode 100644 db.go diff --git a/action_create.go b/action_create.go index 4f3e743..c8e068f 100644 --- a/action_create.go +++ b/action_create.go @@ -7,17 +7,18 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/mongodb" + "go.mongodb.org/mongo-driver/bson/primitive" ) // CreateOptions ... type CreateOptions struct { - Name string - Phone string - Email string - HashPassword string - Status string - RoleID string - Other string + Name string + Phone string + Email string + HashedPassword string + Status string + RoleID primitive.ObjectID + Other string } // Create ... @@ -39,9 +40,14 @@ func (s Service) Create(payload CreateOptions) error { return err } - // Find phone,email exists or not - if s.haveNameOrPhoneExisted(ctx, userData.Phone, userData.Email) { - return errors.New("have name or phone existed") + // Find roleID exists or not + if !s.isRoleIDAlreadyExisted(ctx, userData.RoleID) { + return errors.New("roleID does not exist") + } + + // Find phone number,email exists or not + if s.isPhoneNumberOrEmailAlreadyExisted(ctx, userData.Phone, userData.Email) { + return errors.New("phone number or email already existed") } // Create device @@ -59,24 +65,16 @@ func (s Service) Create(payload CreateOptions) error { func (payload CreateOptions) newUser() (result User, err error) { timeNow := now() - - // New RoleID from string - roleID, isValid := mongodb.NewIDFromString(payload.RoleID) - if !isValid { - err = errors.New("invalid roleID") - return - } - return User{ - ID: mongodb.NewObjectID(), - Name: payload.Name, - Phone: payload.Phone, - Email: payload.Email, - HashPassword: payload.HashPassword, - Status: payload.Status, - RoleID: roleID, - Other: payload.Other, - CreatedAt: timeNow, - UpdatedAt: timeNow, + ID: mongodb.NewObjectID(), + Name: payload.Name, + Phone: payload.Phone, + Email: payload.Email, + HashedPassword: payload.HashedPassword, + Status: payload.Status, + RoleID: payload.RoleID, + Other: payload.Other, + CreatedAt: timeNow, + UpdatedAt: timeNow, }, nil } diff --git a/constant.go b/constant.go index d9d6e28..452d4b2 100644 --- a/constant.go +++ b/constant.go @@ -3,6 +3,7 @@ package usermngmt // Constant ... const ( tableUser = "users" + tableRole = "roles" timezoneHCM = "Asia/Ho_Chi_Minh" ) diff --git a/db.go b/db.go new file mode 100644 index 0000000..3204684 --- /dev/null +++ b/db.go @@ -0,0 +1,68 @@ +package usermngmt + +import ( + "context" + "fmt" + + "github.com/Selly-Modules/logger" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" +) + +// getUserCollection ... +func (s Service) getUserCollection() *mongo.Collection { + return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser)) +} + +// getRoleCollection ... +func (s Service) getRoleCollection() *mongo.Collection { + return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole)) +} + +func (s Service) isPhoneNumberOrEmailAlreadyExisted(ctx context.Context, phone, email string) bool { + var ( + col = s.getUserCollection() + user = User{} + ) + + // Find + cond := bson.M{ + "$or": []bson.M{ + { + "phone": phone, + }, + { + "email": email, + }, + }, + } + if err := col.FindOne(ctx, cond).Decode(&user); err != nil { + logger.Error("usermngmt - findByCondition", logger.LogData{ + "condition": cond, + "err": err.Error(), + }) + return true + } + return !user.ID.IsZero() +} + +func (s Service) isRoleIDAlreadyExisted(ctx context.Context, roleID primitive.ObjectID) bool { + var ( + col = s.getRoleCollection() + role = Role{} + ) + + // Find + cond := bson.M{ + "_id": roleID, + } + if err := col.FindOne(ctx, cond).Decode(&role); err != nil { + logger.Error("usermngmt - findRoleByCondition", logger.LogData{ + "condition": cond, + "err": err.Error(), + }) + return false + } + return !role.ID.IsZero() +} diff --git a/helper.go b/helper.go index 396baca..3d4670c 100644 --- a/helper.go +++ b/helper.go @@ -1,42 +1 @@ package usermngmt - -import ( - "context" - "fmt" - - "github.com/Selly-Modules/logger" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" -) - -// getUserCollection ... -func (s Service) getUserCollection() *mongo.Collection { - return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser)) -} - -func (s Service) haveNameOrPhoneExisted(ctx context.Context, phone, email string) bool { - var ( - col = s.getUserCollection() - user = User{} - ) - - // Find - cond := bson.M{ - "$or": []bson.M{ - { - "phone": phone, - }, - { - "email": email, - }, - }, - } - if err := col.FindOne(ctx, cond).Decode(&user); err != nil { - logger.Error("usermngmt - findByCondition", logger.LogData{ - "condition": cond, - "err": err.Error(), - }) - return true - } - return !user.ID.IsZero() -} diff --git a/model.go b/model.go index da79edd..cba9c56 100644 --- a/model.go +++ b/model.go @@ -8,14 +8,24 @@ import ( // User ... type User struct { - ID primitive.ObjectID `bson:"_id" json:"_id"` - Name string `bson:"name" json:"name"` - Phone string `bson:"phone" json:"phone"` // unique - Email string `bson:"email" json:"email"` // unique - HashPassword string `bson:"hashPassword" json:"hashPassword"` - Status string `bson:"status" json:"status"` - RoleID primitive.ObjectID `bson:"roleID" json:"roleId"` - Other string `bson:"other" json:"other"` - CreatedAt time.Time `bson:"createdAt" json:"createdAt"` - UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` + ID primitive.ObjectID `bson:"_id" json:"_id"` + Name string `bson:"name" json:"name"` + Phone string `bson:"phone" json:"phone"` // unique + Email string `bson:"email" json:"email"` // unique + HashedPassword string `bson:"hashedPassword" json:"hashedPassword"` + Status string `bson:"status" json:"status"` + RoleID primitive.ObjectID `bson:"roleID" json:"roleId"` + Other string `bson:"other" json:"other"` + CreatedAt time.Time `bson:"createdAt" json:"createdAt"` + UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` +} + +// Role ... +type Role struct { + ID primitive.ObjectID `bson:"_id" json:"_id"` + Name string `bson:"name" json:"name"` + Code string `bson:"code" json:"code"` + IsAdmin bool `bson:"isAdmin" json:"isAdmin"` + CreatedAt time.Time `bson:"createdAt" json:"createdAt"` + UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` } diff --git a/validate.go b/validate.go index 6354fac..2d5d017 100644 --- a/validate.go +++ b/validate.go @@ -31,12 +31,12 @@ func (co CreateOptions) validate() error { return errors.New("no email data") } - // HashPassword - if co.HashPassword == "" { - logger.Error("usermngmt - Create: no hashPassword data", logger.LogData{ + // HashedPassword + if co.HashedPassword == "" { + logger.Error("usermngmt - Create: no hashedPassword data", logger.LogData{ "payload": co, }) - return errors.New("no hashPassword data") + return errors.New("no hashedPassword data") } // Status @@ -48,11 +48,11 @@ func (co CreateOptions) validate() error { } // RoleID - if co.RoleID == "" { - logger.Error("usermngmt - Create: no roleID data", logger.LogData{ + if co.RoleID.IsZero() { + logger.Error("usermngmt - Create: invalid roleID data", logger.LogData{ "payload": co, }) - return errors.New("no roleID data") + return errors.New("invalid roleID data") } return nil