Skip to content

Commit

Permalink
�fix: 인증율 하락 버그 수정 (#233)
Browse files Browse the repository at this point in the history
* refactor: 코드 정리

* fix: 인증율 하락 수정
  • Loading branch information
ymkim97 authored Dec 3, 2023
1 parent eaaad58 commit dab0c58
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
41 changes: 28 additions & 13 deletions src/main/java/com/moabam/api/application/room/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static org.apache.commons.lang3.StringUtils.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -85,7 +87,7 @@ public RoomDetailsResponse getRoomDetails(Long memberId, Long roomId, LocalDate
roomId, dailyMemberCertifications, date, room.getRoomType());
List<LocalDate> certifiedDates = getCertifiedDatesBeforeWeek(roomId);
double completePercentage = calculateCompletePercentage(dailyMemberCertifications.size(),
room.getCurrentUserCount());
room, date);

return RoomMapper.toRoomDetailsResponse(memberId, room, managerNickname, routineResponses, certifiedDates,
todayCertificateRankResponses, completePercentage);
Expand All @@ -94,7 +96,7 @@ public RoomDetailsResponse getRoomDetails(Long memberId, Long roomId, LocalDate
public MyRoomsResponse getMyRooms(Long memberId) {
LocalDate today = clockHolder.date();
List<MyRoomResponse> myRoomResponses = new ArrayList<>();
List<Participant> participants = participantSearchRepository.findNotDeletedParticipantsByMemberId(memberId);
List<Participant> participants = participantSearchRepository.findNotDeletedAllByMemberId(memberId);

for (Participant participant : participants) {
Room room = participant.getRoom();
Expand All @@ -108,7 +110,7 @@ public MyRoomsResponse getMyRooms(Long memberId) {
}

public RoomsHistoryResponse getJoinHistory(Long memberId) {
List<Participant> participants = participantSearchRepository.findAllParticipantsByMemberId(memberId);
List<Participant> participants = participantSearchRepository.findAllByMemberId(memberId);
List<RoomHistoryResponse> roomHistoryResponses = participants.stream()
.map(participant -> {
if (participant.getRoom() == null) {
Expand All @@ -134,7 +136,7 @@ public ManageRoomResponse getRoomForModification(Long memberId, Long roomId) {

Room room = participant.getRoom();
List<RoutineResponse> routineResponses = getRoutineResponses(roomId);
List<Participant> participants = participantSearchRepository.findParticipantsByRoomId(roomId);
List<Participant> participants = participantSearchRepository.findAllByRoomId(roomId);
List<Long> memberIds = participants.stream()
.map(Participant::getMemberId)
.toList();
Expand All @@ -158,7 +160,6 @@ public GetAllRoomsResponse getAllRooms(@Nullable RoomType roomType, @Nullable Lo
return RoomMapper.toSearchAllRoomsResponse(hasNext, getAllRoomResponse);
}

// TODO: full-text search 로 바꾸면서 리팩토링 예정
public GetAllRoomsResponse searchRooms(String keyword, @Nullable RoomType roomType, @Nullable Long roomId) {
List<GetAllRoomResponse> getAllRoomResponse = new ArrayList<>();
List<Room> rooms = new ArrayList<>();
Expand Down Expand Up @@ -258,9 +259,8 @@ private List<RoutineResponse> getRoutineResponses(Long roomId) {
private List<TodayCertificateRankResponse> getTodayCertificateRankResponses(Long memberId, Long roomId,
List<DailyMemberCertification> dailyMemberCertifications, LocalDate date, RoomType roomType) {

List<TodayCertificateRankResponse> responses = new ArrayList<>();
List<Certification> certifications = certificationsSearchRepository.findCertifications(roomId, date);
List<Participant> participants = participantSearchRepository.findAllParticipantsByRoomId(roomId);
List<Participant> participants = participantSearchRepository.findAllWithDeletedByRoomId(roomId);
List<Member> members = memberService.getRoomMembers(participants.stream()
.map(Participant::getMemberId)
.distinct()
Expand All @@ -273,10 +273,14 @@ private List<TodayCertificateRankResponse> getTodayCertificateRankResponses(Long
.toList();
List<Inventory> inventories = inventorySearchRepository.findDefaultInventories(memberIds, roomType.name());

responses.addAll(completedMembers(dailyMemberCertifications, members, certifications, participants, date,
knocks, inventories));
responses.addAll(uncompletedMembers(dailyMemberCertifications, members, participants, date, knocks,
inventories));
List<TodayCertificateRankResponse> responses = new ArrayList<>(
completedMembers(dailyMemberCertifications, members, certifications, participants, date, knocks,
inventories));

if (clockHolder.date() == date) {
responses.addAll(uncompletedMembers(dailyMemberCertifications, members, participants, date, knocks,
inventories));
}

return responses;
}
Expand Down Expand Up @@ -392,8 +396,19 @@ private List<LocalDate> getCertifiedDatesBeforeWeek(Long roomId) {
.toList();
}

private double calculateCompletePercentage(int certifiedMembersCount, int currentsMembersCount) {
double completePercentage = ((double)certifiedMembersCount / currentsMembersCount) * 100;
private double calculateCompletePercentage(int certifiedMembersCount, Room room, LocalDate date) {
if (date != clockHolder.date()) {
return 0;
}

LocalDateTime now = clockHolder.times();
LocalTime targetTime = LocalTime.of(room.getCertifyTime(), 0);
LocalDateTime targetDateTime = LocalDateTime.of(now.toLocalDate(), targetTime);

List<Participant> participants = participantSearchRepository.findAllByRoomIdBeforeDate(room.getId(),
targetDateTime);

double completePercentage = ((double)certifiedMembersCount / participants.size()) * 100;

return Math.round(completePercentage * 100) / 100.0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.moabam.api.domain.room.Certification;
import com.moabam.api.domain.room.DailyMemberCertification;
import com.moabam.api.domain.room.DailyRoomCertification;
import com.moabam.api.domain.room.Routine;
import com.querydsl.jpa.impl.JPAQueryFactory;

import jakarta.persistence.LockModeType;
Expand All @@ -36,14 +35,6 @@ public List<Certification> findCertifications(Long roomId, LocalDate date) {
.fetch();
}

public List<Certification> findCertificationsByRoutines(List<Routine> routines) {
return jpaQueryFactory.selectFrom(certification)
.where(
certification.routine.in(routines)
)
.fetch();
}

public Optional<DailyMemberCertification> findDailyMemberCertification(Long memberId, Long roomId, LocalDate date) {
return Optional.ofNullable(jpaQueryFactory
.selectFrom(dailyMemberCertification)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.moabam.api.domain.room.QParticipant.*;
import static com.moabam.api.domain.room.QRoom.*;

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

Expand Down Expand Up @@ -34,7 +35,7 @@ public Optional<Participant> findOne(Long memberId, Long roomId) {
);
}

public List<Participant> findParticipantsByRoomId(Long roomId) {
public List<Participant> findAllByRoomId(Long roomId) {
return jpaQueryFactory
.selectFrom(participant)
.where(
Expand All @@ -44,7 +45,7 @@ public List<Participant> findParticipantsByRoomId(Long roomId) {
.fetch();
}

public List<Participant> findAllParticipantsByRoomId(Long roomId) {
public List<Participant> findAllWithDeletedByRoomId(Long roomId) {
return jpaQueryFactory
.selectFrom(participant)
.where(
Expand All @@ -53,7 +54,18 @@ public List<Participant> findAllParticipantsByRoomId(Long roomId) {
.fetch();
}

public List<Participant> findNotDeletedParticipantsByMemberId(Long memberId) {
public List<Participant> findAllByRoomIdBeforeDate(Long roomId, LocalDateTime date) {
return jpaQueryFactory
.selectFrom(participant)
.where(
participant.room.id.eq(roomId),
participant.createdAt.before(date),
participant.deletedAt.isNull()
)
.fetch();
}

public List<Participant> findNotDeletedAllByMemberId(Long memberId) {
return jpaQueryFactory
.selectFrom(participant)
.join(participant.room, room).fetchJoin()
Expand All @@ -64,7 +76,7 @@ public List<Participant> findNotDeletedParticipantsByMemberId(Long memberId) {
.fetch();
}

public List<Participant> findAllParticipantsByMemberId(Long memberId) {
public List<Participant> findAllByMemberId(Long memberId) {
return jpaQueryFactory
.selectFrom(participant)
.leftJoin(participant.room, room).fetchJoin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void enter_room_concurrency_test() throws InterruptedException {

countDownLatch.await();

List<Participant> actual = participantSearchRepository.findParticipantsByRoomId(room.getId());
List<Participant> actual = participantSearchRepository.findAllByRoomId(room.getId());
Member newMember1 = memberRepository.findById(newMembers.get(0).getId()).orElseThrow();
Member newMember2 = memberRepository.findById(newMembers.get(1).getId()).orElseThrow();
Member newMember3 = memberRepository.findById(newMembers.get(2).getId()).orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void get_my_rooms_success() {
Participant participant3 = RoomFixture.participant(room3, memberId);
List<Participant> participants = List.of(participant1, participant2, participant3);

given(participantSearchRepository.findNotDeletedParticipantsByMemberId(memberId)).willReturn(participants);
given(participantSearchRepository.findNotDeletedAllByMemberId(memberId)).willReturn(participants);
given(certificationService.existsMemberCertification(memberId, room1.getId(), today)).willReturn(true);
given(certificationService.existsMemberCertification(memberId, room2.getId(), today)).willReturn(false);
given(certificationService.existsMemberCertification(memberId, room3.getId(), today)).willReturn(true);
Expand Down Expand Up @@ -130,7 +130,7 @@ void get_my_join_history_success() {

when(participant3.getDeletedAt()).thenReturn(today);
when(participant3.getDeletedRoomTitle()).thenReturn("밤 - 첫 번째 방");
given(participantSearchRepository.findAllParticipantsByMemberId(memberId)).willReturn(participants);
given(participantSearchRepository.findAllByMemberId(memberId)).willReturn(participants);

// when
RoomsHistoryResponse response = searchService.getJoinHistory(memberId);
Expand Down

0 comments on commit dab0c58

Please sign in to comment.