Skip to content

Commit

Permalink
fix: Optimize user delete
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Dec 19, 2024
1 parent f1f7542 commit cf6b991
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
21 changes: 17 additions & 4 deletions internal/repo/user/user_backyard_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,23 @@ func (ur *userAdminRepo) GetUserPage(ctx context.Context, page, pageSize int, us
}

// DeletePermanentlyUsers delete permanently users
func (ur *userAdminRepo) DeletePermanentlyUsers(ctx context.Context) (err error) {
_, err = ur.data.DB.Context(ctx).Where("deleted_at IS NOT NULL").Delete(&entity.User{})
func (ur *userAdminRepo) DeletePermanentlyUsers(ctx context.Context) ([]string, error) {
db := ur.data.DB.Context(ctx)

var userIDs []string
if err := db.Table("user").
Where("status = ?", entity.UserStatusDeleted).
Select("id").
Find(&userIDs); err != nil {
return nil, err
}

_, err := db.Where("status = ?", entity.UserStatusDeleted).Delete(&entity.User{})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
return nil, errors.InternalServer(reason.DatabaseError).
WithError(err).
WithStack()
}
return

return userIDs, nil
}
24 changes: 16 additions & 8 deletions internal/service/user_admin/user_backyard.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type UserAdminRepo interface {
AddUser(ctx context.Context, user *entity.User) (err error)
AddUsers(ctx context.Context, users []*entity.User) (err error)
UpdateUserPassword(ctx context.Context, userID string, password string) (err error)
DeletePermanentlyUsers(ctx context.Context) (err error)
DeletePermanentlyUsers(ctx context.Context) ([]string, error)
}

// UserAdminService user service
Expand Down Expand Up @@ -580,14 +580,22 @@ func (us *UserAdminService) SendUserActivation(ctx context.Context, req *schema.
return nil
}

func (us *UserAdminService) DeletePermanently(ctx context.Context, req *schema.DeletePermanentlyReq) (err error) {
if req.Type == constant.DeletePermanentlyUsers {
return us.userRepo.DeletePermanentlyUsers(ctx)
} else if req.Type == constant.DeletePermanentlyQuestions {
func (us *UserAdminService) DeletePermanently(ctx context.Context, req *schema.DeletePermanentlyReq) error {
switch req.Type {
case constant.DeletePermanentlyUsers:
userIDs, err := us.userRepo.DeletePermanentlyUsers(ctx)
if err != nil {
return err
}
for _, userID := range userIDs {
us.removeAllUserCreatedContent(ctx, userID)
}
return nil
case constant.DeletePermanentlyQuestions:
return us.questionCommonRepo.DeletePermanentlyQuestions(ctx)
} else if req.Type == constant.DeletePermanentlyAnswers {
case constant.DeletePermanentlyAnswers:
return us.answerCommonRepo.DeletePermanentlyAnswers(ctx)
default:
return errors.BadRequest(reason.RequestFormatError)
}

return errors.BadRequest(reason.RequestFormatError)
}

0 comments on commit cf6b991

Please sign in to comment.