Skip to content

Commit

Permalink
Merge pull request #486 from tukcomCD2024/fix/group_capsule_open-B-#483
Browse files Browse the repository at this point in the history
fix : 그룹 캡슐 개봉 로직 수정 #483
  • Loading branch information
seokho-1116 authored Jun 11, 2024
2 parents ed7fd18 + a517985 commit e5680e4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -120,26 +119,24 @@ public void open() {
this.isOpened = Boolean.TRUE;
}

public boolean isTimeCapsule() {
return dueDate != null;
}

public boolean canOpen() {
return dueDate == null || dueDate.isBefore(ZonedDateTimeSupplier.utc().get());
public boolean isNotTimeCapsule() {
return dueDate == null;
}

public boolean isAllGroupMemberOpened(Long memberId, Long capsuleId) {
if (groupCapsuleOpens.isEmpty()) {
throw new GroupCapsuleOpenNotFoundException();
}

return groupCapsuleOpens.stream()
.allMatch(groupCapsuleOpen -> {
if (groupCapsuleOpen.matched(capsuleId, memberId)) {
groupCapsuleOpen.open();
}
boolean isCapsuleOpened = true;
for (GroupCapsuleOpen groupCapsuleOpen : groupCapsuleOpens) {
if (groupCapsuleOpen.matched(capsuleId, memberId)) {
groupCapsuleOpen.open();
}

isCapsuleOpened = isCapsuleOpened && groupCapsuleOpen.getIsOpened();
}

return groupCapsuleOpen.getIsOpened();
});
return isCapsuleOpened;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleOpenStateResponse;

public record GroupCapsuleOpenStateDto(
CapsuleOpenStatus capsuleOpenStatus
CapsuleOpenStatus capsuleOpenStatus,
boolean isIndividuallyOpened
) {

public static GroupCapsuleOpenStateDto opened() {
return new GroupCapsuleOpenStateDto(CapsuleOpenStatus.OPEN);
return new GroupCapsuleOpenStateDto(CapsuleOpenStatus.OPEN, true);
}

public static GroupCapsuleOpenStateDto notOpened() {
return new GroupCapsuleOpenStateDto(CapsuleOpenStatus.NOT_OPEN);
public static GroupCapsuleOpenStateDto notOpened(boolean isIndividuallyOpened) {
return new GroupCapsuleOpenStateDto(CapsuleOpenStatus.NOT_OPEN, isIndividuallyOpened);
}

public GroupCapsuleOpenStateResponse toResponse() {
return new GroupCapsuleOpenStateResponse(
capsuleOpenStatus,
capsuleOpenStatus.getStatusMessage()
capsuleOpenStatus.getStatusMessage(),
isIndividuallyOpened
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package site.timecapsulearchive.core.domain.capsule.group_capsule.data.response;

import io.swagger.v3.oas.annotations.media.Schema;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.CapsuleOpenStatus;

@Schema(description = "그룹 캡슐 개봉 상태 응답")
public record GroupCapsuleOpenStateResponse(

@Schema(description = "캡슐 개봉 상태")
CapsuleOpenStatus capsuleOpenStatus,
String statusMessage

@Schema(description = "캡슐 개봉 상태 메시지")
String statusMessage,

@Schema(description = "현재 요청한 사용자의 개별적인 캡슐 개봉 상태")
boolean isIndividuallyOpened
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,18 @@ public GroupCapsuleOpenStateDto openGroupCapsule(final Long memberId, final Long
capsuleId)
.orElseThrow(CapsuleNotFondException::new);

if (!groupCapsule.canOpen()) {
return GroupCapsuleOpenStateDto.notOpened();
if (groupCapsule.isNotCapsuleOpened()) {
return GroupCapsuleOpenStateDto.notOpened(false);
}

if (!groupCapsule.isTimeCapsule()) {
if (groupCapsule.isNotTimeCapsule()) {
groupCapsule.open();
return GroupCapsuleOpenStateDto.opened();
}

boolean allGroupMemberOpened = groupCapsule.isAllGroupMemberOpened(memberId, capsuleId);
if (!allGroupMemberOpened) {
return GroupCapsuleOpenStateDto.notOpened();
return GroupCapsuleOpenStateDto.notOpened(true);
}

groupCapsule.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class GroupCapsuleServiceTest {
softly.assertThat(groupCapsule.get().getIsOpened()).isFalse();
softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus())
.isEqualTo(CapsuleOpenStatus.NOT_OPEN);
softly.assertThat(groupCapsuleOpenStateDto.isIndividuallyOpened()).isFalse();
});
}

Expand All @@ -257,6 +258,7 @@ class GroupCapsuleServiceTest {
softly.assertThat(groupCapsule.get().getIsOpened()).isTrue();
softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus())
.isEqualTo(CapsuleOpenStatus.OPEN);
softly.assertThat(groupCapsuleOpenStateDto.isIndividuallyOpened()).isTrue();
});
}

Expand All @@ -280,6 +282,7 @@ class GroupCapsuleServiceTest {
softly.assertThat(groupCapsule.get().getIsOpened()).isFalse();
softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus()).isEqualTo(
CapsuleOpenStatus.NOT_OPEN);
softly.assertThat(groupCapsuleOpenStateDto.isIndividuallyOpened()).isTrue();
});
}

Expand All @@ -303,6 +306,7 @@ class GroupCapsuleServiceTest {
softly.assertThat(groupCapsule.get().getIsOpened()).isFalse();
softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus()).isEqualTo(
CapsuleOpenStatus.NOT_OPEN);
softly.assertThat(groupCapsuleOpenStateDto.isIndividuallyOpened()).isTrue();
});
}

Expand Down Expand Up @@ -341,6 +345,7 @@ class GroupCapsuleServiceTest {
softly.assertThat(groupCapsule.get().getIsOpened()).isTrue();
softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus()).isEqualTo(
CapsuleOpenStatus.OPEN);
softly.assertThat(groupCapsuleOpenStateDto.isIndividuallyOpened()).isTrue();
});
}

Expand All @@ -364,6 +369,7 @@ class GroupCapsuleServiceTest {
softly.assertThat(groupCapsule.get().getIsOpened()).isTrue();
softly.assertThat(groupCapsuleOpenStateDto.capsuleOpenStatus()).isEqualTo(
CapsuleOpenStatus.OPEN);
softly.assertThat(groupCapsuleOpenStateDto.isIndividuallyOpened()).isTrue();
});
}

Expand Down

0 comments on commit e5680e4

Please sign in to comment.