2021-11-10 02:54:49 +00:00
|
|
|
package model
|
2021-11-10 01:44:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2022-10-10 03:46:33 +00:00
|
|
|
"git.selly.red/Selly-Modules/logger"
|
|
|
|
"git.selly.red/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
|
|
|
}
|
|
|
|
|
2022-03-25 07:16:52 +00:00
|
|
|
// UserByPermissionQuery ...
|
|
|
|
type UserByPermissionQuery struct {
|
|
|
|
Page int64
|
|
|
|
Limit int64
|
|
|
|
Keyword string
|
|
|
|
Status string
|
|
|
|
Permission string // permission code
|
|
|
|
Sort interface{}
|
|
|
|
Other map[string]interface{} // query fields in other object
|
|
|
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Create: no name data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": co,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Create: no phone data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": co,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Create: no email data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": co,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Create: no password data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": co,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Create: no status data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": co,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Create: no roleID data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": co,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Update: no name data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": uo,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Update: no phone data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": uo,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Update: no email data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": uo,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - Update: no roleID data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": uo,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - ChangePassword: no old password data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": co,
|
|
|
|
},
|
2021-11-10 01:44:22 +00:00
|
|
|
})
|
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{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - ChangePassword: no new password data",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": co,
|
|
|
|
},
|
2021-12-07 07:28:52 +00:00
|
|
|
})
|
|
|
|
return errors.New(internal.ErrorInvalidNewPassword)
|
2021-11-10 01:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-25 07:16:52 +00:00
|
|
|
|
|
|
|
// Validate ...
|
|
|
|
func (q UserByPermissionQuery) Validate() error {
|
|
|
|
// OldPassword, NewPassword
|
|
|
|
if q.Permission == "" {
|
|
|
|
logger.Error("usermngmt - User - GetUsersByPermission : invalid permission", logger.LogData{
|
2022-10-05 10:59:08 +00:00
|
|
|
Source: "usermngmt.Validate",
|
|
|
|
Message: "usermngmt - User - GetUsersByPermission : invalid permission",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"payload": q,
|
|
|
|
},
|
2022-03-25 07:16:52 +00:00
|
|
|
})
|
|
|
|
return errors.New(internal.ErrorInvalidPermission)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|