|
| 1 | +package com.blockguard.server.domain.report.application; |
| 2 | + |
| 3 | +import com.blockguard.server.domain.report.dao.UserReportRecordRepository; |
| 4 | +import com.blockguard.server.domain.report.domain.ReportStepCheckbox; |
| 5 | +import com.blockguard.server.domain.report.domain.ReportStepProgress; |
| 6 | +import com.blockguard.server.domain.report.domain.UserReportRecord; |
| 7 | +import com.blockguard.server.domain.report.domain.enums.CheckboxType; |
| 8 | +import com.blockguard.server.domain.report.domain.enums.ReportStep; |
| 9 | +import com.blockguard.server.domain.report.domain.enums.ReportStepCheckboxConfig; |
| 10 | +import com.blockguard.server.domain.report.dto.request.UpdateReportStepRequest; |
| 11 | +import com.blockguard.server.domain.report.dto.response.CurrentReportRecordResponse; |
| 12 | +import com.blockguard.server.domain.report.dto.response.ReportRecordResponse; |
| 13 | +import com.blockguard.server.domain.report.dto.response.ReportRecordStepResponse; |
| 14 | +import com.blockguard.server.domain.user.domain.User; |
| 15 | +import com.blockguard.server.global.common.codes.ErrorCode; |
| 16 | +import com.blockguard.server.global.exception.BusinessExceptionHandler; |
| 17 | +import lombok.RequiredArgsConstructor; |
| 18 | +import org.springframework.stereotype.Service; |
| 19 | +import org.springframework.transaction.annotation.Transactional; |
| 20 | + |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Optional; |
| 24 | + |
| 25 | +@Service |
| 26 | +@RequiredArgsConstructor |
| 27 | +public class ReportRecordService { |
| 28 | + private final UserReportRecordRepository userReportRecordRepository; |
| 29 | + |
| 30 | + @Transactional |
| 31 | + public ReportRecordResponse createReportRecord(User user) { |
| 32 | + if (userReportRecordRepository.existsByUserAndIsCompletedFalse(user)) { |
| 33 | + throw new BusinessExceptionHandler(ErrorCode.REPORT_ALREADY_IN_PROGRESS); |
| 34 | + } |
| 35 | + |
| 36 | + UserReportRecord userReportRecord = UserReportRecord.builder() |
| 37 | + .user(user) |
| 38 | + .build(); |
| 39 | + |
| 40 | + ReportStepProgress progress = ReportStepProgress.createWithDefaultCheckboxes(userReportRecord, ReportStep.STEP1); |
| 41 | + userReportRecord.addProgress(progress); |
| 42 | + userReportRecordRepository.save(userReportRecord); |
| 43 | + |
| 44 | + return ReportRecordResponse.from(userReportRecord, progress); |
| 45 | + } |
| 46 | + |
| 47 | + @Transactional |
| 48 | + public CurrentReportRecordResponse getCurrentRecord(User user) { |
| 49 | + Optional<CurrentReportRecordResponse> currentReportRecordResponse = userReportRecordRepository.findFirstByUserAndIsCompletedFalseOrderByCreatedAtDesc(user) |
| 50 | + .map(record -> { |
| 51 | + ReportStepProgress lastProgress = record.getProgressList().stream() |
| 52 | + .max(Comparator.comparing(ReportStepProgress::getStep)) |
| 53 | + .orElseThrow(); |
| 54 | + int stepNum = lastProgress.getStep().getStepNumber(); |
| 55 | + return CurrentReportRecordResponse.builder() |
| 56 | + .reportId(record.getId()) |
| 57 | + .createdAt(String.valueOf(record.getCreatedAt())) |
| 58 | + .step(stepNum) |
| 59 | + .build(); |
| 60 | + |
| 61 | + }); |
| 62 | + return currentReportRecordResponse.orElse(null); |
| 63 | + } |
| 64 | + |
| 65 | + @Transactional |
| 66 | + public ReportRecordStepResponse getStepInfo(User user, Long reportId, int stepNumber) { |
| 67 | + UserReportRecord record = getUserReportRecord(user, reportId); |
| 68 | + ReportStep step = getReportStep(stepNumber); |
| 69 | + ReportStepProgress progress = getReportStepProgress(record, step); |
| 70 | + |
| 71 | + return buildReportRecordStepResponse(reportId, stepNumber, progress, record); |
| 72 | + } |
| 73 | + |
| 74 | + @Transactional |
| 75 | + public ReportRecordStepResponse updateStepInfo(User user, Long reportId, int stepNumber, UpdateReportStepRequest updateReportStepRequest) { |
| 76 | + UserReportRecord record = getUserReportRecord(user, reportId); |
| 77 | + ReportStep step = getReportStep(stepNumber); |
| 78 | + ReportStepProgress progress = getReportStepProgress(record, step); |
| 79 | + |
| 80 | + // 요청 유효성 검증 |
| 81 | + validateUpdateStepInfo(updateReportStepRequest, progress, step); |
| 82 | + |
| 83 | + // 체크박스 상태 업데이트 |
| 84 | + updateCheckboxStates(updateReportStepRequest, progress); |
| 85 | + |
| 86 | + // 현 단계가 완료되었을때 다음 단계 생성 또는 해당 신고 완료 |
| 87 | + if (updateReportStepRequest.getIsCompleted()) { |
| 88 | + // step 완료 플레그 업데이트 |
| 89 | + progress.setCompleted(true); |
| 90 | + createNextStepIfNeeded(stepNumber, record); |
| 91 | + } |
| 92 | + |
| 93 | + userReportRecordRepository.save(record); |
| 94 | + |
| 95 | + return buildReportRecordStepResponse(reportId, stepNumber, progress, record); |
| 96 | + } |
| 97 | + |
| 98 | + private void validateUpdateStepInfo(UpdateReportStepRequest updateReportStepRequest, ReportStepProgress progress, ReportStep step) { |
| 99 | + // 이미 완료된 step 인지 검증 |
| 100 | + validateProgressIsCompleted(progress); |
| 101 | + // 체스박스 수 검증 |
| 102 | + validateCheckboxCounts(updateReportStepRequest, step); |
| 103 | + validateCompletionConsistency(updateReportStepRequest); |
| 104 | + } |
| 105 | + |
| 106 | + private void validateProgressIsCompleted(ReportStepProgress progress) { |
| 107 | + if (progress.isCompleted()) { |
| 108 | + throw new BusinessExceptionHandler(ErrorCode.REPORT_STEP_ALREADY_COMPLETED); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void createNextStepIfNeeded(int stepNumber, UserReportRecord record) { |
| 113 | + // 마지막 단계가 아니라면 |
| 114 | + if (stepNumber < ReportStep.STEP4.getStepNumber()){ |
| 115 | + |
| 116 | + // 다음 스텝 존재 여부 검증 |
| 117 | + ReportStep nextStep = ReportStep.from(stepNumber + 1).get(); |
| 118 | + boolean exists = record.getProgressList().stream() |
| 119 | + .anyMatch(p -> p.getStep() == nextStep); |
| 120 | + |
| 121 | + // 다음 스텝이 존재하지 않는다면 새롭게 생성 |
| 122 | + if (!exists) { |
| 123 | + ReportStepProgress nextProgress = ReportStepProgress.createWithDefaultCheckboxes(record, nextStep); |
| 124 | + record.addProgress(nextProgress); |
| 125 | + } |
| 126 | + } |
| 127 | + // 마지막 STEP 이 완료된 것이라면 |
| 128 | + else{ |
| 129 | + record.setCompleted(true); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private void updateCheckboxStates(UpdateReportStepRequest updateReportStepRequest, ReportStepProgress progress) { |
| 134 | + progress.getCheckboxes().stream() |
| 135 | + .filter(cb -> cb.getType() == CheckboxType.REQUIRED) |
| 136 | + .sorted(Comparator.comparingInt(ReportStepCheckbox::getBoxIndex)) |
| 137 | + .forEach((cb) -> |
| 138 | + cb.updateChecked(updateReportStepRequest.getCheckBoxes().get(cb.getBoxIndex())) |
| 139 | + ); |
| 140 | + |
| 141 | + if(updateReportStepRequest.getRecommendedCheckBoxes() != null){ |
| 142 | + progress.getCheckboxes().stream() |
| 143 | + .filter(cb -> cb.getType() == CheckboxType.RECOMMENDED) |
| 144 | + .sorted(Comparator.comparingInt(ReportStepCheckbox::getBoxIndex)) |
| 145 | + .forEach((cb) -> |
| 146 | + cb.updateChecked(updateReportStepRequest.getRecommendedCheckBoxes().get(cb.getBoxIndex())) |
| 147 | + ); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void validateCheckboxCounts(UpdateReportStepRequest request, ReportStep step) { |
| 152 | + ReportStepCheckboxConfig checkboxConfig = ReportStepCheckboxConfig.of(step); |
| 153 | + |
| 154 | + // 필수 체크박스 검증 |
| 155 | + boolean isRequiredCountInvalid = request.getCheckBoxes().size() != checkboxConfig.getRequiredCount(); |
| 156 | + |
| 157 | + // 권장 체크박스 개수 검증 |
| 158 | + List<Boolean> rec = request.getRecommendedCheckBoxes(); |
| 159 | + boolean isRecommendedCountInvalid; |
| 160 | + if (checkboxConfig.getRecommendedCount() == 0) { |
| 161 | + // 권장 개수가 0개면, rec는 null 또는 비어 있어야 함 |
| 162 | + isRecommendedCountInvalid = rec != null && !rec.isEmpty(); |
| 163 | + } else { |
| 164 | + // 권장 개수가 1개 이상이면, null 아니고 정확히 갯수 맞아야 함 |
| 165 | + isRecommendedCountInvalid = (rec == null || rec.size() != checkboxConfig.getRecommendedCount()); |
| 166 | + } |
| 167 | + if (isRequiredCountInvalid || isRecommendedCountInvalid) { |
| 168 | + throw new BusinessExceptionHandler(ErrorCode.INVALID_CHECKBOX_COUNT); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // 1) isCompleted=true 인데 하나라도 unchecked → 예외 |
| 173 | + private void validateCompletionConsistency(UpdateReportStepRequest request) { |
| 174 | + boolean allRequiredChecked = request.getCheckBoxes().stream() |
| 175 | + .allMatch(Boolean::booleanValue); // 모두 true 인가 |
| 176 | + if (!allRequiredChecked && request.getIsCompleted()) { |
| 177 | + throw new BusinessExceptionHandler(ErrorCode.INVALID_STEP_COMPLETION); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private ReportRecordStepResponse buildReportRecordStepResponse(Long reportId, int stepNumber, ReportStepProgress progress, UserReportRecord record) { |
| 182 | + List<Boolean> resultRequiredCheckboxes = getRequiredCheckboxes(progress, CheckboxType.REQUIRED); |
| 183 | + List<Boolean> resultRecommendedCheckboxes = getRequiredCheckboxes(progress, CheckboxType.RECOMMENDED); |
| 184 | + |
| 185 | + resultRecommendedCheckboxes = resultRecommendedCheckboxes.isEmpty() ? null : resultRecommendedCheckboxes; |
| 186 | + |
| 187 | + return ReportRecordStepResponse.builder() |
| 188 | + .reportId(reportId) |
| 189 | + .step(stepNumber) |
| 190 | + .checkBoxes(resultRequiredCheckboxes) |
| 191 | + .recommendedCheckBoxes(resultRecommendedCheckboxes) |
| 192 | + .createdAt(String.valueOf(record.getCreatedAt())) |
| 193 | + .isCompleted(progress.isCompleted()) |
| 194 | + .build(); |
| 195 | + } |
| 196 | + |
| 197 | + private List<Boolean> getRequiredCheckboxes(ReportStepProgress progress, CheckboxType type) { |
| 198 | + return progress.getCheckboxes().stream() |
| 199 | + .filter(cb -> cb.getType() == type) |
| 200 | + .sorted(Comparator.comparingInt(ReportStepCheckbox::getBoxIndex)) |
| 201 | + .map(ReportStepCheckbox::isChecked) |
| 202 | + .toList(); |
| 203 | + } |
| 204 | + |
| 205 | + private ReportStepProgress getReportStepProgress(UserReportRecord record, ReportStep step) { |
| 206 | + return record.getProgressList().stream() |
| 207 | + .filter(p -> p.getStep() == step) |
| 208 | + .findFirst() |
| 209 | + .orElseThrow(() -> new BusinessExceptionHandler(ErrorCode.INVALID_STEP)); |
| 210 | + } |
| 211 | + |
| 212 | + private ReportStep getReportStep(int stepNumber) { |
| 213 | + return ReportStep.from(stepNumber) |
| 214 | + .orElseThrow(() -> new BusinessExceptionHandler(ErrorCode.INVALID_STEP)); |
| 215 | + } |
| 216 | + |
| 217 | + private UserReportRecord getUserReportRecord(User user, Long reportId) { |
| 218 | + return userReportRecordRepository.findByIdAndUser(reportId, user) |
| 219 | + .orElseThrow(() -> new BusinessExceptionHandler(ErrorCode.REPORT_NOT_FOUND)); |
| 220 | + } |
| 221 | +} |
0 commit comments