Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import life.mosu.mosuserver.domain.user.repository.UserJpaRepository;
import life.mosu.mosuserver.global.exception.CustomRuntimeException;
import life.mosu.mosuserver.global.exception.ErrorCode;
import life.mosu.mosuserver.global.util.PhoneNumberUtil;
import life.mosu.mosuserver.presentation.user.dto.response.UserInfoResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -38,7 +39,8 @@ public void syncUserInfoFromProfile(UserJpaEntity user, ProfileJpaEntity profile
}

public Long saveOrGetUser(UserJpaEntity user) {
return userJpaRepository.findByPhoneNumber(user.getPhoneNumber())
return userJpaRepository.findByPhoneNumber(
PhoneNumberUtil.formatGuestPhoneNumber(user.getPhoneNumber()))
Comment on lines +42 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The current logic for saveOrGetUser is prone to causing DataIntegrityViolationException and subsequent application errors.

The method first attempts to find an existing user by a guest-formatted phone number (G prefix). If no guest user is found, it proceeds to save the new user object in the orElseGet block.

The issue arises if a non-guest user (e.g., with a U prefix) already exists with the same phone number. The initial lookup for the guest user will fail, and the code will attempt to save the new user, leading to a unique constraint violation on the phone_number column.

For example:

  1. A user with phone number U01012345678 exists in the database.
  2. saveOrGetUser is called with a new UserJpaEntity whose phone number is also U01012345678.
  3. The code will look up G01012345678 and won't find it.
  4. It will then try to save the new user with U01012345678, which will fail.

This logic is further complicated by the UserJpaEntity.getPhoneNumber() method, which strips the prefix from the phone number, making it difficult to perform a lookup for the user's actual stored phone number.

To fix this, the logic should be revised to check for any existing user with the same base phone number before attempting to save. A proper fix would likely require changes to UserJpaEntity to allow accessing the raw phone number without modification.

.map(UserJpaEntity::getId)
.orElseGet(() -> {
Long userId = userJpaRepository.save(user).getId();
Expand Down