Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 현재 출석 상태 조회 기능 구현 #29

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ class CheckInSession(
attendanceGateway.save(attendance.checkIn(input.now, thisWeekSession.startTime))
}

private fun LocalDateTime.getMonday(): LocalDateTime {
val dayOfWeek = this.dayOfWeek
val daysToAdd = DayOfWeek.MONDAY.value - dayOfWeek.value
return this.plusDays(daysToAdd.toLong())
}
private fun LocalDateTime.getMonday() = this.toLocalDate().with(DayOfWeek.MONDAY).atStartOfDay()

private fun inRange(distance: Double, range: Double) = distance <= range

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.depromeet.makers.domain.usecase

import com.depromeet.makers.domain.exception.InvalidCheckInTimeException
import com.depromeet.makers.domain.gateway.AttendanceGateway
import com.depromeet.makers.domain.gateway.MemberGateway
import com.depromeet.makers.domain.gateway.SessionGateway
import com.depromeet.makers.domain.model.Attendance
import com.depromeet.makers.domain.model.AttendanceStatus
import java.time.DayOfWeek
import java.time.LocalDateTime

class GetCheckInStatus(
private val memberGateway: MemberGateway,
private val sessionGateway: SessionGateway,
private val attendanceGateway: AttendanceGateway,
) : UseCase<GetCheckInStatus.GetCheckInStatusInput, GetCheckInStatus.GetCheckInStatusOutput> {
data class GetCheckInStatusInput(
val now: LocalDateTime,
val memberId: String,
)

data class GetCheckInStatusOutput(
val generation: Int,
val week: Int,
val isBeforeSession15minutes: Boolean,
val needFloatingButton: Boolean,
val expectAttendanceStatus: AttendanceStatus,
)

override fun execute(input: GetCheckInStatusInput): GetCheckInStatusOutput {
val member = memberGateway.getById(input.memberId)

val monday = input.now.getMonday()
val thisWeekSession = sessionGateway.findByStartTimeBetween(
monday,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 당일이 아니라 그 주 range로 찾는 이유가 따로 있나용?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

원래 있던 로직 가져온거긴한데 당일 아니면 시간체크 다르게 먹어서 크게 상관없을것 같긴 합니다!

monday.plusDays(7)
) ?: throw InvalidCheckInTimeException()

val attendance = runCatching {
attendanceGateway.findByMemberIdAndGenerationAndWeek(
member.memberId,
thisWeekSession.generation,
thisWeekSession.week
)
}.getOrDefault(
Attendance.newAttendance(
generation = thisWeekSession.generation,
week = thisWeekSession.week,
member = member,
)
)

// 현재 시간이 세션 시간 15분 전 && 세션 시작 시간 사이인지 확인 -> 팝업 띄우기 여부
val isBeforeSession15minutes = input.now.isAfter(thisWeekSession.startTime.minusMinutes(15)) &&
input.now.isBefore(thisWeekSession.startTime)

// 현재 시간이 출석 요청 가능 시간 사이 && 출석 상태가 ON_HOLD인 경우 -> 플로팅 버튼 띄우기 여부
val needFloatingButton = attendance.isAttendanceOnHold() &&
input.now.isAfter(thisWeekSession.startTime) && input.now.isBefore(thisWeekSession.startTime.plusMinutes(120))

val expectAttendanceStatus = when {
// 세션 시작 ~ 30분 사이 -> 출석으로 인정될 상태
input.now.isAfter(thisWeekSession.startTime) && input.now.isBefore(thisWeekSession.startTime.plusMinutes(30)) -> AttendanceStatus.ATTENDANCE
// 세션 시작 30분 ~ 120분 사이 -> 지각으로 인정될 상태
input.now.isAfter(thisWeekSession.startTime.plusMinutes(30)) &&
input.now.isBefore(thisWeekSession.startTime.plusMinutes(120)) -> AttendanceStatus.TARDY

else -> AttendanceStatus.ATTENDANCE_ON_HOLD
}

return GetCheckInStatusOutput(
generation = attendance.generation,
week = attendance.week,
isBeforeSession15minutes = isBeforeSession15minutes,
needFloatingButton = needFloatingButton,
expectAttendanceStatus = expectAttendanceStatus,
)
}

private fun LocalDateTime.getMonday() = this.toLocalDate().with(DayOfWeek.MONDAY).atStartOfDay()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface JpaSessionRepository : JpaRepository<SessionEntity, String> {

fun findByGenerationAndWeek(generation: Int, week: Int): SessionEntity

fun findByStartTimeBetween(startTime: LocalDateTime, endTime: LocalDateTime): SessionEntity?
fun findByStartTimeBetween(startTime: LocalDateTime, endTime: LocalDateTime): List<SessionEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class SessionGatewayImpl(
): Session? {
return jpaSessionRepository
.findByStartTimeBetween(startTime, endTime)
.firstOrNull()
?.toDomain()
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.depromeet.makers.presentation.restapi.controller

import com.depromeet.makers.domain.usecase.CheckInSession
import com.depromeet.makers.domain.usecase.GetCheckInStatus
import com.depromeet.makers.presentation.restapi.dto.response.CheckInStatusResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.security.core.Authentication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
Expand All @@ -17,6 +20,7 @@ import java.time.LocalDateTime
@RequestMapping("/v1/check-in")
class CheckInController(
private val checkInSession: CheckInSession,
private val getCheckInStatus: GetCheckInStatus,
) {

@Operation(summary = "세션 출석", description = "세션에 출석합니다. (서버에서 현재 시간을 기준으로 출석 처리)")
Expand Down Expand Up @@ -44,4 +48,24 @@ class CheckInController(
)
)
}

@Operation(summary = "세션 출석 상태", description = "현재 세션 출석 상태 확인합니다. (CheckInStatusResponse 스키마에 자세한 설명있습니다.)")
@GetMapping
fun checkInStatus(
authentication: Authentication,
): CheckInStatusResponse {
val checkInStatus = getCheckInStatus.execute(
GetCheckInStatus.GetCheckInStatusInput(
now = LocalDateTime.now(),
memberId = authentication.name,
)
)
return CheckInStatusResponse(
generation = checkInStatus.generation,
week = checkInStatus.week,
isBeforeSession15minutes = checkInStatus.isBeforeSession15minutes,
needFloatingButton = checkInStatus.needFloatingButton,
expectAttendanceStatus = checkInStatus.expectAttendanceStatus,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.depromeet.makers.presentation.restapi.dto.response

import com.depromeet.makers.domain.model.AttendanceStatus
import io.swagger.v3.oas.annotations.media.Schema

@Schema(name = "CheckInStatusResponse", description = "세션 출석 상태 응답 DTO")
data class CheckInStatusResponse(
@Schema(name = "generation", description = "기수")
val generation: Int,

@Schema(name = "week", description = "주차")
val week: Int,

@Schema(name = "isBeforeSession15minutes", description = "팝업 메시지를 띄울지 여부 (세션 시작 15분 전 ~ 세션 시작 시간 사이인 경우)")
val isBeforeSession15minutes: Boolean,

@Schema(name = "needFloatingButton", description = "플로팅 버튼 노출 여부 (현재 시간이 출석 요청 가능 시간 && 멤버의 출석 상태가 ATTENDANCE_ON_HOLD 인 경우)")
val needFloatingButton: Boolean,

@Schema(name = "expectAttendanceStatus", description = "출석 시 예상하는 상태")
val expectAttendanceStatus: AttendanceStatus,
) {
}