129 lines
3.8 KiB
Go
129 lines
3.8 KiB
Go
package user
|
|
|
|
import (
|
|
"Rezina-Backend/internal/domain"
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// defaultUnpaginatedUserLimit is used when PageSize is unset (e.g. bulk notification role targeting).
|
|
const defaultUnpaginatedUserLimit int64 = 100_000
|
|
|
|
func (s *Service) UpdateUserKnowledgeLevel(ctx context.Context, userID int64, knowledgeLevel string) error {
|
|
return s.userStore.UpdateUserKnowledgeLevel(ctx, userID, knowledgeLevel)
|
|
}
|
|
|
|
func (s *Service) CreateUser(
|
|
ctx context.Context,
|
|
req domain.CreateUserReq,
|
|
isCompany bool,
|
|
) (domain.User, error) {
|
|
|
|
// Hash the password
|
|
hashedPassword, err := hashPassword(req.Password)
|
|
if err != nil {
|
|
return domain.User{}, err
|
|
}
|
|
|
|
// Create the user
|
|
return s.userStore.CreateUserWithoutOtp(ctx, domain.User{
|
|
FirstName: req.FirstName,
|
|
LastName: req.LastName,
|
|
// UserName: req.UserName,
|
|
Email: req.Email,
|
|
PhoneNumber: req.PhoneNumber,
|
|
Password: hashedPassword,
|
|
Role: domain.Role(req.Role),
|
|
EmailVerified: true, // assuming auto-verified on creation
|
|
PhoneVerified: true,
|
|
Status: domain.UserStatusActive,
|
|
AgeGroup: req.AgeGroup,
|
|
EducationLevel: req.EducationLevel,
|
|
Country: req.Country,
|
|
Region: req.Region,
|
|
ProfileCompleted: false,
|
|
})
|
|
}
|
|
|
|
func (s *Service) DeleteUser(ctx context.Context, id int64) error {
|
|
// Delete User
|
|
return s.userStore.DeleteUser(ctx, id)
|
|
}
|
|
|
|
func (s *Service) RequestUserDeletion(ctx context.Context, userID int64, gracePeriod time.Duration) (time.Time, error) {
|
|
return s.userStore.RequestUserDeletion(ctx, userID, gracePeriod)
|
|
}
|
|
|
|
func (s *Service) CancelUserDeletion(ctx context.Context, userID int64) error {
|
|
return s.userStore.CancelUserDeletion(ctx, userID)
|
|
}
|
|
|
|
func (s *Service) PurgeDueUserDeletions(ctx context.Context, limit int32) (int64, error) {
|
|
return s.userStore.PurgeDueUserDeletions(ctx, limit)
|
|
}
|
|
|
|
func (s *Service) GetAllUsers(
|
|
ctx context.Context,
|
|
filter domain.UserFilter,
|
|
) ([]domain.User, int64, error) {
|
|
normalized := filter
|
|
if normalized.Role != "" {
|
|
normalized.Role = strings.ToUpper(strings.TrimSpace(normalized.Role))
|
|
}
|
|
if normalized.Status != "" {
|
|
normalized.Status = strings.ToUpper(strings.TrimSpace(normalized.Status))
|
|
}
|
|
if normalized.PlanCategory != "" {
|
|
normalized.PlanCategory = strings.ToUpper(strings.TrimSpace(normalized.PlanCategory))
|
|
}
|
|
if normalized.DevicePlatform != "" {
|
|
normalized.DevicePlatform = strings.ToLower(strings.TrimSpace(normalized.DevicePlatform))
|
|
}
|
|
|
|
if normalized.PageSize <= 0 {
|
|
normalized.PageSize = defaultUnpaginatedUserLimit
|
|
}
|
|
|
|
return s.userStore.GetAllUsers(ctx, normalized)
|
|
}
|
|
|
|
func (s *Service) GetUserSummary(ctx context.Context) (domain.UserSummary, error) {
|
|
return s.userStore.GetUserSummary(ctx)
|
|
}
|
|
|
|
func (s *Service) ListAccountDeletionRequests(ctx context.Context, filter domain.AccountDeletionRequestFilter) ([]domain.AccountDeletionRequest, int64, error) {
|
|
return s.userStore.ListAccountDeletionRequests(ctx, filter)
|
|
}
|
|
|
|
func (s *Service) UpdateUserStatus(ctx context.Context, req domain.UpdateUserStatusReq) error {
|
|
if err := s.userStore.UpdateUserStatus(ctx, req); err != nil {
|
|
return err
|
|
}
|
|
s.authStatusCache.invalidate(req.UserID)
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) BulkDeactivateUsersByRole(ctx context.Context, role string, excludeUserID int64) (int64, error) {
|
|
n, err := s.userStore.BulkDeactivateUsersByRole(ctx, role, excludeUserID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
s.authStatusCache.invalidateAll()
|
|
return n, nil
|
|
}
|
|
|
|
func (s *Service) BulkReactivateUsersByRole(ctx context.Context, role string, excludeUserID int64) (int64, error) {
|
|
n, err := s.userStore.BulkReactivateUsersByRole(ctx, role, excludeUserID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
s.authStatusCache.invalidateAll()
|
|
return n, nil
|
|
}
|
|
|
|
func (s *Service) GetUserById(ctx context.Context, id int64) (domain.User, error) {
|
|
|
|
return s.userStore.GetUserByID(ctx, id)
|
|
}
|