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-09 03:54:14 +00:00
|
|
|
if err := payload.validate(ctx); err != nil {
|
2021-11-08 04:53:03 +00:00
|
|
|
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
|
2021-11-09 03:54:14 +00:00
|
|
|
if err = s.userCreate(ctx, doc); err != nil {
|
2021-11-08 11:21:52 +00:00
|
|
|
return err
|
2021-11-08 04:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-09 07:16:34 +00:00
|
|
|
func (payload CreateOptions) newUser() (result dbUser, err error) {
|
2021-11-08 04:53:03 +00:00
|
|
|
timeNow := now()
|
2021-11-08 11:21:52 +00:00
|
|
|
roleID, _ := mongodb.NewIDFromString(payload.RoleID)
|
2021-11-09 07:16:34 +00:00
|
|
|
return dbUser{
|
2021-11-08 08:34:20 +00:00
|
|
|
ID: mongodb.NewObjectID(),
|
|
|
|
Name: payload.Name,
|
2021-11-09 05:26:51 +00:00
|
|
|
SearchString: getSearchString(payload.Name, payload.Phone, payload.Email),
|
2021-11-08 08:34:20 +00:00
|
|
|
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
|
|
|
|
}
|