diff --git a/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java b/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java index d68c5b5d..88fe8f6c 100644 --- a/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java +++ b/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java @@ -123,7 +123,7 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId) .select(member.count()) .from(member) .where(member.familyId.eq(familyId) - .and(member.deletedAt.isNull())) + .and(isActiveMember())) .fetchFirst(); long survivalPostCount = queryFactory @@ -139,27 +139,32 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId) return survivalPostCount >= totalFamilyMembers / 2; } - public int calculateRemainingSurvivalPostCount(String familyId) { - Long familyMemberCount = queryFactory + @Override + public int countFamilyMembersByFamilyId(String familyId) { + Long count = queryFactory .select(member.id.count()) .from(member) .where(member.familyId.eq(familyId) - .and(member.deletedAt.isNull())) + .and(isActiveMember())) .fetchFirst(); + return count.intValue(); + } + @Override + public int countTodaySurvivalPostsByFamilyId(String familyId) { LocalDate today = ZonedDateTime.now().toLocalDate(); - Long todaySurvivalCount = queryFactory + Long count = queryFactory .select(post.id.count()) .from(post) .where(post.familyId.eq(familyId), post.type.eq(PostType.SURVIVAL), dateExpr(post.createdAt).eq(today)) .fetchFirst(); + return count.intValue(); + } - int requiredSurvivalPostCount = familyMemberCount != null ? (int) Math.floor(familyMemberCount / 2.0) : 0; - int todaySurvivalPostCount = todaySurvivalCount != null ? todaySurvivalCount.intValue() : 0; - - return Math.max(requiredSurvivalPostCount - todaySurvivalPostCount, 0); + private BooleanExpression isActiveMember() { + return member.deletedAt.isNull(); } private DateTimeTemplate dateExpr(DateTimePath localDateTime) { diff --git a/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java b/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java index 6506c52a..649b8ee2 100644 --- a/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java +++ b/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java @@ -154,26 +154,27 @@ void setup() { assertThat(exists).isFalse(); } - void 가족_구성원_수가_짝수인_경우_남은_생존게시글_업로드_수를_확인한다() { + @Test + void 해당_가족_구성원이_오늘_올린_생존신고_게시글_수를_조회한다() { // given String familyId = testMember1.getFamilyId(); // when - int remainingSurvivalPostCount = postRepositoryCustomImpl.calculateRemainingSurvivalPostCount(familyId); + int todaySurvivalPostCount = postRepositoryCustomImpl.countTodaySurvivalPostsByFamilyId(familyId); // then - assertThat(remainingSurvivalPostCount).isEqualTo(1); + assertThat(todaySurvivalPostCount).isEqualTo(0); } @Test - void 가족_구성원_수가_홀수인_경우_남은_생존게시글_업로드_수를_확인한다() { + void 가족_구성원_수를_조회한다() { // given - String familyId = testMember3.getFamilyId(); + String familyId = testMember1.getFamilyId(); // when - int remainingSurvivalPostCount = postRepositoryCustomImpl.calculateRemainingSurvivalPostCount(familyId); + int familyMemberCount = postRepositoryCustomImpl.countFamilyMembersByFamilyId(familyId); // then - assertThat(remainingSurvivalPostCount).isEqualTo(0); + assertThat(familyMemberCount).isEqualTo(2); } } diff --git a/post/src/main/java/com/oing/controller/PostController.java b/post/src/main/java/com/oing/controller/PostController.java index 839e405e..39969fe2 100644 --- a/post/src/main/java/com/oing/controller/PostController.java +++ b/post/src/main/java/com/oing/controller/PostController.java @@ -116,7 +116,7 @@ public MissionAvailableStatusResponse getMissionAvailableStatus(String memberId, public RemainingSurvivalPostCountResponse getRemainingSurvivalPostCount(String memberId, String loginMemberId, String loginFamilyId) { validateMemberId(loginMemberId, memberId); - int remainingSurvivalPostCount = postService.calculateRemainingSurvivalPostCount(loginFamilyId); + int remainingSurvivalPostCount = postService.calculateRemainingSurvivalPostCountUntilMissionUnlocked(loginFamilyId); return new RemainingSurvivalPostCountResponse(remainingSurvivalPostCount); } diff --git a/post/src/main/java/com/oing/repository/PostRepositoryCustom.java b/post/src/main/java/com/oing/repository/PostRepositoryCustom.java index 36377133..85de5c0a 100644 --- a/post/src/main/java/com/oing/repository/PostRepositoryCustom.java +++ b/post/src/main/java/com/oing/repository/PostRepositoryCustom.java @@ -23,5 +23,8 @@ QueryResults searchPosts(int page, int size, LocalDate date, String member boolean existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(String memberId, String familyId, PostType type, LocalDate postDate); boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId); - int calculateRemainingSurvivalPostCount(String familyId); + + int countFamilyMembersByFamilyId(String familyId); + + int countTodaySurvivalPostsByFamilyId(String familyId); } diff --git a/post/src/main/java/com/oing/service/PostService.java b/post/src/main/java/com/oing/service/PostService.java index ec21da2b..27f00686 100644 --- a/post/src/main/java/com/oing/service/PostService.java +++ b/post/src/main/java/com/oing/service/PostService.java @@ -165,7 +165,11 @@ public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId) return postRepository.isCreatedSurvivalPostByMajority(date, familyId); } - public int calculateRemainingSurvivalPostCount(String familyId) { - return postRepository.calculateRemainingSurvivalPostCount(familyId); + public int calculateRemainingSurvivalPostCountUntilMissionUnlocked(String familyId) { + int familyMemberCount = postRepository.countFamilyMembersByFamilyId(familyId); + int requiredSurvivalPostCount = familyMemberCount / 2; + int todaySurvivalPostCount = postRepository.countTodaySurvivalPostsByFamilyId(familyId); + + return Math.max(requiredSurvivalPostCount - todaySurvivalPostCount, 0); } } diff --git a/post/src/test/java/com/oing/service/PostServiceTest.java b/post/src/test/java/com/oing/service/PostServiceTest.java new file mode 100644 index 00000000..39179aee --- /dev/null +++ b/post/src/test/java/com/oing/service/PostServiceTest.java @@ -0,0 +1,51 @@ +package com.oing.service; + +import com.oing.domain.Emoji; +import com.oing.domain.Post; +import com.oing.domain.PostType; +import com.oing.domain.Reaction; +import com.oing.dto.request.PostReactionRequest; +import com.oing.repository.PostRepository; +import com.oing.repository.ReactionRepository; +import com.oing.util.IdentityGenerator; +import com.oing.util.PreSignedUrlGenerator; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.context.ApplicationEventPublisher; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class PostServiceTest { + + @InjectMocks + private PostService postService; + + @Mock + private PostRepository postRepository; + @Mock + private ApplicationEventPublisher applicationEventPublisher; + @Mock + private PreSignedUrlGenerator preSignedUrlGenerator; + @Mock + private MissionBridge missionBridge; + @Mock + private IdentityGenerator identityGenerator; + + @Test + void 미션_키_획득까지_남은_생존신고_게시글_업로드_수_조회_테스트() { + //given + String familyId = "1"; + + //when + when(postRepository.countFamilyMembersByFamilyId(familyId)).thenReturn(1); + when(postRepository.countTodaySurvivalPostsByFamilyId(familyId)).thenReturn(0); + + //then + assertEquals(0, postService.calculateRemainingSurvivalPostCountUntilMissionUnlocked(familyId)); + } +}