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

merge: publishNotificationToApplicant 사용으로 변경 #706

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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 @@ -3,21 +3,22 @@ package team.aliens.dms.domain.point.service
import team.aliens.dms.common.annotation.Service
import team.aliens.dms.common.spi.NotificationEventPort
import team.aliens.dms.common.spi.SecurityPort
import team.aliens.dms.domain.notification.exception.DeviceTokenNotFoundException
import team.aliens.dms.domain.notification.model.Notification
import team.aliens.dms.domain.notification.spi.QueryDeviceTokenPort
import team.aliens.dms.domain.point.model.PointHistory
import team.aliens.dms.domain.point.model.PointOption
import team.aliens.dms.domain.point.spi.CommandPointHistoryPort
import team.aliens.dms.domain.point.spi.CommandPointOptionPort
import team.aliens.dms.domain.student.spi.QueryStudentPort

@Service
class CommandPointServiceImpl(
private val commandPointHistoryPort: CommandPointHistoryPort,
private val commandPointOptionPort: CommandPointOptionPort,
private val notificationEventPort: NotificationEventPort,
private val securityPort: SecurityPort,
private val deviceTokenPort: QueryDeviceTokenPort
private val deviceTokenPort: QueryDeviceTokenPort,
private val queryStudentPort: QueryStudentPort
) : CommandPointService {

override fun savePointHistory(pointHistory: PointHistory) =
Expand All @@ -28,18 +29,31 @@ class CommandPointServiceImpl(
}

override fun saveAllPointHistories(pointHistories: List<PointHistory>) {
val userId = securityPort.getCurrentUserId()
val deviceToken = deviceTokenPort.queryDeviceTokenByUserId(userId)
?: throw DeviceTokenNotFoundException

commandPointHistoryPort.saveAllPointHistories(pointHistories)
val schoolId = securityPort.getCurrentUserSchoolId()

pointHistories.forEach { pointHistory ->
notificationEventPort.publishNotification(
deviceToken,
Notification.PointNotification(pointHistory)
)
val (grade, classRoom, number) = parseGcn(pointHistory.studentGcn)

val student = queryStudentPort.queryStudentBySchoolIdAndGcn(schoolId, grade, classRoom, number)

val deviceToken = student?.let {
deviceTokenPort.queryDeviceTokenByUserId(it.userId!!)
}

if (deviceToken != null) {
notificationEventPort.publishNotificationToApplicant(
listOf(deviceToken),
Notification.PointNotification(pointHistory)
)
}
}

commandPointHistoryPort.saveAllPointHistories(pointHistories)
}

private fun parseGcn(studentGcn: String): Triple<Int, Int, Int> {
val parts = studentGcn.split("-").map { it.toInt() }
return Triple(parts[0], parts[1], parts[2])
}

override fun savePointOption(pointOption: PointOption): PointOption =
Expand Down
Loading