Skip to content

Commit

Permalink
[#140] refactor: 리마인드 발송 대상 조회 로직 수정, FCM 발송 예외 처리,
Browse files Browse the repository at this point in the history
  • Loading branch information
JuHyun419 committed Sep 10, 2022
1 parent 12a8c86 commit 92b3fed
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.yapp.web2.domain.remind.entity.dto.RemindToggleRequest
import com.yapp.web2.exception.custom.AccountNotFoundException
import com.yapp.web2.exception.custom.BookmarkNotFoundException
import com.yapp.web2.infra.fcm.FirebaseService
import com.yapp.web2.infra.slack.SlackService
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.util.Message
import com.yapp.web2.util.RemindCycleUtil
Expand All @@ -25,7 +26,8 @@ class RemindService(
private val bookmarkRepository: BookmarkRepository,
private val accountRepository: AccountRepository,
private val jwtProvider: JwtProvider,
private val firebaseService: FirebaseService
private val firebaseService: FirebaseService,
private val slackApi: SlackService
) {

companion object {
Expand All @@ -36,24 +38,30 @@ class RemindService(
/**
* 오늘자 기준으로 리마인드 발송 대상인 Bookmark List 조회
*/
fun getRemindBookmark(): List<Bookmark> {
fun getRemindBookmark(): MutableList<Bookmark> {
val today = LocalDate.now().toString()
val remindBookmarkList: List<Bookmark> =
bookmarkRepository.findAllBookmarkByRemindTime(today)
val remindBookmarkList: MutableList<Bookmark> = bookmarkRepository.findAllBookmarkByRemindTime(today)

// 리마인드 발송 시각이 오늘이 아닌 리마인드들은 제거
// 리마인드 발송 대상이 아닌 리마인드 제거(발송 시각이 오늘이 아니거나, 리마인드 발송이 이미 처리된 리마인드)
remindBookmarkList.forEach { bookmark ->
bookmark.remindList.removeIf { remind ->
today != remind.remindTime
(today != remind.remindTime) || remind.remindStatus
}
}
return remindBookmarkList
}

fun sendNotification(bookmark: Bookmark) {
bookmark.remindList.forEach { remind ->
val response = firebaseService.sendMessage(remind.fcmToken, Message.NOTIFICATION_MESSAGE, bookmark.title!!)
log.info("Send notification [userId: ${remind.userId}] succeed, Response => $response")
var response = ""
kotlin.runCatching {
response = firebaseService.sendMessage(remind.fcmToken, Message.NOTIFICATION_MESSAGE, bookmark.title!!)
}.onSuccess {
log.info("리마인드 발송 성공 => [User id: ${remind.userId}], response => $response")
}.onFailure {
log.info("리마인드 발송 실패 => [User id: ${remind.userId}, Bookmark id: ${bookmark.id}]")
slackApi.sendSlackAlarm("<!channel> 리마인드 발송 실패 => User id: ${remind.userId}, Bookmark id: ${bookmark.id}")
}
}
}

Expand Down

0 comments on commit 92b3fed

Please sign in to comment.