Skip to content

[feat] 챌린지 종료 이후 정산하는 API 생성 #362#363

Merged
solgito merged 5 commits intomainfrom
feat/create_api_for_settlement#362
Jan 22, 2025
Merged

[feat] 챌린지 종료 이후 정산하는 API 생성 #362#363
solgito merged 5 commits intomainfrom
feat/create_api_for_settlement#362

Conversation

@solgito
Copy link
Contributor

@solgito solgito commented Jan 22, 2025

개요

  • 챌린지가 (중간 취소 등 없이) 정상적으로 종료되면 COMPLETED_PENDING_SETTLEMENT의 상태가 됩니다.
    • 이 상태는 챌린지가 종료되었으나 아직 정산되지 않았음을 의미합니다.
  • 이때 정산 API를 추가하여 벌금 처리 등 챌린지를 완료할 수 있도록 합니다.
    • 이때 챌린지의 상태가 COMPLETED_SETTLED로 변경됩니다.
    • 또한 챌린지 내 isPaidAll 변수값이 true로 변경됩니다.
    • 현재는 앱 내 벌금 결제나 분배 등 다른 기능이 없으므로, 호스트가 자율적으로 정산하게 됩니다.

코드 내용

1. 정산 api를 위한 컨트롤러 메서드 생성

// ChallengeApi.java

@PostMapping("/challenges/{id}/settlement")
public SuccessResponse<Void> setChallengeSettled(@PathVariable("id") Long id,
                                                 @AuthenticationPrincipal CustomUserDetails user) {
    return challengeSettlementService.settleChallenge(id, user.getMember());
}

2. 정산 api를 위한 서비스 클래스 및 메서드 생성

// challenge/application/ChallengeSettlementService.java

public class ChallengeSettlementService {
    ...

    public SuccessResponse<Void> settleChallenge(Long challengeId, Member member) {
        Challenge challenge = challengeSearchService.getChallengeById(challengeId);

        memberUtilsService.isChallengeHost(challenge, member);

        ZonedDateTime now = TimeZoneConverter.convertEtcToLocalTimeZone(ZonedDateTime.now());
        if (!now.isAfter(challenge.getEndDate())) {
            throw new BadRequestException(ErrorCode.INVALID_CHALLENGE_SETTLEMENT_TIME);
        }

        if (!(challenge.getState() == ChallengeState.COMPLETED_PENDING_SETTLEMENT)) {
            throw new BadRequestException(ErrorCode.INVALID_CHALLENGE_STATE_FOR_SETTLEMENT);
        }

        challenge.setStateCompletedSettled();
        challengeRepository.save(challenge);

        return SuccessResponse.of(SuccessCode.CHALLENGE_SETTLEMENT_SUCCESS);
    }
}
  • 정산 로직을 담당하는 메서드입니다.
  • 호스트 여부, 종료 날짜 이후인지 검증, 챌린지의 기존 상태가 COMPLETED_PENDING_SETTLEMENT인지 총 3가지를 확인합니다.
  • 이후 챌린지의 상태를 변경하고 저장합니다.

3. 챌린지 상태 변경을 위한 set 메서드 추가

// Challenge.java

...
public void setStateCompletedSettled() {
    this.isPaidAll = true;
    this.state = ChallengeState.COMPLETED_SETTLED;
}
...
  • 챌린지의 상태를 COMPLETED_SETTLED로 변경하고 isPaidAll 변수의 값을 true로 변경합니다.

4. 기타

  • 관련 테스트 코드 및 문서를 작성했습니다.
  • 스케쥴러 메서드 내의 디버깅 용도 로그 출력 코드를 삭제했습니다.

@solgito solgito added documentation Improvements or additions to documentation enhancement New feature or request labels Jan 22, 2025
@solgito solgito self-assigned this Jan 22, 2025
@solgito solgito linked an issue Jan 22, 2025 that may be closed by this pull request
@solgito solgito merged commit 47d4c69 into main Jan 22, 2025
1 check passed
@solgito solgito deleted the feat/create_api_for_settlement#362 branch January 22, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feat] 챌린지 완료 시 정산 API 추가

1 participant