Skip to content

Commit

Permalink
refactor: (#491) 닉네임 변경 주기 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
jeomxon committed Sep 1, 2023
1 parent d71539f commit e89e99b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.votogether.domain.member.entity.vo.Gender;
import com.votogether.domain.member.entity.vo.Nickname;
import com.votogether.domain.member.entity.vo.SocialType;
import com.votogether.domain.member.exception.MemberExceptionType;
import com.votogether.global.exception.BadRequestException;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
Expand All @@ -15,6 +17,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -78,9 +81,17 @@ public static Member from(final KakaoMemberResponse response) {
}

public void changeNickname(final String nickname) {
if (isNotPassedChangingCycle()) {
throw new BadRequestException(MemberExceptionType.NOT_PASSED_NICKNAME_CHANGING_CYCLE);
}
this.nickname = new Nickname(nickname);
}

private boolean isNotPassedChangingCycle() {
return (!this.getCreatedAt().equals(this.getUpdatedAt())) &&
(this.getUpdatedAt().isAfter(LocalDateTime.now().minusDays(14L)));
}

public void changeNicknameByReport() {
final String reportedNickname = "Pause1" + RandomStringUtils.random(9, true, true);
this.nickname = new Nickname(reportedNickname);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum MemberExceptionType implements ExceptionType {
INVALID_AGE(804, "존재할 수 없는 연령입니다."),
ALREADY_ASSIGNED_GENDER(805, "이미 성별이 할당되어 있습니다."),
ALREADY_ASSIGNED_BIRTH_YEAR(806, "이미 출생년도가 할당되어 있습니다."),
NOT_PASSED_NICKNAME_CHANGING_CYCLE(807, "최소 닉네임 변경주기가 지나지 않았습니다."),
;

private final int code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void register() {
class ChangeNickname {

@Test
@DisplayName("주어질 때 정상적으로 닉네임을 변경한다.")
@DisplayName("한번도 변경되지 않았다면 닉네임 변경 주기에 상관없이 닉네임을 변경한다.")
void changeNickname() {
// given
String newNickname = "jeomxon";
Expand Down Expand Up @@ -136,6 +136,20 @@ void changeNicknameEqualToPrevious() {
.hasMessage("이미 중복된 닉네임이 존재합니다.");
}

@Test
@DisplayName("최초 닉네임을 변경한 후 닉네임 변경 주기가 지나지 않았다면 예외가 발생한다.")
void changeNicknameThrowsExceptionNotPassedChangingCycle() {
// given
Member member = memberRepository.save(MemberFixtures.MALE_20.get());

memberService.changeNickname(member, "저문");

// when, then
assertThatThrownBy(() -> memberService.changeNickname(member, "저라니"))
.isInstanceOf(BadRequestException.class)
.hasMessage("최소 닉네임 변경주기가 지나지 않았습니다.");
}

}

@Nested
Expand Down

0 comments on commit e89e99b

Please sign in to comment.