initial commit
This commit is contained in:
76
internal/application/auth/setup_profile.go
Normal file
76
internal/application/auth/setup_profile.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"base/internal/domain/profile"
|
||||
"base/internal/dto"
|
||||
)
|
||||
|
||||
var slugRe = regexp.MustCompile(`[^a-z0-9-]+`)
|
||||
|
||||
func (s *service) SetupProfile(ctx context.Context, userID uuid.UUID, req dto.SetupProfileRequest) error {
|
||||
existingProfile, _ := s.profileRepo.FindByUserID(ctx, userID)
|
||||
if existingProfile != nil {
|
||||
return ErrProfileAlreadyExists
|
||||
}
|
||||
|
||||
user, err := s.userRepo.FindByID(ctx, userID)
|
||||
if err != nil || user == nil {
|
||||
return ErrUserNotFound
|
||||
}
|
||||
|
||||
handle := generateHandle(user.FirstName, user.LastName, userID)
|
||||
if req.Handle != "" {
|
||||
handle = req.Handle
|
||||
}
|
||||
other, _ := s.profileRepo.FindByHandle(ctx, handle)
|
||||
if other != nil {
|
||||
return ErrHandleAlreadyTaken
|
||||
}
|
||||
|
||||
newProfile := &profile.Profile{
|
||||
ID: uuid.New(),
|
||||
UserID: &userID,
|
||||
Handle: handle,
|
||||
Hero: profile.Hero{
|
||||
FirstName: user.FirstName,
|
||||
LastName: user.LastName,
|
||||
ShortDescription: req.ShortDescription,
|
||||
},
|
||||
Contact: profile.Contact{
|
||||
Email: user.Email,
|
||||
Phone: user.PhoneNumber,
|
||||
},
|
||||
PageSetting: profile.PageSetting{
|
||||
VisibilityLevel: "public",
|
||||
},
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
newProfile.Hero.Role = &profile.Role{ID: req.RoleID}
|
||||
|
||||
if req.RoleLevel != "" && newProfile.Hero.Role != nil {
|
||||
newProfile.Hero.Role.Level = req.RoleLevel
|
||||
} else if req.RoleLevel != "" {
|
||||
newProfile.Hero.Role = &profile.Role{Level: req.RoleLevel}
|
||||
}
|
||||
|
||||
return s.profileRepo.Create(ctx, newProfile)
|
||||
}
|
||||
|
||||
func generateHandle(firstName, lastName string, userID uuid.UUID) string {
|
||||
slug := slugRe.ReplaceAllString(strings.ToLower(strings.TrimSpace(firstName+"-"+lastName)), "-")
|
||||
slug = strings.Trim(slug, "-")
|
||||
if slug == "" {
|
||||
slug = "user"
|
||||
}
|
||||
return fmt.Sprintf("%s-%s", slug, userID.String()[:8])
|
||||
}
|
||||
Reference in New Issue
Block a user