Skip to content

Commit

Permalink
Merge pull request #103 from TeamMiso/feat/notification-alarm
Browse files Browse the repository at this point in the history
๐Ÿ”€ :: ํ‘ธ์‹œ ์•Œ๋ฆผ ๋กœ์ง ์ถ”๊ฐ€
  • Loading branch information
uuuuuuuk authored Apr 12, 2024
2 parents 4137f19 + 6e8048e commit 78c2b37
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InquiryAdapter(
private val writeInquiryUseCase: WriteInquiryUseCase,
private val detailInquiryUseCase: DetailInquiryUseCase,
private val listFilterInquiryUseCase: ListFilterInquiryUseCase,
private val writeInquiryRespondUseCase: WriteInquiryRespondUseCase
private val inquiryRespondUseCase: InquiryRespondUseCase
) {
@PostMapping
fun write(
Expand Down Expand Up @@ -51,6 +51,6 @@ class InquiryAdapter(

@PatchMapping("/respond/{id}")
fun respond(@PathVariable id: Long, @RequestBody @Valid writeInquiryRespondRequest: WriteInquiryRespondRequest): ResponseEntity<Void> =
writeInquiryRespondUseCase.execute(id, inquiryDataMapper toDto writeInquiryRespondRequest)
inquiryRespondUseCase.execute(id, inquiryDataMapper toDto writeInquiryRespondRequest)
.let { ResponseEntity.status(HttpStatus.OK).build() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package andreas311.miso.domain.inquiry.application.port.input

import andreas311.miso.domain.inquiry.application.port.input.dto.WriteInquiryRespondDto

interface WriteInquiryRespondUseCase {
interface InquiryRespondUseCase {
fun execute(id: Long, writeInquiryRespondDto: WriteInquiryRespondDto)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,33 @@ package andreas311.miso.domain.inquiry.application.service
import andreas311.miso.common.annotation.RollbackService
import andreas311.miso.domain.auth.application.port.output.UserSecurityPort
import andreas311.miso.domain.inquiry.application.exception.InquiryNotFoundException
import andreas311.miso.domain.inquiry.application.port.input.WriteInquiryRespondUseCase
import andreas311.miso.domain.inquiry.application.port.input.InquiryRespondUseCase
import andreas311.miso.domain.inquiry.application.port.input.dto.WriteInquiryRespondDto
import andreas311.miso.domain.inquiry.application.port.output.CommandInquiryPort
import andreas311.miso.domain.inquiry.application.port.output.QueryInquiryPort
import andreas311.miso.domain.inquiry.domain.InquiryStatus
import andreas311.miso.domain.notification.application.port.input.InquiryNotificationSendUseCase
import andreas311.miso.domain.notification.application.port.output.CommandNotificationPort
import andreas311.miso.domain.notification.application.port.output.QueryDeviceTokenPort
import andreas311.miso.domain.notification.domain.Notification

@RollbackService
class WriteInquiryRespondService(
class InquiryRespondService(
private val userSecurityPort: UserSecurityPort,
private val queryInquiryPort: QueryInquiryPort,
private val commandInquiryPort: CommandInquiryPort,
private val commandNotificationPort: CommandNotificationPort
) : WriteInquiryRespondUseCase {
private val queryDeviceTokenPort: QueryDeviceTokenPort,
private val commandNotificationPort: CommandNotificationPort,
private val inquiryNotificationSendUseCase: InquiryNotificationSendUseCase
) : InquiryRespondUseCase {
override fun execute(id: Long, writeInquiryRespondDto: WriteInquiryRespondDto) {
val user = userSecurityPort.currentUser()

val inquiry = queryInquiryPort.findByIdOrNull(id)
?: throw InquiryNotFoundException()

val deviceToken = queryDeviceTokenPort.findByUserIdOrNull(user.id)

commandInquiryPort.saveInquiry(inquiry.updateInquiryStatus(InquiryStatus.COMPLETE))

commandNotificationPort.saveNotification(
Expand All @@ -34,5 +40,7 @@ class WriteInquiryRespondService(
inquiry = inquiry
)
)

deviceToken?.let { inquiryNotificationSendUseCase.execute(inquiry, deviceToken.deviceToken) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import andreas311.miso.domain.inquiry.application.port.output.CommandInquiryPort
import andreas311.miso.domain.inquiry.application.port.output.DiscordMessageSendPort
import andreas311.miso.domain.inquiry.domain.Inquiry
import andreas311.miso.domain.inquiry.domain.InquiryStatus
import andreas311.miso.domain.notification.application.port.input.InquiryNotificationSendUseCase
import andreas311.miso.domain.notification.application.port.output.QueryDeviceTokenPort
import andreas311.miso.thirdparty.aws.s3.util.S3Util
import org.springframework.web.multipart.MultipartFile
import java.time.LocalDateTime
Expand All @@ -17,7 +19,9 @@ class WriteInquiryService(
private val s3Util: S3Util,
private val userSecurityPort: UserSecurityPort,
private val commandInquiryPort: CommandInquiryPort,
private val discordMessageSendPort: DiscordMessageSendPort
private val queryDeviceTokenPort: QueryDeviceTokenPort,
private val discordMessageSendPort: DiscordMessageSendPort,
private val inquiryNotificationSendUseCase: InquiryNotificationSendUseCase
) : WriteInquiryUseCase {
override fun execute(writeInquiryDto: WriteInquiryDto, multipartFile: MultipartFile?) {
val user = userSecurityPort.currentUser()
Expand All @@ -27,8 +31,9 @@ class WriteInquiryService(
s3Util.execute(multipartFile)
} else null

commandInquiryPort.saveInquiry(
Inquiry(
val deviceToken = queryDeviceTokenPort.findByUserIdOrNull(user.id)

val inquiry = Inquiry(
id = 0L,
title = writeInquiryDto.title,
content = writeInquiryDto.content,
Expand All @@ -37,12 +42,15 @@ class WriteInquiryService(
user = user,
createdDate = LocalDateTime.now()
)
)

commandInquiryPort.saveInquiry(inquiry)

sendDiscordMessage(writeInquiryDto.title)

deviceToken?.let { inquiryNotificationSendUseCase.execute(inquiry, deviceToken.deviceToken) }
}

private fun sendDiscordMessage(title: String) {

val inquiryMessage = discordMessageSendPort.createInquiryMessage(title)

discordMessageSendPort.sendDiscordMessage(inquiryMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ class QueryDeviceTokenPersistenceAdapter(
val deviceTokenEntity = deviceTokenRepository.findByUserId(id)
return deviceTokenMapper toDomain deviceTokenEntity
}

override fun findAll(): List<DeviceToken> {
val deviceTokenList = deviceTokenRepository.findAll()
return deviceTokenList.map { deviceTokenMapper.toDomain(it)!! }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package andreas311.miso.domain.notification.application.port.input

interface EnvironmentNotificationSendUseCase {
fun execute()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package andreas311.miso.domain.notification.application.port.input

import andreas311.miso.domain.inquiry.domain.Inquiry

interface InquiryNotificationSendUseCase {
fun execute(inquiry: Inquiry, token: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import java.util.UUID

interface QueryDeviceTokenPort {
fun findByUserIdOrNull(id: UUID): DeviceToken?
fun findAll(): List<DeviceToken>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package andreas311.miso.domain.notification.application.scheduler

import andreas311.miso.domain.notification.application.port.input.EnvironmentNotificationSendUseCase
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class EnvironmentNotificationScheduler(
private val environmentNotificationSendUseCase: EnvironmentNotificationSendUseCase
) {
@Scheduled(cron = "0 0 13 * * ?")
fun sendEnvironmentNotification() {
environmentNotificationSendUseCase.execute()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package andreas311.miso.domain.notification.application.service

import andreas311.miso.domain.notification.application.port.input.EnvironmentNotificationSendUseCase
import andreas311.miso.domain.notification.application.port.output.FcmNotificationPort
import andreas311.miso.domain.notification.application.port.output.QueryDeviceTokenPort
import andreas311.miso.domain.notification.domain.NotificationAlarm
import andreas311.miso.domain.notification.domain.NotificationType
import org.springframework.stereotype.Component

@Component
class EnvironmentNotificationSendService(
private val fcmNotificationPort: FcmNotificationPort,
private val queryDeviceTokenPort: QueryDeviceTokenPort
) : EnvironmentNotificationSendUseCase {
override fun execute() {

runCatching {
fcmNotificationPort.sendEnvironmentNotification(
deviceTokens = queryDeviceTokenPort.findAll().map { it.deviceToken },
notificationAlarm = NotificationAlarm(
title = NotificationType.ENVIRONMENT.title,
content = NotificationType.ENVIRONMENT.content,
writer = "๋ฏธ์†Œ"
)
)

}.onFailure {
it.printStackTrace()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package andreas311.miso.domain.notification.application.service

import andreas311.miso.common.annotation.RollbackService
import andreas311.miso.domain.inquiry.domain.Inquiry
import andreas311.miso.domain.inquiry.domain.InquiryStatus
import andreas311.miso.domain.notification.application.port.input.InquiryNotificationSendUseCase
import andreas311.miso.domain.notification.application.port.output.FcmNotificationPort
import andreas311.miso.domain.notification.domain.NotificationAlarm
import andreas311.miso.domain.notification.domain.NotificationType

@RollbackService
class InquiryNotificationSendService(
private val fcmNotificationPort: FcmNotificationPort
) : InquiryNotificationSendUseCase {
override fun execute(inquiry: Inquiry, token: String) {
val notificationType = when (inquiry.inquiryStatus) {
InquiryStatus.WAIT -> NotificationType.INQUIRY_WAIT
InquiryStatus.COMPLETE -> NotificationType.INQUIRY_COMPLETE
}

runCatching {
fcmNotificationPort.sendInquiryNotification(
deviceToken = token,
notificationAlarm = NotificationAlarm(
title = notificationType.title,
content = notificationType.content,
writer = "๋ฏธ์†Œ"
)
)

}.onFailure {
it.printStackTrace()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package andreas311.miso.domain.notification.domain

enum class NotificationType(
val title: String,
val content: String
) {

INQUIRY_WAIT(
title = "Miso ๋ฌธ์˜์‚ฌํ•ญ ๋“ฑ๋ก ์™„๋ฃŒ!",
content = "์ตœ๋Œ€ํ•œ ๋น ๋ฅด๊ฒŒ ๋‹ต๋ณ€ ๋“œ๋ฆดํ…Œ๋‹ˆ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š” :)"
),

INQUIRY_COMPLETE(
title = "Miso ๋ฌธ์˜์‚ฌํ•ญ ๋‹ต๋ณ€ ๋„์ฐฉ!",
content = "๋‚ด ๋ฌธ์˜ ๋‚ด์—ญ์—์„œ ๋‹ต๋ณ€์„ ํ™•์ธํ•ด์ฃผ์„ธ์š” :)"
),

ENVIRONMENT(
title = "์˜ค๋Š˜์˜ ํ™˜๊ฒฝ ์ง€์‹์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด?",
content = "Miso ์—์„œ ์˜ค๋Š˜์˜ ํ™˜๊ฒฝ ์ง€์‹์„ ํ™•์ธํ•ด๋ณด์„ธ์š” :)"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.MulticastMessage
import com.google.firebase.messaging.Notification
import org.springframework.stereotype.Component

@Component
class FcmNotificationAdapter : FcmNotificationPort {
private val firebaseInstance: FirebaseMessaging
get() = FirebaseMessaging.getInstance()
Expand Down

0 comments on commit 78c2b37

Please sign in to comment.