Skip to content

Commit

Permalink
refactor: 방 인증 기획 관련 수정 (#219)
Browse files Browse the repository at this point in the history
* refactor: 방 인증 시간 정각부터 10분까지로 수정

* refactor: 참여자 중 한명 이상이 인증 했을 시 방 시간 수정 못하게 변경

* test: 테스트 코드 작성
  • Loading branch information
ymkim97 authored Dec 2, 2023
1 parent b672371 commit bd3fb6b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public boolean existsRoomCertification(Long roomId, LocalDate date) {
return dailyRoomCertificationRepository.existsByRoomIdAndCertifiedAt(roomId, date);
}

public boolean existsAnyMemberCertification(Long roomId, LocalDate date) {
return dailyMemberCertificationRepository.existsByRoomIdAndCreatedAtBetween(roomId, date.atStartOfDay(),
date.atTime(LocalTime.MAX));
}

public Certification findCertification(Long certificationId) {
return certificationRepository.findById(certificationId)
.orElseThrow(() -> new NotFoundException(CERTIFICATION_NOT_FOUND));
Expand All @@ -112,14 +117,13 @@ private void validateCertifyTime(LocalDateTime now, int certifyTime) {
LocalTime targetTime = LocalTime.of(certifyTime, 0);
LocalDateTime targetDateTime = LocalDateTime.of(now.toLocalDate(), targetTime);

if (certifyTime == MIDNIGHT_HOUR && now.getHour() == ONE_HOUR_BEFORE_MIDNIGHT_HOUR) {
if (certifyTime == MIDNIGHT_HOUR && now.getHour() != MIDNIGHT_HOUR) {
targetDateTime = targetDateTime.plusDays(1);
}

LocalDateTime minusTenMinutes = targetDateTime.minusMinutes(10);
LocalDateTime plusTenMinutes = targetDateTime.plusMinutes(10);

if (now.isBefore(minusTenMinutes) || now.isAfter(plusTenMinutes)) {
if (now.isBefore(targetDateTime) || now.isAfter(plusTenMinutes)) {
throw new BadRequestException(INVALID_CERTIFY_TIME);
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/moabam/api/application/room/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.moabam.api.dto.room.CreateRoomRequest;
import com.moabam.api.dto.room.EnterRoomRequest;
import com.moabam.api.dto.room.ModifyRoomRequest;
import com.moabam.global.common.util.ClockHolder;
import com.moabam.global.error.exception.BadRequestException;
import com.moabam.global.error.exception.ForbiddenException;
import com.moabam.global.error.exception.NotFoundException;
Expand All @@ -42,7 +43,9 @@ public class RoomService {
private final RoutineRepository routineRepository;
private final ParticipantRepository participantRepository;
private final ParticipantSearchRepository participantSearchRepository;
private final CertificationService certificationService;
private final MemberService memberService;
private final ClockHolder clockHolder;

@Transactional
public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
Expand Down Expand Up @@ -73,8 +76,12 @@ public void modifyRoom(Long memberId, Long roomId, ModifyRoomRequest modifyRoomR
room.changeTitle(modifyRoomRequest.title());
room.changeAnnouncement(modifyRoomRequest.announcement());
room.changePassword(modifyRoomRequest.password());
room.changeCertifyTime(modifyRoomRequest.certifyTime());
room.changeMaxCount(modifyRoomRequest.maxUserCount());

if (room.getCertifyTime() != modifyRoomRequest.certifyTime()) {
validateChangeCertifyTime(roomId);
}
room.changeCertifyTime(modifyRoomRequest.certifyTime());
}

@Transactional
Expand Down Expand Up @@ -160,6 +167,12 @@ public Room findRoom(Long roomId) {
.orElseThrow(() -> new NotFoundException(ROOM_NOT_FOUND));
}

private void validateChangeCertifyTime(Long roomId) {
if (certificationService.existsAnyMemberCertification(roomId, clockHolder.date())) {
throw new BadRequestException(UNAVAILABLE_TO_CHANGE_CERTIFY_TIME);
}
}

private Participant getParticipant(Long memberId, Long roomId) {
return participantSearchRepository.findOne(memberId, roomId)
.orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface DailyMemberCertificationRepository extends JpaRepository<DailyM

boolean existsByMemberIdAndRoomIdAndCreatedAtBetween(Long memberId, Long roomId, LocalDateTime startTime,
LocalDateTime endTime);

boolean existsByRoomIdAndCreatedAtBetween(Long roomId, LocalDateTime startTime, LocalDateTime endTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class GlobalConstant {
public static final String CHARSET_UTF_8 = ";charset=UTF-8";
public static final String SPACE = " ";
public static final int MIDNIGHT_HOUR = 0;
public static final int ONE_HOUR_BEFORE_MIDNIGHT_HOUR = 23;
public static final int ONE_HOUR = 1;
public static final int HOURS_IN_A_DAY = 24;
public static final int NOT_COMPLETED_RANK = 500;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum ErrorMessage {
NEED_TO_EXIT_ALL_ROOMS("모든 방에서 나가야 회원 탈퇴가 가능합니다."),
PARTICIPANT_DEPORT_ERROR("방장은 자신을 추방할 수 없습니다."),
IMAGE_CONVERT_FAIL("이미지 변환을 실패했습니다."),
UNAVAILABLE_TO_CHANGE_CERTIFY_TIME("이미 한명 이상이 인증을 하면 인증 시간을 바꿀 수 없습니다."),

LOGIN_FAILED("로그인에 실패했습니다."),
LOGIN_FAILED_ADMIN_KEY("어드민키가 달라요"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void get_certified_member_info_success() {
imageUrls.add("https://image.moabam.com/certifications/20231108/1_asdfsdfxcv-4815vcx-asfd");
imageUrls.add("https://image.moabam.com/certifications/20231108/2_asdfsdfxcv-4815vcx-asfd");

given(clockHolder.times()).willReturn(LocalDateTime.now().withHour(9).withMinute(58));
given(clockHolder.times()).willReturn(LocalDateTime.now().withHour(10).withMinute(6));
given(clockHolder.date()).willReturn(today);
given(participantSearchRepository.findOne(memberId, roomId)).willReturn(Optional.of(participant));
given(memberService.findMember(memberId)).willReturn(member1);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/moabam/api/presentation/RoomControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,46 @@ void unauthorized_modify_room_fail() throws Exception {
.andDo(print());
}

@DisplayName("방 수정 실패 - 이미 한 참여자가 인증하고 방의 인증 시간을 바꾸려고 할때 예외 처리")
@WithMember(id = 1L)
@Test
void room_certify_time_modification_fail() throws Exception {
// given
Room room = Room.builder()
.title("처음 제목")
.password("1234")
.roomType(MORNING)
.certifyTime(9)
.maxUserCount(5)
.build();
room = roomRepository.save(room);

Member member2 = MemberFixture.member("12313123");
member2 = memberRepository.save(member2);

Participant participant1 = RoomFixture.participant(room, 1L);
participant1.enableManager();

Participant participant2 = RoomFixture.participant(room, member2.getId());

participantRepository.saveAll(List.of(participant1, participant2));

DailyMemberCertification dailyMemberCertification = RoomFixture.dailyMemberCertification(member2.getId(),
room.getId(), participant2);

dailyMemberCertificationRepository.save(dailyMemberCertification);

ModifyRoomRequest modifyRoomRequest = new ModifyRoomRequest("수정할 방임!", "방 공지", "1234", 10, 7);
String json = objectMapper.writeValueAsString(modifyRoomRequest);

// expected
mockMvc.perform(put("/rooms/" + room.getId())
.contentType(APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest())
.andDo(print());
}

@DisplayName("비밀번호 있는 방 참여 성공")
@WithMember(id = 1L)
@Test
Expand Down

0 comments on commit bd3fb6b

Please sign in to comment.