Skip to content

Commit 44d6c5e

Browse files
committed
✅ test(report): 인증, 레포트 관련 테스트 완료
1 parent a74d93f commit 44d6c5e

24 files changed

+864
-376
lines changed

server/src/main/kotlin/com/app/server/challenge_certification/application/service/CertificationService.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class CertificationService(
2323
) : CertificationUseCase {
2424

2525
override fun certificateChallengeWithDate(
26-
userId: Long,
2726
certificationRequestDto: CertificationRequestDto,
2827
certificationDate: LocalDate
2928
): UserChallenge {
@@ -45,7 +44,6 @@ class CertificationService(
4544

4645
eventPublisher.publishEvent(
4746
CertificationSucceededEvent(
48-
userId = userId,
4947
userChallengeId = userChallenge.id!!,
5048
imageUrl = certificationRequestDto.imageUrl,
5149
certificatedDate = certificationDate,

server/src/main/kotlin/com/app/server/challenge_certification/event/CertificationSucceededEvent.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.app.server.challenge_certification.event
33
import java.time.LocalDate
44

55
data class CertificationSucceededEvent(
6-
val userId : Long,
76
val userChallengeId: Long,
87
val imageUrl: String,
98
val certificatedDate: LocalDate

server/src/main/kotlin/com/app/server/challenge_certification/ui/controller/CertificationController.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ class CertificationController(
2323

2424
@PostMapping("/certification")
2525
fun certificateDailyUserChallenge(
26-
@UserId userId: Long,
2726
@RequestBody certificationRequestDto: CertificationRequestDto
2827
): ApiResponse<ResultCode> {
2928
certificationUseCase.certificateChallengeWithDate(
30-
userId = userId,
3129
certificationRequestDto = certificationRequestDto,
3230
certificationDate = LocalDate.now()
3331
)

server/src/main/kotlin/com/app/server/challenge_certification/ui/usecase/CertificationUseCase.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import java.time.LocalDate
77
interface CertificationUseCase {
88

99
fun certificateChallengeWithDate(
10-
userId: Long,
1110
certificationRequestDto: CertificationRequestDto,
1211
certificationDate: LocalDate
1312
): UserChallenge

server/src/main/kotlin/com/app/server/common/config/SchedulerConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ import org.springframework.scheduling.annotation.EnableScheduling
66
@Configuration
77
@EnableScheduling
88
class SchedulerConfig {
9+
// TODO : Pending 상태인 챌린지들도 시간 보고 COMPLETED 상태로 변경해줘야 함
910
}

server/src/main/kotlin/com/app/server/user_challenge/application/service/UserChallengeCommandService.kt

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.app.server.user_challenge.domain.enums.EUserChallengeStatus
1515
import com.app.server.user_challenge.domain.exception.UserChallengeException
1616
import com.app.server.user_challenge.domain.model.UserChallenge
1717
import com.app.server.user_challenge.domain.model.UserChallengeHistory
18+
import com.app.server.user_challenge.enums.EUserChallengeParticipantState
1819
import com.app.server.user_challenge.enums.EUserReportResultCode
1920
import com.app.server.user_challenge.event.ReportCreatedEvent
2021
import com.app.server.user_challenge.infra.ReportInfraService
@@ -25,26 +26,62 @@ import org.springframework.stereotype.Service
2526
import 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와 연결된 모든 히스토리가 함께 저장됨

server/src/main/kotlin/com/app/server/user_challenge/application/service/UserChallengeEventListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class UserChallengeEventListener(
3434

3535
private fun processWhenReceive(event: CertificationSucceededEvent) {
3636
userChallengeCommandService.processAfterCertificateSuccess(
37-
userId = event.userId,
3837
userChallengeId = event.userChallengeId,
3938
certificationDto = CertificationDataDto(
4039
imageUrl = event.imageUrl,
@@ -63,6 +62,7 @@ class UserChallengeEventListener(
6362
val reportDto = ReportDto(
6463
userChallengeId = userChallenge.id!!,
6564
totalDays = userChallenge.participantDays,
65+
userChallengeStatus = userChallenge.status,
6666
successDays = userChallenge.totalParticipationDayCount.toInt(),
6767
reportMessage = userChallenge.reportMessage!!,
6868
maxConsecutiveParticipationDays = userChallenge.maxConsecutiveParticipationDayCount,

server/src/main/kotlin/com/app/server/user_challenge/application/service/UserChallengeHistoryCommandService.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,30 @@ import java.time.LocalDate
99

1010
@Service
1111
@Transactional
12-
class UserChallengeHistoryCommandService (
12+
class UserChallengeHistoryCommandService(
1313
) {
1414

15-
fun createUserChallengeHistory(userChallenge: UserChallenge, participantsDate: Int) {
15+
fun createUserChallengeHistory(
16+
userChallenge: UserChallenge,
17+
participantsDate: Int,
18+
startDay: LocalDate
19+
) {
1620
val userChallengeHistoryList = mutableListOf<UserChallengeHistory>()
1721

1822
for (i in 0..participantsDate.minus(1)) {
19-
val history = createUserChallengeHistory(i, userChallenge)
23+
val history = createUserChallengeHistory(i, userChallenge, startDay)
2024
userChallengeHistoryList.add(history)
2125
}
2226
userChallenge.addUserChallengeHistories(userChallengeHistoryList)
2327

2428
}
2529

26-
private fun createUserChallengeHistory(i: Int, userChallenge: UserChallenge): UserChallengeHistory {
27-
val challengeDate = LocalDate.now().plusDays(i.toLong())
30+
private fun createUserChallengeHistory(
31+
i: Int,
32+
userChallenge: UserChallenge,
33+
startDay: LocalDate
34+
): UserChallengeHistory {
35+
val challengeDate = startDay.plusDays(i.toLong())
2836
return UserChallengeHistory(
2937
id = null,
3038
userChallenge = userChallenge,

server/src/main/kotlin/com/app/server/user_challenge/application/service/UserChallengeQueryService.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package com.app.server.user_challenge.application.service
22

3-
import com.app.server.user_challenge.ui.controller.ReportWaiter
4-
import com.app.server.user_challenge.ui.dto.CertificationReportDataDto
5-
import com.app.server.user_challenge.ui.dto.ReportDto
63
import com.app.server.user_challenge.domain.enums.EUserChallengeCertificationStatus
74
import com.app.server.user_challenge.domain.enums.EUserChallengeStatus
8-
import com.app.server.user_challenge.ui.usecase.GetTotalUserChallengeUseCase
95
import com.app.server.user_challenge.domain.model.UserChallenge
106
import com.app.server.user_challenge.domain.model.UserChallengeHistory
11-
import com.app.server.user_challenge.ui.dto.CertificationData
12-
import com.app.server.user_challenge.ui.dto.GetTotalUserChallengeResponseDto
13-
import com.app.server.user_challenge.ui.dto.UserChallengeQuery
7+
import com.app.server.user_challenge.ui.dto.*
8+
import com.app.server.user_challenge.ui.usecase.GetTotalUserChallengeUseCase
149
import com.app.server.user_challenge.ui.usecase.GetUserChallengeReportUseCase
15-
import org.springframework.cglib.core.Local
1610
import org.springframework.stereotype.Service
1711
import java.time.LocalDate
1812

@@ -59,6 +53,7 @@ class UserChallengeQueryService(
5953
return ReportDto(
6054
userChallengeId = userChallenge.id!!,
6155
totalDays = userChallenge.participantDays,
56+
userChallengeStatus = userChallenge.status,
6257
successDays = successDays,
6358
reportMessage = userChallenge.reportMessage!!,
6459
maxConsecutiveParticipationDays = userChallenge.maxConsecutiveParticipationDayCount,
@@ -73,7 +68,7 @@ class UserChallengeQueryService(
7368
todayDate: LocalDate
7469
) {
7570
val endDate : LocalDate = userChallenge.createdAt!!.toLocalDate()
76-
.plusDays(userChallenge.participantDays.toLong())
71+
.plusDays(userChallenge.participantDays-1L)
7772

7873
if (userChallenge.status == EUserChallengeStatus.PENDING &&
7974
todayDate.isBefore(endDate.plusDays(2))

server/src/main/kotlin/com/app/server/user_challenge/application/service/UserChallengeService.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class UserChallengeService (
1818
return userChallengeRepository.save(userChallenge)
1919
}
2020

21+
fun saveAll(userChallenges: List<UserChallenge>) : List<UserChallenge> {
22+
return userChallengeRepository.saveAll(userChallenges)
23+
}
24+
2125
fun findById(userChallengeId: Long) : UserChallenge =
2226
userChallengeRepository.findById(userChallengeId)
2327
.orElseThrow { NotFoundException(CommonResultCode.NOT_FOUND, "해당 챌린지를 참여하고 있지 않습니다.")

0 commit comments

Comments
 (0)