Customer Completed
This commit is contained in:
@@ -4,8 +4,11 @@ import (
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"furtuna-be/internal/constants/role"
|
||||
"furtuna-be/internal/entity"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@@ -29,11 +32,29 @@ func (e *User) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func RandomEmail() string {
|
||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
const length = 8
|
||||
|
||||
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
randomString := make([]byte, length)
|
||||
|
||||
for i := range randomString {
|
||||
randomString[i] = charset[seededRand.Intn(len(charset))]
|
||||
}
|
||||
|
||||
return string(randomString) + "@example.com"
|
||||
}
|
||||
|
||||
func (u *User) ToEntity(ctx mycontext.Context, userType string) *entity.User {
|
||||
if !ctx.IsAdmin() {
|
||||
u.PartnerID = ctx.GetPartnerID()
|
||||
}
|
||||
|
||||
if u.Email == "" {
|
||||
u.Email = RandomEmail()
|
||||
}
|
||||
|
||||
return &entity.User{
|
||||
Name: u.Name,
|
||||
Email: strings.ToLower(u.Email),
|
||||
@@ -47,6 +68,24 @@ func (u *User) ToEntity(ctx mycontext.Context, userType string) *entity.User {
|
||||
}
|
||||
}
|
||||
|
||||
type Customer struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
PhoneNumber string `json:"phone_number" binding:"required"`
|
||||
RoleID int64 `json:"role_id" binding:"required"`
|
||||
SiteID *int64 `json:"site_id" binding:"required"`
|
||||
}
|
||||
|
||||
func (c *Customer) ToEntity(ctx mycontext.Context, userType string) entity.Customer {
|
||||
return entity.Customer{
|
||||
Name: c.Name,
|
||||
Email: c.Email,
|
||||
PhoneNumber: c.PhoneNumber,
|
||||
SiteID: c.SiteID,
|
||||
PartnerID: ctx.GetPartnerID(),
|
||||
}
|
||||
}
|
||||
|
||||
type UserParam struct {
|
||||
Search string `form:"search" json:"search" example:"admin,branch1"`
|
||||
Name string `form:"name" json:"name" example:"Admin 1"`
|
||||
|
||||
Reference in New Issue
Block a user