diff --git a/action_create.go b/action_create.go index 3a59fd3..cc0cb4a 100644 --- a/action_create.go +++ b/action_create.go @@ -2,11 +2,8 @@ package usermngmt import ( "context" - "fmt" - "github.com/Selly-Modules/logger" "github.com/Selly-Modules/mongodb" - "go.mongodb.org/mongo-driver/bson/primitive" ) // CreateOptions ... @@ -16,14 +13,13 @@ type CreateOptions struct { Email string Password string Status string - RoleID primitive.ObjectID + RoleID string Other string } // Create ... func (s Service) Create(payload CreateOptions) error { var ( - col = s.getUserCollection() ctx = context.Background() ) @@ -34,19 +30,15 @@ func (s Service) Create(payload CreateOptions) error { } // New user data from payload - userData, err := payload.newUser() + doc, err := payload.newUser() if err != nil { return err } - // Create device - _, err = col.InsertOne(ctx, userData) + // Create user + err = s.userCreate(ctx, doc) if err != nil { - logger.Error("usermngmt - Create", logger.LogData{ - "doc": userData, - "err": err.Error(), - }) - return fmt.Errorf("error when create user: %s", err.Error()) + return err } return nil @@ -54,6 +46,7 @@ func (s Service) Create(payload CreateOptions) error { func (payload CreateOptions) newUser() (result User, err error) { timeNow := now() + roleID, _ := mongodb.NewIDFromString(payload.RoleID) return User{ ID: mongodb.NewObjectID(), Name: payload.Name, @@ -61,7 +54,7 @@ func (payload CreateOptions) newUser() (result User, err error) { Email: payload.Email, HashedPassword: hashPassword(payload.Password), Status: payload.Status, - RoleID: payload.RoleID, + RoleID: roleID, Other: payload.Other, CreatedAt: timeNow, UpdatedAt: timeNow, diff --git a/constant.go b/constant.go index 6b6e124..ab8a97d 100644 --- a/constant.go +++ b/constant.go @@ -2,8 +2,9 @@ package usermngmt // Constant ... const ( - tableUser = "users" - tableRole = "roles" + tableUser = "users" + tableRole = "roles" + tablePrefixDefault = "usermngmt" timezoneHCM = "Asia/Ho_Chi_Minh" diff --git a/db.go b/db.go index 08eb7b1..c5b0d5b 100644 --- a/db.go +++ b/db.go @@ -12,18 +12,12 @@ import ( // getUserCollection ... func (s Service) getUserCollection() *mongo.Collection { - if s.TablePrefix != "" { - return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser)) - } - return s.DB.Collection(tableUser) + return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser)) } // getRoleCollection ... func (s Service) getRoleCollection() *mongo.Collection { - if s.TablePrefix != "" { - s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole)) - } - return s.DB.Collection(tableRole) + return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole)) } func (s Service) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool { @@ -72,3 +66,20 @@ func (s Service) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) } return total != 0 } + +func (s Service) userCreate(ctx context.Context, doc User) error { + var ( + col = s.getUserCollection() + ) + + _, err := col.InsertOne(ctx, doc) + if err != nil { + logger.Error("usermngmt - Create", logger.LogData{ + "doc": doc, + "err": err.Error(), + }) + return fmt.Errorf("error when create user: %s", err.Error()) + } + + return nil +} diff --git a/usermngmt.go b/usermngmt.go index ef63c29..5979cff 100644 --- a/usermngmt.go +++ b/usermngmt.go @@ -35,6 +35,11 @@ func Init(config Config) (*Service, error) { return nil, errors.New("please provide all necessary information for init user") } + // If prefixTable is empty then it is usermngmt + if config.TablePrefix == "" { + config.TablePrefix = tablePrefixDefault + } + // Connect MongoDB db, err := mongodb.Connect( config.MongoDB.Host, diff --git a/validate.go b/validate.go index 13b45c4..71a2b8e 100644 --- a/validate.go +++ b/validate.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/Selly-Modules/logger" + "github.com/Selly-Modules/mongodb" ) func (co CreateOptions) validate(ctx context.Context) error { @@ -49,15 +50,19 @@ func (co CreateOptions) validate(ctx context.Context) error { } // RoleID - if co.RoleID.IsZero() { - logger.Error("usermngmt - Create: invalid roleID data", logger.LogData{ + if co.RoleID == "" { + logger.Error("usermngmt - Create: no roleID data", logger.LogData{ "payload": co, }) - return errors.New("invalid roleID data") + return errors.New("no role id data") } // Find roleID exists or not - if !s.isRoleIDExisted(ctx, co.RoleID) { + roleID, isValid := mongodb.NewIDFromString(co.RoleID) + if !isValid { + return errors.New("invalid role id data") + } + if !s.isRoleIDExisted(ctx, roleID) { return errors.New("role id does not exist") }