diff --git a/src/main/java/com/example/daemawiki/domain/auth/service/Signup.java b/src/main/java/com/example/daemawiki/domain/auth/service/Signup.java index 773641bf..f3e4a008 100644 --- a/src/main/java/com/example/daemawiki/domain/auth/service/Signup.java +++ b/src/main/java/com/example/daemawiki/domain/auth/service/Signup.java @@ -39,19 +39,27 @@ public Signup(UserRepository userRepository,AuthMailRepository authMailRepositor public Mono execute(SignupRequest request) { return userRepository.findByEmail(request.email()) .flatMap(user -> Mono.error(EmailAlreadyExistsException.EXCEPTION)) - .switchIfEmpty(Mono.defer(() -> authMailRepository.findByMail(request.email()) - .filter(verified -> verified) - .switchIfEmpty(Mono.error(UnVerifiedEmailException.EXCEPTION)) - .flatMap(verified -> Mono.fromSupplier(() -> passwordEncoder.encode(request.password())) - .subscribeOn(scheduler) - .flatMap(password -> createUser(request, password)) - .flatMap(user -> userRepository.save(user) - .flatMap(savedUser -> createDocumentByUser.execute(savedUser) - .doOnNext(document -> savedUser.setDocumentId(document.getId())) - .flatMap(document -> userRepository.save(savedUser)) - .then(authMailRepository.delete(savedUser.getEmail())))) - .onErrorMap(e -> ExecuteFailedException.EXCEPTION) - ))).then(); + .switchIfEmpty(Mono.defer(() -> checkAndCreateUser(request))) + .then(); + } + + private Mono checkAndCreateUser(SignupRequest request) { + return authMailRepository.findByMail(request.email()) + .filter(verified -> verified) + .switchIfEmpty(Mono.error(UnVerifiedEmailException.EXCEPTION)) + .flatMap(verified -> Mono.fromSupplier(() -> passwordEncoder.encode(request.password())) + .subscribeOn(scheduler) + .flatMap(password -> createUser(request, password)) + .flatMap(this::saveUserAndCreateDocument) + .then(authMailRepository.delete(request.email())) + .onErrorMap(e -> ExecuteFailedException.EXCEPTION)); + } + + private Mono saveUserAndCreateDocument(User user) { + return userRepository.save(user) + .flatMap(savedUser -> createDocumentByUser.execute(savedUser) + .doOnNext(document -> savedUser.setDocumentId(document.getId())) + .flatMap(document -> userRepository.save(savedUser))); } private Mono createUser(SignupRequest request, String password) {