diff --git a/src/main/kotlin/goodspace/bllsoneshot/entity/assignment/Task.kt b/src/main/kotlin/goodspace/bllsoneshot/entity/assignment/Task.kt index a70a8f3..cf5a0e7 100644 --- a/src/main/kotlin/goodspace/bllsoneshot/entity/assignment/Task.kt +++ b/src/main/kotlin/goodspace/bllsoneshot/entity/assignment/Task.kt @@ -7,6 +7,7 @@ import goodspace.bllsoneshot.global.exception.ExceptionMessage.NEGATIVE_ACTUAL_M import jakarta.persistence.* import org.hibernate.annotations.BatchSize import java.time.LocalDate +import java.time.LocalDateTime @Entity class Task( @@ -49,6 +50,9 @@ class Task( @Column(nullable = false) var completed: Boolean = false + protected set + + var completedAt: LocalDate? = null @get:Transient val questions: List @@ -84,6 +88,11 @@ class Task( proofShots.forEach { it.markFeedbackAsRead() } } + fun markAsCompleted(date: LocalDate) { + completed = true + completedAt = date + } + // TODO: 로직 이해하기 // 피드백 재저장 시, 기존 FEEDBACK 타입 Comment만 삭제하고 QUESTION은 보존한다. // Comment는 task.comments와 proofShot.comments 양쪽에 참조되어 있으므로, diff --git a/src/main/kotlin/goodspace/bllsoneshot/entity/user/User.kt b/src/main/kotlin/goodspace/bllsoneshot/entity/user/User.kt index c04e464..4fe540e 100644 --- a/src/main/kotlin/goodspace/bllsoneshot/entity/user/User.kt +++ b/src/main/kotlin/goodspace/bllsoneshot/entity/user/User.kt @@ -1,6 +1,7 @@ package goodspace.bllsoneshot.entity.user import goodspace.bllsoneshot.entity.BaseEntity +import jakarta.persistence.CascadeType import jakarta.persistence.Column import jakarta.persistence.Entity import jakarta.persistence.EnumType @@ -33,12 +34,9 @@ class User( @Column(columnDefinition = "MEDIUMBLOB") val profileImage: ByteArray? = null ) : BaseEntity() { - @OneToMany(mappedBy = "mentee", fetch = FetchType.LAZY) + @OneToMany(mappedBy = "mentee", fetch = FetchType.LAZY, cascade = [CascadeType.ALL]) val subjects: MutableList = mutableListOf() - @OneToMany(mappedBy = "mentee", fetch = FetchType.LAZY) - val reports: MutableList = mutableListOf() - var refreshToken: String? = null var fcmToken: String? = null diff --git a/src/main/kotlin/goodspace/bllsoneshot/global/init/UserInitializer.kt b/src/main/kotlin/goodspace/bllsoneshot/global/init/UserInitializer.kt index 2387fc3..34849b0 100644 --- a/src/main/kotlin/goodspace/bllsoneshot/global/init/UserInitializer.kt +++ b/src/main/kotlin/goodspace/bllsoneshot/global/init/UserInitializer.kt @@ -1,5 +1,7 @@ package goodspace.bllsoneshot.global.init +import goodspace.bllsoneshot.entity.assignment.Subject +import goodspace.bllsoneshot.entity.user.MenteeSubject import goodspace.bllsoneshot.entity.user.User import goodspace.bllsoneshot.entity.user.UserRole import goodspace.bllsoneshot.entity.user.UserRole.ROLE_MENTEE @@ -89,6 +91,13 @@ class UserInitializer( profileImage = profileImage ) + val subjects = listOf( + MenteeSubject(user, Subject.KOREAN), + MenteeSubject(user, Subject.ENGLISH), + MenteeSubject(user, Subject.MATH), + ) + user.subjects.addAll(subjects) + return userRepository.save(user) } diff --git a/src/main/kotlin/goodspace/bllsoneshot/mentor/service/MentorDashboardService.kt b/src/main/kotlin/goodspace/bllsoneshot/mentor/service/MentorDashboardService.kt index b8d4558..30c579d 100644 --- a/src/main/kotlin/goodspace/bllsoneshot/mentor/service/MentorDashboardService.kt +++ b/src/main/kotlin/goodspace/bllsoneshot/mentor/service/MentorDashboardService.kt @@ -85,9 +85,9 @@ class MentorDashboardService( .toSet() val recentTaskByMenteeId = taskRepository - .findMostRecentTasksByMenteeIds(menteeIds) + .findMostRecentTaskByMenteeIds(menteeIds) .groupBy { it.mentee.id!! } - .mapValues { (_, tasks) -> tasks.first() } + .mapValues { (_, tasks) -> tasks.first() } // completedAt DESC이므로 first가 가장 최근 val menteeDetails = mentees.map { mentee -> val recentTask = recentTaskByMenteeId[mentee.id] diff --git a/src/main/kotlin/goodspace/bllsoneshot/repository/task/TaskRepository.kt b/src/main/kotlin/goodspace/bllsoneshot/repository/task/TaskRepository.kt index 718dce9..ad503a9 100644 --- a/src/main/kotlin/goodspace/bllsoneshot/repository/task/TaskRepository.kt +++ b/src/main/kotlin/goodspace/bllsoneshot/repository/task/TaskRepository.kt @@ -10,6 +10,7 @@ import goodspace.bllsoneshot.notification.dto.response.UnfinishedTaskCountRespon import java.time.LocalDate import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query +import org.springframework.data.repository.query.Param interface TaskRepository : JpaRepository { @@ -160,18 +161,16 @@ interface TaskRepository : JpaRepository { @Query( """ - SELECT t FROM Task t - WHERE t.mentee.id IN :menteeIds - AND t.isResource = false - AND t.date = ( - SELECT MAX(t2.date) FROM Task t2 - WHERE t2.mentee = t.mentee - AND t2.isResource = false - ) - ORDER BY t.mentee.id, t.id DESC - """ + SELECT t FROM Task t + WHERE t.mentee.id IN :menteeIds + AND t.isResource = false + AND t.completedAt IS NOT NULL + ORDER BY t.completedAt DESC + """ ) - fun findMostRecentTasksByMenteeIds(menteeIds: List): List + fun findMostRecentTaskByMenteeIds( + @Param("menteeIds") menteeIds: List + ): List @Query( """ diff --git a/src/main/kotlin/goodspace/bllsoneshot/task/service/TaskService.kt b/src/main/kotlin/goodspace/bllsoneshot/task/service/TaskService.kt index e4b73ea..1e3d7f2 100644 --- a/src/main/kotlin/goodspace/bllsoneshot/task/service/TaskService.kt +++ b/src/main/kotlin/goodspace/bllsoneshot/task/service/TaskService.kt @@ -193,7 +193,7 @@ class TaskService( validateTaskCompletable(task, request.currentDate) task.actualMinutes = request.actualMinutes - task.completed = true + task.markAsCompleted(request.currentDate) } @Transactional @@ -322,10 +322,6 @@ class TaskService( val task = taskRepository.findById(taskId) .orElseThrow { IllegalArgumentException(TASK_NOT_FOUND.message) } - if (task.isResource) { - throw IllegalArgumentException(TASK_NOT_FOUND.message) - } - return task }