2021-11-08 04:53:03 +00:00
|
|
|
package usermngmt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/logger"
|
|
|
|
"github.com/Selly-Modules/mongodb"
|
2021-11-08 08:34:20 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2021-11-08 04:53:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateOptions ...
|
|
|
|
type CreateOptions struct {
|
2021-11-08 08:34:20 +00:00
|
|
|
Name string
|
|
|
|
Phone string
|
|
|
|
Email string
|
|
|
|
HashedPassword string
|
|
|
|
Status string
|
|
|
|
RoleID primitive.ObjectID
|
|
|
|
Other string
|
2021-11-08 04:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create ...
|
|
|
|
func (s Service) Create(payload CreateOptions) error {
|
|
|
|
var (
|
|
|
|
col = s.getUserCollection()
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Validate payload
|
|
|
|
err := payload.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// New user data from payload
|
|
|
|
userData, err := payload.newUser()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-08 08:34:20 +00:00
|
|
|
// 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")
|
2021-11-08 04:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create device
|
|
|
|
_, err = col.InsertOne(ctx, userData)
|
|
|
|
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 nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload CreateOptions) newUser() (result User, err error) {
|
|
|
|
timeNow := now()
|
|
|
|
return User{
|
2021-11-08 08:34:20 +00:00
|
|
|
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,
|
2021-11-08 04:53:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|