@@ -15,6 +15,7 @@ import com.app.server.user_challenge.domain.enums.EUserChallengeStatus
1515import com.app.server.user_challenge.domain.exception.UserChallengeException
1616import com.app.server.user_challenge.domain.model.UserChallenge
1717import com.app.server.user_challenge.domain.model.UserChallengeHistory
18+ import com.app.server.user_challenge.enums.EUserChallengeParticipantState
1819import com.app.server.user_challenge.enums.EUserReportResultCode
1920import com.app.server.user_challenge.event.ReportCreatedEvent
2021import com.app.server.user_challenge.infra.ReportInfraService
@@ -25,26 +26,62 @@ import org.springframework.stereotype.Service
2526import java.time.LocalDate
2627
2728@Service
28- @Transactional
29- class UserChallengeCommandService (
29+ class UserChallengeCommandService (
3030 private val userChallengeService : UserChallengeService ,
3131 private val challengeService : ChallengeService ,
3232 private val userChallengeHistoryCommandService : UserChallengeHistoryCommandService ,
3333 private val reportInfraService : ReportInfraService ,
34- private val eventPublisher : ApplicationEventPublisher
34+ private val eventPublisher : ApplicationEventPublisher
3535) : ParticipantChallengeUseCase, UsingIceUseCase {
3636
3737 override fun execute (
3838 challengeParticipantDto : ChallengeParticipantDto ,
39- ) : UserChallenge {
40- validateUserCanParticipateInChallenge(challengeParticipantDto)
41- val userChallenge = createUserChallenge(challengeParticipantDto)
42- saveUserChallengeWithHistory(userChallenge, challengeParticipantDto.participantsTotalDays)
39+ ): UserChallenge {
40+ val userCanParticipantInChallenge: EUserChallengeParticipantState ? =
41+ validateUserCanParticipateInChallenge(challengeParticipantDto)
42+
43+ if (userCanParticipantInChallenge == null ||
44+ userCanParticipantInChallenge == EUserChallengeParticipantState .NEW_CHALLENGE_START
45+ ) {
46+ val userChallenge = createUserChallenge(challengeParticipantDto)
47+
48+ saveUserChallengeWithHistory(
49+ userChallenge,
50+ challengeParticipantDto.participantsTotalDays,
51+ challengeParticipantDto.participantsStartDate
52+ )
53+
54+ return userChallenge
55+ }
56+ // 이미 참여 중인 챌린지인 경우
57+ // if (userCanParticipantInChallenge == EUserChallengeParticipantState.EXISTING_CHALLENGE_CONTINUE)
58+ val userChallenge = userChallengeService.findByUserIdAndChallengeId(
59+ userId = challengeParticipantDto.userId,
60+ challengeId = challengeParticipantDto.challengeId
61+ )
62+
63+ userChallenge!! .plusParticipatedDays(
64+ participantsDate = challengeParticipantDto.participantsTotalDays,
65+ )
66+
67+ saveUserChallengeWithHistory(
68+ userChallenge,
69+ challengeParticipantDto.participantsTotalDays,
70+ participantsStartDay = challengeParticipantDto.participantsStartDate
71+ )
72+
73+ userChallenge.increaseIceCountWhenUserChallengeContinues()
74+
75+ userChallenge.updateReportMessage(null )
76+
77+ userChallenge.updateStatus(EUserChallengeStatus .RUNNING )
78+
4379 return userChallenge
80+
4481 }
4582
4683 fun processAfterCertificateSuccess (
47- userId : Long , userChallengeId : Long , certificationDto : CertificationDataDto
84+ userChallengeId : Long , certificationDto : CertificationDataDto
4885 ): UserChallenge {
4986 val userChallenge = userChallengeService.findById(userChallengeId)
5087
@@ -66,16 +103,15 @@ class UserChallengeCommandService (
66103 userChallenge.validateIncreaseIceCount()
67104
68105 // 챌린지 종료 여부 확인
69- if (userChallenge.checkIsDone(certificationDto.certificationDate)){
70- makeReport(userId, userChallenge)
106+ if (userChallenge.checkIsDone(certificationDto.certificationDate)) {
107+ makeReport(userChallenge)
71108 }
72109
73110 return userChallenge
74111 }
75112
76- private fun makeReport (userId : Long , userChallenge : UserChallenge ) {
113+ private fun makeReport (userChallenge : UserChallenge ) {
77114 val sendToReportServerRequestDto = SendToReportServerRequestDto .from(
78- userId = userId,
79115 challengeTitle = userChallenge.challenge.title,
80116 progress = userChallenge.totalParticipationDayCount.floorDiv(
81117 userChallenge.participantDays.toLong()
@@ -134,7 +170,7 @@ class UserChallengeCommandService (
134170 val userChallenge = userChallengeService.findById(userChallengeId = iceDto.userChallengeId)
135171
136172 // 얼리기 가능 여부 판단
137- userChallenge.validateCanIceAndUse ()
173+ userChallenge.useIce ()
138174 // 날짜 인증 상태 Ice로 변경
139175 userChallenge.updateCertificationStateIsIce(certificationDate)
140176 // 연속 참여 일수 증가
@@ -163,27 +199,27 @@ class UserChallengeCommandService (
163199 pastUserChallengeHistory : UserChallengeHistory ? ,
164200 userChallengeHistory : UserChallengeHistory
165201 ) {
166- // 첫 날인 경우
167- val nowCount : Long = userChallenge.nowConsecutiveParticipationDayCount
202+ val nowCount: Long = userChallenge.nowConsecutiveParticipationDayCount
168203 var consecutiveState: EConsecutiveState
169204
205+ // 첫 날인 경우
170206 if (pastUserChallengeHistory == null && userChallengeHistory.status != EUserChallengeCertificationStatus .FAILED ) {
171207 consecutiveState = EConsecutiveState .FIRST_DAY
172208 }
173209 // 연속 일자 증가
174- else if (userChallengeHistory.status != EUserChallengeCertificationStatus .FAILED &&
175- pastUserChallengeHistory!! .status != EUserChallengeCertificationStatus .FAILED
176- ) {
177-
178- consecutiveState = EConsecutiveState .CONSECUTIVE_ONLY
179-
180- } else if (
210+ else if (
181211 userChallengeHistory.status != EUserChallengeCertificationStatus .FAILED &&
182212 pastUserChallengeHistory!! .status != EUserChallengeCertificationStatus .FAILED &&
183213 userChallenge.nowConsecutiveParticipationDayCount == userChallenge.maxConsecutiveParticipationDayCount
184214 ) {
185215 consecutiveState = EConsecutiveState .CONSECUTIVE_MAX
186216 }
217+ else if (
218+ userChallengeHistory.status != EUserChallengeCertificationStatus .FAILED &&
219+ pastUserChallengeHistory!! .status != EUserChallengeCertificationStatus .FAILED
220+ ) {
221+ consecutiveState = EConsecutiveState .CONSECUTIVE_ONLY
222+ }
187223 // 연속 일자 초기화
188224 else if (userChallengeHistory.status != EUserChallengeCertificationStatus .FAILED && pastUserChallengeHistory!! .status == EUserChallengeCertificationStatus .FAILED ) {
189225 consecutiveState = EConsecutiveState .CONSECUTIVE_RESET
@@ -224,13 +260,14 @@ class UserChallengeCommandService (
224260 }
225261 }
226262
227- private fun validateUserCanParticipateInChallenge (challengeParticipantDto : ChallengeParticipantDto ) {
228- val existingUserChallenge = userChallengeService.findByUserIdAndChallengeId(
263+ private fun validateUserCanParticipateInChallenge (challengeParticipantDto : ChallengeParticipantDto )
264+ : EUserChallengeParticipantState ? {
265+ val existingUserChallenge: UserChallenge ? = userChallengeService.findByUserIdAndChallengeId(
229266 userId = challengeParticipantDto.userId,
230267 challengeId = challengeParticipantDto.challengeId
231268 )
232269
233- existingUserChallenge?.validateCanParticipants()
270+ return existingUserChallenge?.validateCanParticipants()
234271 }
235272
236273 private fun createUserChallenge (
@@ -242,16 +279,21 @@ class UserChallengeCommandService (
242279 userId = challengeParticipantDto.userId,
243280 challenge = challenge,
244281 participantsDate = challengeParticipantDto.participantsTotalDays,
245- status = challengeParticipantDto.status
282+ status = challengeParticipantDto.status
246283 )
247284 )
248285 }
249286
250- private fun saveUserChallengeWithHistory (userChallenge : UserChallenge , participantsDate : Int ) {
287+ private fun saveUserChallengeWithHistory (
288+ userChallenge : UserChallenge ,
289+ participantsDate : Int ,
290+ participantsStartDay : LocalDate
291+ ) {
251292 // UserChallengeHistory를 생성하고 UserChallenge에 연결
252293 userChallengeHistoryCommandService.createUserChallengeHistory(
253294 userChallenge = userChallenge,
254- participantsDate = participantsDate
295+ participantsDate = participantsDate,
296+ startDay = participantsStartDay
255297 )
256298
257299 // UserChallenge와 연결된 모든 히스토리가 함께 저장됨
0 commit comments