usermngmt/model/user_request.go

166 lines
3.6 KiB
Go
Raw Permalink Normal View History

2021-11-10 02:54:49 +00:00
package model
2021-11-10 01:44:22 +00:00
import (
"errors"
"github.com/Selly-Modules/logger"
2021-12-07 07:28:52 +00:00
"github.com/Selly-Modules/usermngmt/internal"
2021-12-28 08:37:57 +00:00
"go.mongodb.org/mongo-driver/bson"
2021-11-10 01:44:22 +00:00
)
2021-11-10 04:01:39 +00:00
// UserCreateOptions ...
type UserCreateOptions struct {
2021-11-17 06:32:29 +00:00
Name string
Phone string
Email string
Password string
Status string
RoleID string
RequireToChangePassword bool
Other interface{}
2021-11-22 10:35:21 +00:00
Avatar interface{} // if not, pass default file object
2021-11-10 01:44:22 +00:00
}
2021-11-10 04:01:39 +00:00
// UserUpdateOptions ...
type UserUpdateOptions struct {
2021-11-10 01:44:22 +00:00
Name string
Phone string
Email string
RoleID string
2021-11-19 04:17:58 +00:00
Other map[string]interface{}
2021-11-10 01:44:22 +00:00
}
// ChangePasswordOptions ...
type ChangePasswordOptions struct {
OldPassword string
NewPassword string
}
2021-11-10 04:01:39 +00:00
// UserAllQuery ...
type UserAllQuery struct {
2021-11-10 01:44:22 +00:00
Page int64
Limit int64
Keyword string
RoleID string
Status string
2021-11-16 07:26:50 +00:00
Sort interface{}
2021-11-19 04:00:18 +00:00
Other map[string]interface{} // query fields in other object
2021-12-28 08:37:57 +00:00
Cond bson.M
2021-11-19 04:00:18 +00:00
}
// UserCountQuery ...
type UserCountQuery struct {
RoleID string
Other map[string]interface{} // query fields in other object
2021-11-10 01:44:22 +00:00
}
// Validate ...
2021-11-10 04:01:39 +00:00
func (co UserCreateOptions) Validate() error {
2021-11-10 01:44:22 +00:00
// Name
if co.Name == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Create: no name data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": co,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidName)
2021-11-10 01:44:22 +00:00
}
// Phone
if co.Phone == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Create: no phone data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": co,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidPhoneNumber)
2021-11-10 01:44:22 +00:00
}
// Email
if co.Email == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Create: no email data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": co,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidEmail)
2021-11-10 01:44:22 +00:00
}
// Password
if co.Password == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Create: no password data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": co,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidPassword)
2021-11-10 01:44:22 +00:00
}
// Status
if co.Status == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Create: no status data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": co,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidStatus)
2021-11-10 01:44:22 +00:00
}
// RoleID
if co.RoleID == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Create: no roleID data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": co,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidRole)
2021-11-10 01:44:22 +00:00
}
return nil
}
// Validate ...
2021-11-10 04:01:39 +00:00
func (uo UserUpdateOptions) Validate() error {
2021-11-10 01:44:22 +00:00
// Name
if uo.Name == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Update: no name data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": uo,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidName)
2021-11-10 01:44:22 +00:00
}
// Phone
if uo.Phone == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Update: no phone data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": uo,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidPhoneNumber)
2021-11-10 01:44:22 +00:00
}
// Email
if uo.Email == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Update: no email data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": uo,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidEmail)
2021-11-10 01:44:22 +00:00
}
// RoleID
if uo.RoleID == "" {
2021-11-10 07:50:43 +00:00
logger.Error("usermngmt - User - Update: no roleID data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": uo,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidRole)
2021-11-10 01:44:22 +00:00
}
return nil
}
// Validate ...
func (co ChangePasswordOptions) Validate() error {
// OldPassword, NewPassword
2021-12-07 07:28:52 +00:00
if co.OldPassword == "" {
logger.Error("usermngmt - User - ChangePassword: no old password data", logger.LogData{
2021-11-10 01:44:22 +00:00
"payload": co,
})
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidOldPassword)
}
if co.NewPassword == "" {
logger.Error("usermngmt - User - ChangePassword: no new password data", logger.LogData{
"payload": co,
})
return errors.New(internal.ErrorInvalidNewPassword)
2021-11-10 01:44:22 +00:00
}
return nil
}