2021-11-08 04:53:03 +00:00
|
|
|
package usermngmt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/mongodb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateOptions ...
|
|
|
|
type CreateOptions struct {
|
2021-11-08 10:04:01 +00:00
|
|
|
Name string
|
|
|
|
Phone string
|
|
|
|
Email string
|
|
|
|
Password string
|
|
|
|
Status string
|
2021-11-08 11:21:52 +00:00
|
|
|
RoleID string
|
2021-11-08 10:04:01 +00:00
|
|
|
Other string
|
2021-11-08 04:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create ...
|
|
|
|
func (s Service) Create(payload CreateOptions) error {
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Validate payload
|
2021-11-08 10:04:01 +00:00
|
|
|
err := payload.validate(ctx)
|
2021-11-08 04:53:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// New user data from payload
|
2021-11-08 11:21:52 +00:00
|
|
|
doc, err := payload.newUser()
|
2021-11-08 04:53:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:21:52 +00:00
|
|
|
// Create user
|
|
|
|
err = s.userCreate(ctx, doc)
|
2021-11-08 04:53:03 +00:00
|
|
|
if err != nil {
|
2021-11-08 11:21:52 +00:00
|
|
|
return err
|
2021-11-08 04:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload CreateOptions) newUser() (result User, err error) {
|
|
|
|
timeNow := now()
|
2021-11-08 11:21:52 +00:00
|
|
|
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
|
2021-11-08 04:53:03 +00:00
|
|
|
return User{
|
2021-11-08 08:34:20 +00:00
|
|
|
ID: mongodb.NewObjectID(),
|
|
|
|
Name: payload.Name,
|
|
|
|
Phone: payload.Phone,
|
|
|
|
Email: payload.Email,
|
2021-11-08 10:04:01 +00:00
|
|
|
HashedPassword: hashPassword(payload.Password),
|
2021-11-08 08:34:20 +00:00
|
|
|
Status: payload.Status,
|
2021-11-08 11:21:52 +00:00
|
|
|
RoleID: roleID,
|
2021-11-08 08:34:20 +00:00
|
|
|
Other: payload.Other,
|
|
|
|
CreatedAt: timeNow,
|
|
|
|
UpdatedAt: timeNow,
|
2021-11-08 04:53:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|