Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

건강지표 마지막 생성일 조회 api 생성/ 닉네임 조건 수정 / 헬스커넥트 연동 여부 수정 #59

Merged
merged 12 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
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 @@ -21,6 +21,7 @@
import com.coniverse.dangjang.domain.auth.mapper.AuthMapper;
import com.coniverse.dangjang.domain.auth.repository.BlackTokenRepository;
import com.coniverse.dangjang.domain.auth.repository.RefreshTokenRepository;
import com.coniverse.dangjang.domain.healthmetric.enums.HealthConnect;
import com.coniverse.dangjang.domain.infrastructure.auth.client.OAuthClient;
import com.coniverse.dangjang.domain.infrastructure.auth.dto.OAuthInfoResponse;
import com.coniverse.dangjang.domain.notification.service.NotificationService;
Expand Down Expand Up @@ -82,8 +83,9 @@ public LoginResponse login(OauthLoginRequest params, String fcmToken) {
OAuthInfoResponse oAuthInfoResponse = request(params);
User user = userSearchService.findUserByOauthId(oAuthInfoResponse.getOauthId());
notificationService.saveFcmToken(fcmToken, user.getOauthId());
HealthConnect healthConnect = userSearchService.findInterlockHealthConnect(user.getOauthId());
user.verifyActiveUser();
return new LoginResponse(user.getNickname(), false, false);
return new LoginResponse(user.getNickname(), false, healthConnect.isConnecting());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.coniverse.dangjang.domain.healthmetric.dto.HealthMetricLastDateResponse;
import com.coniverse.dangjang.domain.healthmetric.dto.request.HealthMetricPatchRequest;
import com.coniverse.dangjang.domain.healthmetric.dto.request.HealthMetricPostRequest;
import com.coniverse.dangjang.domain.healthmetric.dto.response.HealthMetricChartResponse;
import com.coniverse.dangjang.domain.healthmetric.dto.response.HealthMetricResponse;
import com.coniverse.dangjang.domain.healthmetric.exception.SameTypeException;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricChartSearchService;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricRegisterService;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricSearchService;
import com.coniverse.dangjang.global.dto.SuccessSingleResponse;
import com.coniverse.dangjang.global.validator.ValidLocalDate;

Expand All @@ -41,6 +43,7 @@
public class HealthMetricController {
private final HealthMetricRegisterService healthMetricRegisterService;
private final HealthMetricChartSearchService healthMetricChartSearchService;
private final HealthMetricSearchService healthMetricSearchService;

/**
* HTTP POST METHOD
Expand Down Expand Up @@ -85,4 +88,19 @@ public ResponseEntity<SuccessSingleResponse<HealthMetricChartResponse>> getHealt
LocalDate.parse(endDate));
return ResponseEntity.ok().body(new SuccessSingleResponse<>(HttpStatus.OK.getReasonPhrase(), response));
}

/**
* 마지막 건강지표 생성일을 조회한다
*
* @param principal 유저 정보
* @return 유저의 마지막 건강지표 생성일
* @since 1.1.0
*/

@GetMapping("/last-date")
public ResponseEntity<SuccessSingleResponse<HealthMetricLastDateResponse>> getHealthMetricLastDate(@AuthenticationPrincipal User principal) {
String oauthId = principal.getUsername();
HealthMetricLastDateResponse response = healthMetricSearchService.findHealthMetricLastDate(oauthId);
return ResponseEntity.ok(new SuccessSingleResponse<>(HttpStatus.OK.getReasonPhrase(), response));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.coniverse.dangjang.domain.healthmetric.dto;

import java.time.LocalDate;

/**
* health metric last Date
*
* @author EVE
* @since 1.1.0
*/
public record HealthMetricLastDateResponse(LocalDate date) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.coniverse.dangjang.domain.healthmetric.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* health connect 연동여부 Enum
* <p>
Expand All @@ -8,8 +11,12 @@
* @author EVE
* @since 1.0.0
*/
@AllArgsConstructor
@Getter
public enum HealthConnect {
NEVER_CONNECTED,
CONNECTING,
DISCONNECTED
NEVER_CONNECTED(false),
CONNECTING(true),
DISCONNECTED(false);

boolean isConnecting;
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ public interface HealthMetricRepository extends JpaRepository<HealthMetric, Heal
*/
@Query("SELECT h FROM HealthMetric h WHERE h.healthMetricId.oauthId = ?1 AND h.groupCode = ?2 and h.healthMetricId.createdAt between ?3 and ?4")
List<HealthMetric> findLastWeekByGroupCodeAndCreatedAt(String oauthId, GroupCode groupCode, LocalDate startDate, LocalDate endDate);

/**
* 사용자의 health metrics 마지막 생성일 조회
*
* @param oauthId 사용자 ID
* @return CreatedAt
* @since 1.1.0
*/
@Query("SELECT h.healthMetricId.createdAt FROM HealthMetric h WHERE h.healthMetricId.oauthId = ?1 ORDER BY h.healthMetricId.createdAt DESC LIMIT 1")
Optional<LocalDate> findCreatedAtByOauthId(String oauthId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.coniverse.dangjang.domain.code.enums.CommonCode;
import com.coniverse.dangjang.domain.code.enums.GroupCode;
import com.coniverse.dangjang.domain.healthmetric.dto.HealthMetricLastDateResponse;
import com.coniverse.dangjang.domain.healthmetric.entity.HealthMetric;
import com.coniverse.dangjang.domain.healthmetric.exception.HealthMetricNotFoundException;
import com.coniverse.dangjang.domain.healthmetric.repository.HealthMetricRepository;
Expand Down Expand Up @@ -76,4 +77,17 @@ public List<HealthMetric> findWeeklyHealthMetricById(String oauthId, CommonCode
public List<HealthMetric> findWeeklyHealthMetricByGroupCode(String oauthId, GroupCode code, LocalDate startDate, LocalDate endDate) {
return healthMetricRepository.findLastWeekByGroupCodeAndCreatedAt(oauthId, code, startDate, endDate);
}

/**
* 유저의 마지막 건강 지표 생성일을 조회한다.
*
* @param oauthId 유저 아이디
* @return HealthMetricLastDateResponse 유저의 마지막 건강 지표 생성일
* @throws HealthMetricNotFoundException 유저의 건강 지표를 찾을 수 없을 경우 발생한다.
* @since 1.1.0
*/
public HealthMetricLastDateResponse findHealthMetricLastDate(String oauthId) {
LocalDate lastDate = healthMetricRepository.findCreatedAtByOauthId(oauthId).orElseThrow(HealthMetricNotFoundException::new);
return new HealthMetricLastDateResponse(lastDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class UserController { // TODO 전체 수정 (위치: signup, 이름: dup
*/
@GetMapping("/duplicateNickname")
public ResponseEntity<SuccessSingleResponse<DuplicateNicknameResponse>> checkDuplicateNickname(
@RequestParam @Pattern(regexp = "^[ㄱ-ㅎ가-힣a-zA-Z0-9]{1,8}$", message = "닉네임은 영어,한글,숫자 1~8글자 이내로 이루어져있어야 합니다.") @NotBlank(message = "닉네임은 1~8글자 이내여야 합니다.") String nickname) {
@RequestParam @Pattern(regexp = "^[가-힣ㄱ-ㅎㅏ-ㅣa-zA-Z0-9]{1,8}$", message = "닉네임은 영어,한글,숫자 1~8글자 이내로 이루어져있어야 합니다.") @NotBlank(message = "닉네임은 1~8글자 이내여야 합니다.") String nickname) {
DuplicateNicknameResponse duplicateNicknameResponse = userSignupService.checkDuplicatedNickname(nickname);
return ResponseEntity.ok().body(new SuccessSingleResponse<>(HttpStatus.OK.getReasonPhrase(), duplicateNicknameResponse));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public record SignUpRequest(@NotBlank(message = "oauth 접근 토큰은 필수로 입력해야 합니다.") String accessToken,
@Pattern(regexp = "^[a-z]*$", message = "provider는 소문자여야 합니다.") @NotBlank(message = "oauth provider는 필수로 입력해야 합니다.") String provider,
@Pattern(regexp = "^[ㄱ-ㅎ가-힣a-zA-Z0-9]{1,8}$", message = "닉네임은 영어,한글,숫자 1~8글자 이내로 이루어져있어야 합니다.") String nickname,
@Pattern(regexp = "^[가-힣ㄱ-ㅎㅏ-ㅣa-zA-Z0-9]{1,8}$", message = "닉네임은 영어,한글,숫자 1~8글자 이내로 이루어져있어야 합니다.") String nickname,
@NotNull(message = "성별은 필수로 입력해야 합니다.") Boolean gender,
@NotNull(message = "생년월일은 필수로 입력해야 합니다.") @Past(message = "생년월일은 현재보다 과거이어야 합니다.") LocalDate birthday,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.coniverse.dangjang.domain.healthmetric.enums.HealthConnect;
import com.coniverse.dangjang.domain.user.entity.User;

/**
Expand All @@ -29,4 +30,14 @@ public interface UserRepository extends JpaRepository<User, String> {
*/
@Query("SELECT u FROM users u left join fetch u.userPoint up where u.oauthId = :oauthId")
Optional<User> findJoinUserPoint(@Param("oauthId") String oauthId);

/**
* health connect 연동 여부 조회
*
* @param oauthId 사용자 pk
* @return HealthConnect 연동여부
* @since 1.1.0
*/
@Query("SELECT u.healthConnect FROM users u WHERE u.oauthId = :oauthId")
HealthConnect findHealthConnectByOauthId(String oauthId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coniverse.dangjang.domain.healthmetric.enums.HealthConnect;
import com.coniverse.dangjang.domain.user.entity.User;
import com.coniverse.dangjang.domain.user.exception.NonExistentUserException;
import com.coniverse.dangjang.domain.user.repository.UserRepository;
Expand Down Expand Up @@ -56,4 +57,14 @@ public Optional<User> findNickname(String nickname) {
public User findJoinUserPoint(String oauthId) {
return userRepository.findJoinUserPoint(oauthId).orElseThrow(NonExistentUserException::new);
}

/**
* 헬스커넥트 연동 여부를 조회한다.
*
* @param oauthId 사용자 PK
* @return boolean 헬스커넥트 연동 여부
*/
public HealthConnect findInterlockHealthConnect(String oauthId) {
return userRepository.findHealthConnectByOauthId(oauthId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public LoginResponse signUp(SignUpRequest signUpRequest, String fcmToken) {
registerWeight(savedUser, signUpRequest.weight());
pointService.addSignupPoint(savedUser.getOauthId());
notificationService.saveFcmToken(fcmToken, savedUser.getOauthId());
return authMapper.toLoginResponse(savedUser.getNickname(), false, false);
return authMapper.toLoginResponse(savedUser.getNickname(), false, savedUser.getHealthConnect().isConnecting());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ void tearDown() {
);
}

@Order(250)
@Test
void 헬스커넥트를_연동한_유저면_로그인시_healthConnect를_true로_반환한다() {
//given
User 이브 = userRepository.save(유저_이브());
이브.connectToHealthConnect();
entityManager.flush();
KakaoLoginRequest request = 카카오_로그인_요청();
int tokenCount = 0;
//when
LoginResponse response = oauthLoginService.login(request, fcmToken);

//then
assertAll(
() -> assertThat(response.nickname()).isEqualTo(이브.getNickname()),
() -> assertThat(response.dangjangClub()).isFalse(),
() -> assertThat(response.healthConnect()).isTrue()
);
}

@Order(300)
@Test
void 존재하는_사용자라면_auth토큰을_발급해준다() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HealthConnectControllerTest extends ControllerTest {
}

@Test
void 헬스_커넥를_연동하면_성공_메시지를_반환한다() throws Exception {
void 헬스_커넥트를_연동하면_성공_메시지를_반환한다() throws Exception {
// given
HealthConnectRegisterRequest request = 헬스_커넥트_연동_요청(true);
String content = objectMapper.writeValueAsString(request);
Expand All @@ -56,4 +56,5 @@ class HealthConnectControllerTest extends ControllerTest {
jsonPath("$.data").doesNotExist()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.coniverse.dangjang.domain.healthmetric.dto.HealthMetricLastDateResponse;
import com.coniverse.dangjang.domain.healthmetric.dto.request.HealthMetricPatchRequest;
import com.coniverse.dangjang.domain.healthmetric.dto.request.HealthMetricPostRequest;
import com.coniverse.dangjang.domain.healthmetric.dto.response.BloodSugarMinMax;
import com.coniverse.dangjang.domain.healthmetric.dto.response.HealthMetricChartData;
import com.coniverse.dangjang.domain.healthmetric.dto.response.HealthMetricResponse;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricChartSearchService;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricRegisterService;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricSearchService;
import com.coniverse.dangjang.support.ControllerTest;
import com.coniverse.dangjang.support.annotation.WithDangjangUser;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -50,6 +52,8 @@ class HealthMetricControllerTest extends ControllerTest {
private HealthMetricRegisterService healthMetricRegisterService;
@Autowired
private HealthMetricChartSearchService healthMetricChartSearchService;
@Autowired
private HealthMetricSearchService healthMetricSearchService;
public static LocalDate 시작_날짜 = LocalDate.parse("2023-12-31");
public static LocalDate 마지막_날짜 = LocalDate.parse("2024-01-06");
public static LocalDate 생성_날짜 = LocalDate.of(2023, 12, 31);
Expand Down Expand Up @@ -247,4 +251,24 @@ void setUp() throws JsonProcessingException {
jsonPath("$.errorCode").value(400)
);
}

@Order(700)
@Test
void 건강지표_마지막_생성일을_전달한다() throws Exception {
//given
String subUrl = URL + "/last-date";
LocalDate lastDate = LocalDate.now();
HealthMetricLastDateResponse response = new HealthMetricLastDateResponse(lastDate);
given(healthMetricSearchService.findHealthMetricLastDate(any())).willReturn(response);

//when
ResultActions resultActions = get(mockMvc, subUrl);

//then
resultActions.andExpectAll(
status().isOk(),
jsonPath("$.message").value("OK"),
jsonPath("$.data.date").value(response.date().toString())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.transaction.annotation.Transactional;

import com.coniverse.dangjang.domain.code.enums.CommonCode;
import com.coniverse.dangjang.domain.healthmetric.dto.HealthMetricLastDateResponse;
import com.coniverse.dangjang.domain.healthmetric.entity.HealthMetric;
import com.coniverse.dangjang.domain.healthmetric.exception.HealthMetricNotFoundException;
import com.coniverse.dangjang.domain.healthmetric.repository.HealthMetricRepository;
Expand All @@ -40,6 +41,7 @@ class HealthMetricSearchServiceTest {
@Autowired
private UserRepository userRepository;
private String oauthId;
private LocalDate 마지막_생성일 = 조회_날짜.plusDays(10);

@BeforeEach
void setUpUser() {
Expand All @@ -50,6 +52,7 @@ void setUpUser() {
.mapToObj(i -> 건강지표_엔티티(user, 조회_타입, 조회_날짜.plusDays(i)))
.toList()
);
healthMetricRepository.save(건강지표_엔티티(user, 조회_타입, 마지막_생성일));
}

@Test
Expand Down Expand Up @@ -93,7 +96,7 @@ void setUpUser() {
// then
assertAll(
() -> assertThat(조회된_건강지표.getType()).isEqualTo(조회_타입),
() -> assertThat(조회된_건강지표.getCreatedAt()).isEqualTo(조회_날짜.plusDays(9)),
() -> assertThat(조회된_건강지표.getCreatedAt()).isEqualTo(마지막_생성일),
() -> assertThat(조회된_건강지표.getOauthId()).isEqualTo(oauthId)
);
}
Expand Down Expand Up @@ -126,4 +129,14 @@ void setUpUser() {
&& 조회된_건강지표_리스트.get(i - 1).getCreatedAt().equals(조회_날짜.plusDays(i)))
);
}

@Test
void 건강지표_마지막_생성일을_조회한다() {
// when
HealthMetricLastDateResponse 조회된_마지막_생성일 = healthMetricSearchService.findHealthMetricLastDate(oauthId);

// then
assertThat(조회된_마지막_생성일.date()).isEqualTo(마지막_생성일);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
import com.coniverse.dangjang.domain.healthmetric.service.HealthConnectService;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricChartSearchService;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricRegisterService;
import com.coniverse.dangjang.domain.healthmetric.service.HealthMetricSearchService;
import com.coniverse.dangjang.domain.intro.controller.IntroController;
import com.coniverse.dangjang.domain.intro.service.IntroService;
import com.coniverse.dangjang.domain.log.controller.AppLogController;
import com.coniverse.dangjang.domain.log.service.LogService;
import com.coniverse.dangjang.domain.log.controller.AppLogController;
import com.coniverse.dangjang.domain.log.service.LogService;
import com.coniverse.dangjang.domain.notification.controller.NotificationController;
import com.coniverse.dangjang.domain.notification.service.NotificationService;
import com.coniverse.dangjang.domain.point.controller.PointController;
Expand Down Expand Up @@ -109,5 +108,7 @@ public class ControllerTest {
private UserWithdrawalService userWithdrawalService;
@MockBean
private NotificationService notificationService;
@MockBean
private HealthMetricSearchService healthMetricSearchService;

}
Loading