Skip to content

Commit

Permalink
fix: 오늘 미션 인증한 사람 수 세는 로직 수정 (#94)
Browse files Browse the repository at this point in the history
* fix: 오늘 미션 인증한 사람 수 세는 로직 수정

* remove: 변경된 로직 테스트에서 삭제

* refactor: 컨트롤러 dto, 서비스 dto 분리

* refactor: TimeUtil 사용하도록 변경

* fix: 잘못 병합된 코드 수정
  • Loading branch information
kimyu0218 authored Nov 14, 2024
1 parent 58aede3 commit df44687
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.nexters.goalpanzi.application.auth.dto.request.AppleLoginCommand;
import com.nexters.goalpanzi.application.auth.dto.request.GoogleLoginCommand;
import com.nexters.goalpanzi.application.auth.dto.request.ReissueTokenCommand;
import com.nexters.goalpanzi.application.auth.dto.response.LoginResponse;
import com.nexters.goalpanzi.application.auth.dto.response.TokenResponse;
import com.nexters.goalpanzi.application.auth.google.GoogleIdentityToken;
Expand Down Expand Up @@ -73,14 +74,14 @@ public void logout(final Long memberId) {
refreshTokenRepository.delete(memberId.toString());
}

public TokenResponse reissueToken(final Long memberId, final String refreshToken) {
// TODO : 토큰 만료기간 이슈로 무조건 재발급
// if (memberId == 104) {
// validateRefreshToken(memberId, refreshToken);
public TokenResponse reissueToken(final ReissueTokenCommand command) {
// TODO : 토큰 만료기간 이슈로 무조건 재발급
// if (command.memberId() == 104) {
// validateRefreshToken(command.memberId(), command.refreshToken());
// }

Jwt jwt = jwtProvider.generateTokens(memberId.toString());
refreshTokenRepository.save(memberId.toString(), jwt.refreshToken(), jwt.refreshExpiresIn());
Jwt jwt = jwtProvider.generateTokens(command.memberId().toString());
refreshTokenRepository.save(command.memberId().toString(), jwt.refreshToken(), jwt.refreshExpiresIn());

return new TokenResponse(jwt.accessToken(), jwt.refreshToken());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nexters.goalpanzi.application.auth.dto.request;

public record ReissueTokenCommand(
Long memberId,
String refreshToken
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@
import com.nexters.goalpanzi.application.mission.dto.response.MissionBoardsResponse;
import com.nexters.goalpanzi.domain.member.Member;
import com.nexters.goalpanzi.domain.member.repository.MemberRepository;
import com.nexters.goalpanzi.domain.mission.MemberRanks;
import com.nexters.goalpanzi.domain.mission.Mission;
import com.nexters.goalpanzi.domain.mission.MissionMember;
import com.nexters.goalpanzi.domain.mission.MissionMembers;
import com.nexters.goalpanzi.domain.mission.*;
import com.nexters.goalpanzi.domain.mission.repository.MissionMemberRepository;
import com.nexters.goalpanzi.domain.mission.repository.MissionRepository;
import com.nexters.goalpanzi.domain.mission.repository.MissionVerificationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Transactional(readOnly = true)
@RequiredArgsConstructor
@Service
public class MissionBoardService {

private final MissionRepository missionRepository;
private final MissionMemberRepository missionMemberRepository;
private final MissionVerificationRepository missionVerificationRepository;
private final MemberRepository memberRepository;

@Transactional(readOnly = true)
public MissionBoardsResponse getBoard(final MissionBoardQuery query) {
Member member = memberRepository.getMember(query.memberId());
Mission mission = missionRepository.getMission(query.missionId());
Expand All @@ -45,7 +45,7 @@ public MissionBoardsResponse getBoard(final MissionBoardQuery query) {
}

return new MissionBoardsResponse(
missionMembers.getProgressCount(),
getProgressCount(query.missionId()),
MemberRanks.from(missionMembers.getMissionMembers()).getRankByMember(member).rank(),
boards);
}
Expand Down Expand Up @@ -88,4 +88,9 @@ private Map<Integer, List<Member>> initializeBoard(final Integer boardCount) {
.boxed()
.collect(Collectors.toMap(i -> i, i -> new ArrayList<>()));
}

private int getProgressCount(final Long missionId) {
List<MissionVerification> missionVerifications = missionVerificationRepository.findAllByMissionIdAndDate(missionId, LocalDate.now());
return missionVerifications.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ public LocalDateTime getMissionUploadEndDateTime() {
// 현재 시간이 미션 시작 예고 시간인지 검증
// 미션 시작 예고 시간 == 미션 시작 1시간 전
public boolean isReadyTime() {
LocalDateTime startTime = LocalDateTime.of(this.missionStartDate.toLocalDate(), LocalTime.parse(this.uploadStartTime));
LocalDateTime startTime = TimeUtil.combineDateAndTime(
missionStartDate, LocalTime.parse(uploadStartTime)
);
Duration duration = Duration.between(startTime, LocalDateTime.now());

return duration.isNegative() && duration.toHours() <= 1;
Expand All @@ -165,10 +167,10 @@ public boolean isReadyTime() {
// 1. 인증 시간이 오전인 경우, 09시에 푸시
// 2. 인증 시간이 오후이거나 종일인 경우, 15시에 푸시
public boolean isPushTime(final int hour) {
if (this.uploadStartTime.equals(TimeOfDay.MORNING.getStartTime()) && this.uploadEndTime.equals(TimeOfDay.MORNING.getEndTime())) {
if (uploadStartTime.equals(TimeOfDay.MORNING.getStartTime()) && uploadEndTime.equals(TimeOfDay.MORNING.getEndTime())) {
return hour == PushTime.MORNING.getHour();
}
if (this.uploadStartTime.equals(TimeOfDay.AFTERNOON.getStartTime()) && this.uploadEndTime.equals(TimeOfDay.AFTERNOON.getEndTime())) {
if (uploadStartTime.equals(TimeOfDay.AFTERNOON.getStartTime()) && uploadEndTime.equals(TimeOfDay.AFTERNOON.getEndTime())) {
return hour == PushTime.AFTERNOON.getHour();
}
return hour == PushTime.EVERYDAY.getHour();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.LocalDate;
import java.util.List;

@RequiredArgsConstructor
@Getter
public class MissionMembers {

private final List<MissionMember> missionMembers;

public Integer getProgressCount() {
return missionMembers.stream()
.filter(missionMember ->
missionMember.getUpdatedAt().toLocalDate().equals(LocalDate.now()))
.toList().size();
}

public List<MissionMember> getMissionMembersByBoardNumber(final Integer boardNumber) {
return missionMembers.stream()
.filter(missionMember -> missionMember.getVerificationCount().equals(boardNumber))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import com.nexters.goalpanzi.application.auth.AuthService;
import com.nexters.goalpanzi.application.auth.dto.request.AppleLoginCommand;
import com.nexters.goalpanzi.application.auth.dto.request.GoogleLoginCommand;
import com.nexters.goalpanzi.application.auth.dto.request.RefreshTokenCommand;
import com.nexters.goalpanzi.application.auth.dto.response.LoginResponse;
import com.nexters.goalpanzi.application.auth.dto.response.TokenResponse;
import com.nexters.goalpanzi.common.argumentresolver.LoginMemberId;
import com.nexters.goalpanzi.presentation.auth.dto.ReissueTokenRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -56,10 +55,10 @@ public ResponseEntity<Void> logout(
@Override
@PostMapping("/token:reissue")
public ResponseEntity<TokenResponse> reissueToken(
@RequestBody @Valid final RefreshTokenCommand refreshTokenCommand,
@RequestBody @Valid final ReissueTokenRequest request,
@LoginMemberId final Long memberId
) {
TokenResponse tokenResponse = authService.reissueToken(memberId, refreshTokenCommand.refreshToken());
TokenResponse tokenResponse = authService.reissueToken(request.toServiceDto(memberId));

return ResponseEntity.ok(tokenResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.nexters.goalpanzi.application.auth.dto.request.AppleLoginCommand;
import com.nexters.goalpanzi.application.auth.dto.request.GoogleLoginCommand;
import com.nexters.goalpanzi.application.auth.dto.request.RefreshTokenCommand;
import com.nexters.goalpanzi.application.auth.dto.response.LoginResponse;
import com.nexters.goalpanzi.application.auth.dto.response.TokenResponse;
import com.nexters.goalpanzi.common.argumentresolver.LoginMemberId;
import com.nexters.goalpanzi.presentation.auth.dto.ReissueTokenRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down Expand Up @@ -64,7 +64,7 @@ ResponseEntity<LoginResponse> loginGoogle(
})
@PostMapping("/token:reissue")
ResponseEntity<TokenResponse> reissueToken(
@RequestBody @Valid final RefreshTokenCommand refreshTokenCommand,
@RequestBody @Valid final ReissueTokenRequest request,
@Parameter(hidden = true) @LoginMemberId final Long memberId
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.nexters.goalpanzi.presentation.auth.dto;

import com.nexters.goalpanzi.application.auth.dto.request.ReissueTokenCommand;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;

public record ReissueTokenRequest(
@Schema(description = "refresh token", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty String refreshToken
) {

public ReissueTokenCommand toServiceDto(final Long memberId) {
return new ReissueTokenCommand(memberId, this.refreshToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Sort;

import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -66,16 +65,12 @@ void setUp() {
MissionMember missionMember3 = mock(MissionMember.class);
MissionBoardQuery query = new MissionBoardQuery(me.getId(), 1L, MissionBoardQuery.SortType.RANK, Sort.Direction.ASC);

LocalDateTime now = LocalDateTime.now();
when(missionMember1.getUpdatedAt()).thenReturn(now);
when(missionMember1.getVerificationCount()).thenReturn(1);
when(missionMember1.getMember()).thenReturn(members.get(0));

when(missionMember2.getUpdatedAt()).thenReturn(now);
when(missionMember2.getVerificationCount()).thenReturn(1);
when(missionMember2.getMember()).thenReturn(members.get(1));

when(missionMember3.getUpdatedAt()).thenReturn(now);
when(missionMember3.getVerificationCount()).thenReturn(2);
when(missionMember3.getMember()).thenReturn(members.get(2));

Expand All @@ -89,7 +84,6 @@ void setUp() {
assertAll(
() -> assertThat(response.missionBoards().size()).isEqualTo(mission.getBoardCount() + 1),
() -> assertThat(response.rank()).isEqualTo(2),
() -> assertThat(response.progressCount()).isEqualTo(3),

() -> assertThat(response.missionBoards().get(1).isMyPosition()).isTrue(),
() -> assertThat(response.missionBoards().get(1).missionBoardMembers().size()).isEqualTo(2),
Expand Down

0 comments on commit df44687

Please sign in to comment.