Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ SELECT COALESCE(SUM(earn_exp), 0)
JOIN battles b ON b.id IN (:battleIds)
AND ueh.fk_user_id = :userId
AND ueh.date >= (b.started_at AT TIME ZONE 'Asia/Seoul')::date
AND ueh.date < (b.end_at AT TIME ZONE 'Asia/Seoul')::date
AND ueh.date <= ((b.end_at - interval '1 microsecond') AT TIME ZONE 'Asia/Seoul')::date
GROUP BY b.id
""", nativeQuery = true)
List<Object[]> findAverageExpForBattles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.ZoneId;
import java.util.List;

@Service
Expand All @@ -29,7 +28,6 @@ public class AcceptBattleService implements AcceptBattleUseCase {
private final RivalRepositoryPort rivalRepositoryPort;
private final UserNoticeRepositoryPort userNoticeRepositoryPort;
private final CompeteRefetchNotifier competeRefetchNotifier;
private final ZoneId battleZoneId;

@Override
public void execute(ModifyBattleData.Command command) {
Expand All @@ -39,8 +37,7 @@ public void execute(ModifyBattleData.Command command) {
Battle battle = battleRepositoryPort.findById(command.id())
.orElseThrow(BattleNotFoundException::new);

// 승인 시각을 배틀 시작 시각으로 사용, 종료 시각은 수락일(KST) + duration일의 자정 KST
Battle updatedBattle = battle.accept(Instant.now(), battleZoneId);
Battle updatedBattle = battle.accept(Instant.now());

battleRepositoryPort.save(updatedBattle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private BattleInfo toBattleInfo(
);

LocalDate expireDate = battle.endAt() != null
? battle.endAt().atZone(battleZoneId).toLocalDate().minusDays(1)
? battle.endAt().minusNanos(1).atZone(battleZoneId).toLocalDate()
: null;

return BattleInfo.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public FindDetailedBattleInfoData.Result execute(FindDetailedBattleInfoData.Comm
? battle.startedAt().atZone(battleZoneId).toLocalDate()
: LocalDate.now(battleZoneId);

// exp 계산 기간 종료일: DONE이면 마지막 활동일(endAt 자정의 전날), 진행 중이면 오늘까지
// exp 계산 기간 종료일: DONE이면 endAt 기준 날짜, 진행 중이면 오늘까지
LocalDate endDate = battle.battleStatus().equals(BattleStatus.DONE) && battle.endAt() != null
? battle.endAt().atZone(battleZoneId).toLocalDate().minusDays(1)
? battle.endAt().minusNanos(1).atZone(battleZoneId).toLocalDate()
: LocalDate.now(battleZoneId);

// 클라이언트 표시용 종료 예정일: endAt 자정의 전날 = 마지막 활동일
// 클라이언트 표시용 종료 예정일: endAt 기준 날짜 (마지막 활동일)
LocalDate expireDate = battle.endAt() != null
? battle.endAt().atZone(battleZoneId).toLocalDate().minusDays(1)
? battle.endAt().minusNanos(1).atZone(battleZoneId).toLocalDate()
: null;

// 상대방이 탈퇴해 라이벌이 삭제된 경우 상대 정보 없이 반환
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.process.clash.domain.rival.battle.enums.BattleStatus;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;

public record Battle(
Long id,
Expand Down Expand Up @@ -35,9 +34,8 @@ public static Battle createDefault(int duration, Long rivalId, Long applicantId)
);
}

public Battle accept(Instant now, ZoneId zoneId) {
LocalDate acceptanceDateKST = now.atZone(zoneId).toLocalDate();
Instant endAt = acceptanceDateKST.plusDays(this.duration).atStartOfDay(zoneId).toInstant();
public Battle accept(Instant now) {
Instant endAt = now.plus(this.duration, ChronoUnit.DAYS);
Comment thread
hjbin-25 marked this conversation as resolved.

return new Battle(
this.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.Instant;
import java.time.ZoneId;
import java.util.Collection;
import java.util.Optional;

Expand Down Expand Up @@ -47,17 +46,14 @@ class AcceptBattleServiceTest {

private AcceptBattleService acceptBattleService;

private static final ZoneId BATTLE_ZONE_ID = ZoneId.of("Asia/Seoul");

@BeforeEach
void setUp() {
acceptBattleService = new AcceptBattleService(
battleRepositoryPort,
modifyBattlePolicy,
rivalRepositoryPort,
userNoticeRepositoryPort,
competeRefetchNotifier,
BATTLE_ZONE_ID
competeRefetchNotifier
);
}

Expand All @@ -71,7 +67,7 @@ void execute_notifiesBothUsersOnAccept() {
Battle battle = pendingBattle(battleId, rivalId, opponentId);

when(battleRepositoryPort.findById(battleId)).thenReturn(Optional.of(battle));
when(battleRepositoryPort.save(any(Battle.class))).thenReturn(battle.accept(Instant.now(), BATTLE_ZONE_ID));
when(battleRepositoryPort.save(any(Battle.class))).thenReturn(battle.accept(Instant.now()));
when(rivalRepositoryPort.findOpponentIdByIdAndUserId(rivalId, actor.id())).thenReturn(opponentId);
when(userNoticeRepositoryPort.save(any(UserNotice.class))).thenAnswer(invocation -> invocation.getArgument(0));

Expand All @@ -96,7 +92,7 @@ void execute_savesOneNoticeOnAccept() {
Battle battle = pendingBattle(battleId, rivalId, opponentId);

when(battleRepositoryPort.findById(battleId)).thenReturn(Optional.of(battle));
when(battleRepositoryPort.save(any(Battle.class))).thenReturn(battle.accept(Instant.now(), BATTLE_ZONE_ID));
when(battleRepositoryPort.save(any(Battle.class))).thenReturn(battle.accept(Instant.now()));
when(rivalRepositoryPort.findOpponentIdByIdAndUserId(rivalId, actor.id())).thenReturn(opponentId);
when(userNoticeRepositoryPort.save(any(UserNotice.class))).thenAnswer(invocation -> invocation.getArgument(0));

Expand All @@ -115,7 +111,7 @@ void execute_softDeletesApplyBattleNoticeOnAccept() {
Battle battle = pendingBattle(battleId, rivalId, opponentId);

when(battleRepositoryPort.findById(battleId)).thenReturn(Optional.of(battle));
when(battleRepositoryPort.save(any(Battle.class))).thenReturn(battle.accept(Instant.now(), BATTLE_ZONE_ID));
when(battleRepositoryPort.save(any(Battle.class))).thenReturn(battle.accept(Instant.now()));
when(rivalRepositoryPort.findOpponentIdByIdAndUserId(rivalId, actor.id())).thenReturn(opponentId);
when(userNoticeRepositoryPort.save(any(UserNotice.class))).thenAnswer(invocation -> invocation.getArgument(0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void execute_returnsEnemyTierAsRankTier_whenRankTierIsNotNone() {
}

@Test
@DisplayName("IN_PROGRESS 배틀의 expireDate는 endAt을 KST 기준 LocalDate로 변환한 값이다")
@DisplayName("IN_PROGRESS 배틀의 expireDate는 endAt의 KST 날짜(마지막 활동일)이다")
void execute_returnsExpireDateFromEndAt_whenInProgress() {
Instant endAt = Instant.now().plus(6, ChronoUnit.DAYS);
Battle battle = new Battle(BATTLE_ID, Instant.now(), Instant.now(),
Expand All @@ -205,7 +205,7 @@ void execute_returnsExpireDateFromEndAt_whenInProgress() {

FindAllBattleInfoData.Result result = findAllBattleInfoService.execute(command());

LocalDate expectedDate = endAt.atZone(ZoneId.of("Asia/Seoul")).toLocalDate();
LocalDate expectedDate = endAt.minusNanos(1).atZone(ZoneId.of("Asia/Seoul")).toLocalDate();
assertThat(result.battles().get(0).expireDate()).isEqualTo(expectedDate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;

import static org.assertj.core.api.Assertions.assertThat;

class BattleTest {

@Test
@DisplayName("승인 즉시 startedAt이 설정되고, endAt은 수락일(KST) + duration일의 자정 KST로 설정된다")
@DisplayName("승인 즉시 startedAt이 설정되고, endAt은 수락 시각으로부터 duration일(72시간 단위) 후로 설정된다")
void accept_setsStartedAtAndReturnsInProgress() {
Battle battle = pendingBattle(7);
Instant now = Instant.now();
ZoneId zoneId = ZoneId.of("Asia/Seoul");

Battle result = battle.accept(now, zoneId);
Battle result = battle.accept(now);

LocalDate acceptanceDateKST = now.atZone(zoneId).toLocalDate();
Instant expectedEndAt = acceptanceDateKST.plusDays(7).atStartOfDay(zoneId).toInstant();
Instant expectedEndAt = now.plus(7, ChronoUnit.DAYS);

assertThat(result.battleStatus()).isEqualTo(BattleStatus.IN_PROGRESS);
assertThat(result.startedAt()).isEqualTo(now);
Expand Down
Loading