Skip to content

Commit

Permalink
feat: (#523) tokenSubscribe 저장 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls committed Jun 16, 2023
1 parent 493d9d1 commit ff8b4d4
Showing 1 changed file with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package team.aliens.dms.domain.notification.service

import org.springframework.stereotype.Component
import team.aliens.dms.domain.notification.dto.TopicSubscribeResponse
import team.aliens.dms.domain.notification.exception.DeviceTokenNotFoundException
import team.aliens.dms.domain.notification.model.DeviceToken
import team.aliens.dms.domain.notification.model.Notification
import team.aliens.dms.domain.notification.model.Topic
import team.aliens.dms.domain.notification.model.TopicSubscribe
import team.aliens.dms.domain.notification.spi.DeviceTokenPort
import team.aliens.dms.domain.notification.spi.NotificationPort
import team.aliens.dms.domain.notification.spi.TopicSubscribePort
Expand All @@ -13,6 +15,7 @@ import java.util.UUID
@Component
class NotificationServiceImpl(
private val notificationPort: NotificationPort,
private val topicSubscribePort: TopicSubscribePort,
private val deviceTokenPort: DeviceTokenPort
) : NotificationService {

Expand All @@ -38,35 +41,42 @@ class NotificationServiceImpl(
}

override fun unsubscribeTopic(deviceToken: String, topic: Topic) {
val savedToken = this.getDeviceTokenByDeviceToken(deviceToken)
topicSubscribePort.saveTopicSubscribe(
TopicSubscribe(
deviceTokenId = savedToken.id,
topic = topic
)
)
notificationPort.unsubscribeTopic(
deviceToken = this.getDeviceTokenByDeviceToken(deviceToken).deviceToken,
deviceToken = deviceToken,
topic = topic
)
}

override fun updateSubscribes(deviceToken: String, topicsToSubscribe: List<Pair<Topic, Boolean>>) {

val deviceToken = this.getDeviceTokenByDeviceToken(deviceToken)
val savedToken = this.getDeviceTokenByDeviceToken(deviceToken)

val subscribes = mutableListOf<Topic>()
val unsubscribes = mutableListOf<Topic>()

topicsToSubscribe.forEach { (topic, isSubscribe) ->
if (isSubscribe) {
notificationPort.subscribeTopic(
deviceToken = deviceToken.deviceToken,
deviceToken = deviceToken,
topic = topic
)
subscribes.add(topic)
} else {
notificationPort.unsubscribeTopic(
deviceToken = deviceToken.deviceToken,
deviceToken = deviceToken,
topic = topic
)
unsubscribes.add(topic)
}
}

topicSubscribePort.saveAllTopicSubscribes(
subscribes.map { TopicSubscribe(savedToken.id, it) }
)
topicSubscribePort.deleteByUserIdAndTopics(
userId = savedToken.id,
topics = unsubscribes
)
}

private fun getDeviceTokenByDeviceToken(deviceToken: String) =
Expand Down

0 comments on commit ff8b4d4

Please sign in to comment.