-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSugangSnuNotificationService.kt
159 lines (146 loc) · 7.93 KB
/
SugangSnuNotificationService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.wafflestudio.snutt.sugangsnu.common.service
import com.wafflestudio.snutt.common.push.DeeplinkType
import com.wafflestudio.snutt.common.push.dto.PushMessage
import com.wafflestudio.snutt.coursebook.data.Coursebook
import com.wafflestudio.snutt.notification.data.Notification
import com.wafflestudio.snutt.notification.data.NotificationType
import com.wafflestudio.snutt.notification.data.PushCategory
import com.wafflestudio.snutt.notification.service.NotificationService
import com.wafflestudio.snutt.notification.service.PushService
import com.wafflestudio.snutt.notification.service.PushWithNotificationService
import com.wafflestudio.snutt.sugangsnu.common.utils.toKoreanFieldName
import com.wafflestudio.snutt.sugangsnu.job.sync.data.BookmarkLectureDeleteResult
import com.wafflestudio.snutt.sugangsnu.job.sync.data.BookmarkLectureUpdateResult
import com.wafflestudio.snutt.sugangsnu.job.sync.data.TimetableLectureDeleteByOverlapResult
import com.wafflestudio.snutt.sugangsnu.job.sync.data.TimetableLectureDeleteResult
import com.wafflestudio.snutt.sugangsnu.job.sync.data.TimetableLectureSyncResult
import com.wafflestudio.snutt.sugangsnu.job.sync.data.TimetableLectureUpdateResult
import com.wafflestudio.snutt.sugangsnu.job.sync.data.UserLectureSyncResult
import kotlinx.coroutines.coroutineScope
import org.springframework.stereotype.Service
interface SugangSnuNotificationService {
suspend fun notifyUserLectureChanges(userLectureSyncResults: List<UserLectureSyncResult>)
suspend fun notifyCoursebookUpdate(coursebook: Coursebook)
}
@Service
class SugangSnuNotificationServiceImpl(
private val pushWithNotificationService: PushWithNotificationService,
private val notificationService: NotificationService,
private val pushService: PushService,
) : SugangSnuNotificationService {
override suspend fun notifyUserLectureChanges(userLectureSyncResults: List<UserLectureSyncResult>): Unit =
coroutineScope {
val notifications = userLectureSyncResults.map { it.toNotification() }
notificationService.sendNotifications(notifications)
sendPushForTimetable(userLectureSyncResults.filterIsInstance<TimetableLectureSyncResult>())
}
private suspend fun sendPushForTimetable(userLectureSyncResults: List<TimetableLectureSyncResult>) =
coroutineScope {
val userUpdatedLectureCountMap =
userLectureSyncResults.filterIsInstance<TimetableLectureUpdateResult>().toCountMap()
val userDeletedLectureCountMap =
userLectureSyncResults.filter { it is TimetableLectureDeleteResult || it is TimetableLectureDeleteByOverlapResult }
.toCountMap()
val allUserIds = userUpdatedLectureCountMap.keys + userDeletedLectureCountMap.keys
val userIdToMessage =
allUserIds.associateWith { userId ->
val updatedCount = userUpdatedLectureCountMap[userId]
val deletedCount = userDeletedLectureCountMap[userId]
val messageBody =
when {
updatedCount != null && deletedCount != null -> {
"강의 ${updatedCount}개가 변경, ${deletedCount}개가 삭제되었습니다. 알림함에서 자세히 확인하세요."
}
updatedCount != null -> {
"강의 ${updatedCount}개가 변경되었습니다. 알림함에서 자세히 확인하세요."
}
deletedCount != null -> {
"강의 ${deletedCount}개가 삭제되었습니다. 알림함에서 자세히 확인하세요."
}
else -> {
error("This should not happen")
}
}
PushMessage(
title = "수강편람 업데이트",
body = messageBody,
urlScheme = DeeplinkType.NOTIFICATIONS,
)
}
pushService.sendCategoricalTargetPushes(userIdToMessage, PushCategory.LECTURE_UPDATE)
}
override suspend fun notifyCoursebookUpdate(coursebook: Coursebook) {
val messageBody = "${coursebook.year}년도 ${coursebook.semester.fullName} 수강편람이 추가되었습니다."
pushWithNotificationService.sendGlobalPushAndNotification(
PushMessage(title = "신규 수강편람", body = messageBody),
NotificationType.COURSEBOOK,
)
}
private fun List<UserLectureSyncResult>.toCountMap() =
this.map { result -> result.userId to result.lectureId }.distinct().groupingBy { it.first }.eachCount()
private fun UserLectureSyncResult.toNotification(): Notification {
val (message, notificationType, deeplink) =
when (this) {
// 업데이트 알림
is TimetableLectureUpdateResult -> {
Triple(
"""
$year-${semester.fullName} '$timetableTitle' 시간표의
'$courseTitle' 강의가 업데이트 되었습니다.
(항목: ${updatedFields.map { field -> field.toKoreanFieldName() }.distinct().joinToString()})
""".trimIndent().replace("\n", ""),
NotificationType.LECTURE_UPDATE,
DeeplinkType.TIMETABLE_LECTURE.build(timetableId, lectureId),
)
}
is BookmarkLectureUpdateResult -> {
Triple(
"""
$year-${semester.fullName} 관심강좌 목록의 '$courseTitle' 강의가 업데이트 되었습니다.
(항목: ${updatedFields.map { field -> field.toKoreanFieldName() }.distinct().joinToString()})
""".trimIndent().replace("\n", ""),
NotificationType.LECTURE_UPDATE,
DeeplinkType.BOOKMARKS.build(year, semester, lectureId),
)
}
// 폐강 알림
is TimetableLectureDeleteResult -> {
Triple(
"""
$year-${semester.fullName} '$timetableTitle' 시간표의
'$courseTitle' 강의가 폐강되어 삭제되었습니다.
""".trimIndent().replace("\n", ""),
NotificationType.LECTURE_REMOVE,
DeeplinkType.NOTIFICATIONS.build(),
)
}
is BookmarkLectureDeleteResult -> {
Triple(
"""
$year-${semester.fullName} 관심강좌 목록의
'$courseTitle' 강의가 폐강되어 삭제되었습니다.
""".trimIndent().replace("\n", ""),
NotificationType.LECTURE_REMOVE,
DeeplinkType.NOTIFICATIONS.build(),
)
}
is TimetableLectureDeleteByOverlapResult -> {
Triple(
"""
$year-${semester.fullName} '$timetableTitle' 시간표의
'$courseTitle' 강의가 업데이트되었으나, 시간표의 다른 강의와 겹쳐 삭제되었습니다.
""".trimIndent().replace("\n", ""),
NotificationType.LECTURE_REMOVE,
DeeplinkType.NOTIFICATIONS.build(),
)
}
}
return Notification(
userId = userId,
title = "수강편람 업데이트",
message = message,
type = notificationType,
deeplink = deeplink.value,
)
}
}