From cf6b991c99ccf7a1dbc70a77cbfad78a1633d02e Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Thu, 19 Dec 2024 18:17:33 +0800 Subject: [PATCH] fix: Optimize user delete --- internal/repo/user/user_backyard_repo.go | 21 +++++++++++++---- internal/service/user_admin/user_backyard.go | 24 +++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/internal/repo/user/user_backyard_repo.go b/internal/repo/user/user_backyard_repo.go index 62f7f789f..27779f7e0 100644 --- a/internal/repo/user/user_backyard_repo.go +++ b/internal/repo/user/user_backyard_repo.go @@ -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 } diff --git a/internal/service/user_admin/user_backyard.go b/internal/service/user_admin/user_backyard.go index e7f63d5f9..e663f930d 100644 --- a/internal/service/user_admin/user_backyard.go +++ b/internal/service/user_admin/user_backyard.go @@ -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 @@ -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) }