Skip to content

Commit

Permalink
[fix] 알림 설정 수정 API 문제 해결 (#202)
Browse files Browse the repository at this point in the history
* #187 fix: 기존 로컬 회원도 수정시 데이터가 없는 경우 새로 반영하도록 추가

* #201 test: 알림 설정 수정 테스트 코드 추가
  • Loading branch information
yonghwankim-dev authored Feb 7, 2024
1 parent fbe1c7f commit 0dd5efb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ApiResponse<Void> updateNotificationPreference(
@Valid @RequestBody MemberNotificationPreferenceRequest request) {
MemberNotificationPreferenceResponse response = preferenceService.updateNotificationPreference(memberId,
request);
log.info("회원 알림 설정 수정 처리 결과 : memberId={}, response={}", memberId, request);
log.info("회원 알림 설정 수정 처리 결과 : memberId={}, response={}", memberId, response);
return ApiResponse.success(MemberSuccessCode.OK_UPDATE_NOTIFICATION_PREFERENCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import org.springframework.transaction.annotation.Transactional;

import codesquad.fineants.domain.member.Member;
import codesquad.fineants.domain.member.MemberRepository;
import codesquad.fineants.domain.notification_preference.NotificationPreference;
import codesquad.fineants.domain.notification_preference.NotificationPreferenceRepository;
import codesquad.fineants.spring.api.errors.errorcode.MemberErrorCode;
import codesquad.fineants.spring.api.errors.errorcode.NotificationPreferenceErrorCode;
import codesquad.fineants.spring.api.errors.exception.NotFoundResourceException;
import codesquad.fineants.spring.api.member.request.MemberNotificationPreferenceRequest;
Expand All @@ -18,6 +20,7 @@
public class MemberNotificationPreferenceService {

private final NotificationPreferenceRepository notificationPreferenceRepository;
private final MemberRepository memberRepository;

@Transactional
public MemberNotificationPreferenceResponse registerDefaultNotificationPreference(Member member) {
Expand All @@ -31,14 +34,16 @@ public MemberNotificationPreferenceResponse registerDefaultNotificationPreferenc
public MemberNotificationPreferenceResponse updateNotificationPreference(
Long memberId,
MemberNotificationPreferenceRequest request) {
NotificationPreference preference = findNotificationPreference(memberId);
preference.changePreference(request.toEntity());
return MemberNotificationPreferenceResponse.from(preference);
}

private NotificationPreference findNotificationPreference(Long memberId) {
return notificationPreferenceRepository.findByMemberId(memberId)
notificationPreferenceRepository.findByMemberId(memberId)
.ifPresentOrElse(notificationPreference -> notificationPreference.changePreference(request.toEntity()),
() -> {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundResourceException(MemberErrorCode.NOT_FOUND_MEMBER));
notificationPreferenceRepository.save(NotificationPreference.defaultSetting(member));
});
NotificationPreference preference = notificationPreferenceRepository.findByMemberId(memberId)
.orElseThrow(() ->
new NotFoundResourceException(NotificationPreferenceErrorCode.NOT_FOUND_NOTIFICATION_PREFERENCE));
return MemberNotificationPreferenceResponse.from(preference);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ void updateNotificationPreference() {
);
}

@DisplayName("사용자가 계정 설정시 기존 설정이 없다면 새로 등록한다")
@Test
void updateNotificationPreference_whenNotExistPreference_thenRegisterPreference() {
// given
Member member = memberRepository.save(createMember());
MemberNotificationPreferenceRequest request = MemberNotificationPreferenceRequest.builder()
.browserNotify(false)
.targetGainNotify(true)
.maxLossNotify(true)
.targetPriceNotify(true)
.build();

// when
MemberNotificationPreferenceResponse response = service.updateNotificationPreference(member.getId(), request);

// then
NotificationPreference preference = repository.findByMemberId(member.getId()).orElseThrow();
assertAll(
() -> assertThat(response)
.extracting("browserNotify", "targetGainNotify", "maxLossNotify", "targetPriceNotify")
.containsExactly(false, true, true, true),
() -> assertThat(preference)
.extracting("browserNotify", "targetGainNotify", "maxLossNotify", "targetPriceNotify")
.containsExactly(false, true, true, true)
);
}

private Member createMember() {
return Member.builder()
.nickname("일개미1234")
Expand Down

0 comments on commit 0dd5efb

Please sign in to comment.