fix comment
This commit is contained in:
parent
bfb263ca50
commit
b06431b9b9
|
@ -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
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package usermngmt
|
|||
// Constant ...
|
||||
const (
|
||||
tableUser = "users"
|
||||
tableRole = "roles"
|
||||
|
||||
timezoneHCM = "Asia/Ho_Chi_Minh"
|
||||
)
|
||||
|
|
|
@ -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()
|
||||
}
|
41
helper.go
41
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()
|
||||
}
|
||||
|
|
30
model.go
30
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"`
|
||||
}
|
||||
|
|
14
validate.go
14
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
|
||||
|
|
Loading…
Reference in New Issue