usermngmt/validate.go

125 lines
2.7 KiB
Go
Raw Permalink Normal View History

2021-11-08 04:53:03 +00:00
package usermngmt
import (
2021-11-08 10:04:01 +00:00
"context"
2021-11-08 04:53:03 +00:00
"errors"
"github.com/Selly-Modules/logger"
2021-11-08 11:21:52 +00:00
"github.com/Selly-Modules/mongodb"
2021-11-08 04:53:03 +00:00
)
2021-11-08 10:04:01 +00:00
func (co CreateOptions) validate(ctx context.Context) error {
2021-11-08 04:53:03 +00:00
// Name
if co.Name == "" {
logger.Error("usermngmt - Create: no Name data", logger.LogData{
"payload": co,
})
return errors.New("no name data")
}
// Phone
if co.Phone == "" {
logger.Error("usermngmt - Create: no phone data", logger.LogData{
"payload": co,
})
return errors.New("no phone data")
}
// Email
if co.Email == "" {
logger.Error("usermngmt - Create: no email data", logger.LogData{
"payload": co,
})
return errors.New("no email data")
}
2021-11-08 10:04:01 +00:00
// Password
if co.Password == "" {
logger.Error("usermngmt - Create: no password data", logger.LogData{
2021-11-08 04:53:03 +00:00
"payload": co,
})
2021-11-08 10:04:01 +00:00
return errors.New("no password data")
2021-11-08 04:53:03 +00:00
}
// Status
if co.Status == "" {
logger.Error("usermngmt - Create: no status data", logger.LogData{
"payload": co,
})
return errors.New("no status data")
}
// RoleID
2021-11-08 11:21:52 +00:00
if co.RoleID == "" {
logger.Error("usermngmt - Create: no roleID data", logger.LogData{
2021-11-08 04:53:03 +00:00
"payload": co,
})
2021-11-08 11:21:52 +00:00
return errors.New("no role id data")
2021-11-08 04:53:03 +00:00
}
2021-11-08 10:04:01 +00:00
// Find roleID exists or not
2021-11-08 11:21:52 +00:00
roleID, isValid := mongodb.NewIDFromString(co.RoleID)
if !isValid {
return errors.New("invalid role id data")
}
if !s.isRoleIDExisted(ctx, roleID) {
2021-11-08 10:04:01 +00:00
return errors.New("role id does not exist")
}
// Find phone number,email exists or not
if s.isPhoneNumberOrEmailExisted(ctx, co.Phone, co.Email) {
return errors.New("phone number or email already existed")
}
2021-11-08 04:53:03 +00:00
return nil
}
2021-11-09 02:08:56 +00:00
func (co UpdateOptions) validate(ctx context.Context) error {
// Name
if co.Name == "" {
logger.Error("usermngmt - Update: no name data", logger.LogData{
"payload": co,
})
return errors.New("no name data")
}
// Phone
if co.Phone == "" {
logger.Error("usermngmt - Update: no phone data", logger.LogData{
"payload": co,
})
return errors.New("no phone data")
}
// Email
if co.Email == "" {
logger.Error("usermngmt - Update: no email data", logger.LogData{
"payload": co,
})
return errors.New("no email data")
}
// RoleID
if co.RoleID == "" {
logger.Error("usermngmt - Update: no roleID data", logger.LogData{
"payload": co,
})
return errors.New("no role id data")
}
// Find roleID exists or not
roleID, isValid := mongodb.NewIDFromString(co.RoleID)
if !isValid {
return errors.New("invalid role id data")
}
if !s.isRoleIDExisted(ctx, roleID) {
return errors.New("role id does not exist")
}
// Find phone number,email exists or not
if s.isPhoneNumberOrEmailExisted(ctx, co.Phone, co.Email) {
return errors.New("phone number or email already existed")
}
return nil
}