Skip to content

Commit

Permalink
알림톡 완료 후 슬랙 알림 템플릿 변경 (#137)
Browse files Browse the repository at this point in the history
* refactor: 알림 템플릿 변경

* refactor: 예상 비용 확인 기능 추가
  • Loading branch information
cookienc authored Jul 15, 2024
1 parent 668fa20 commit b46019b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ class AlarmService (
Color.GREEN,
listOf(
"""
|*전송한 메세지 수* : ${event.successMessageCount()}
|*남은 포인트* : ${event.point()}
|*잔액* : ${event.balance()}
*전송한 메세지 수* : ${event.successMessageCount()}
*남은 포인트* : ${event.point()}
*잔액* : ${event.balance()}
*예상 차감 금액(VAT 별도)* : -${event.cost()}
""".trimIndent()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backend.itracker.alarm.service.event

import java.time.LocalDateTime


interface SuccessReservationOfNotificationEvent {

fun reservationTime(): LocalDateTime
Expand All @@ -12,4 +11,6 @@ interface SuccessReservationOfNotificationEvent {
fun point(): Float

fun balance(): Float

fun cost(): Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package backend.itracker.schedule.infra.notification.event
import backend.itracker.alarm.service.event.SuccessReservationOfNotificationEvent
import java.time.LocalDateTime

private const val SOLAPI_KAKAO_ALARAM_COST = 9L

data class MessageReservationSuccessEvent(
val reservationTime: LocalDateTime,
val successMessageCount: Int,
Expand All @@ -25,4 +27,8 @@ data class MessageReservationSuccessEvent(
override fun balance(): Float {
return balance
}

override fun cost(): Long {
return successMessageCount * SOLAPI_KAKAO_ALARAM_COST
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import java.time.ZoneOffset

val logger = KotlinLogging.logger {}

private const val RESERVE_FAIlED = 0

@Profile("!test")
@Component
class NotificationClient(
Expand All @@ -31,7 +33,7 @@ class NotificationClient(
override fun reserveNotificationOfPriceChange(
priceChangeTemplate: PriceChangeNotificationInfo,
receiverPhoneNumbers: List<String>,
) {
): Int {
val option = KakaoOption(
pfId = nurigoKakaoChannelConfig.pfId,
templateId = nurigoKakaoChannelConfig.priceChangeNotificationTemplateId,
Expand All @@ -47,20 +49,11 @@ class NotificationClient(
)
}

val reservationTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(nurigoKakaoChannelConfig.sendingHour, 0))
val scheduledDateTime = reservationTime.toInstant(ZoneOffset.of("+9"))!!
val scheduledDateTime = getReservationTime().toInstant(ZoneOffset.of("+9"))!!

try {
messageService.send(messages, scheduledDateTime)
val balance = messageService.getBalance()
eventPublisher.publishEvent(
MessageReservationSuccessEvent(
reservationTime = reservationTime!!,
point = balance.point!!,
balance = balance.balance!!,
successMessageCount = messages.size
)
)
return messages.size
} catch (exception: NurigoMessageNotReceivedException) {
logger.error {
"""
Expand All @@ -76,6 +69,33 @@ class NotificationClient(
exception.message
)
)

return RESERVE_FAIlED
}
}

override fun checkBalance(reservationCount: Int) {
try {
val balance = messageService.getBalance()
eventPublisher.publishEvent(
MessageReservationSuccessEvent(
reservationTime = getReservationTime(),
point = balance.point!!,
balance = balance.balance!!,
successMessageCount = reservationCount
)
)
} catch (exception: NurigoMessageNotReceivedException) {
logger.error { "잔액 확인 실패 cause : $exception" }
}
}

private fun getReservationTime(): LocalDateTime {
return LocalDateTime.of(
LocalDate.now(), LocalTime.of(
nurigoKakaoChannelConfig.sendingHour,
0
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ class NotificationSchedulerService(
.groupBy { it.product}
.mapValues { entry -> entry.value.map { it.member } }

var reservationCount = 0
productCategoryMap.forEach { (product, members) ->
val priceChangeNotificationInfo = notificationComposite.getPriceChangeNotificationInfo(product)
val receiverPhoneNumbers = members.mapNotNull { it.phoneNumber }
notificationSender.reserveNotificationOfPriceChange(priceChangeNotificationInfo, receiverPhoneNumbers)
reservationCount += notificationSender.reserveNotificationOfPriceChange(priceChangeNotificationInfo, receiverPhoneNumbers)
}

if (reservationCount > 0) {
notificationSender.checkBalance(reservationCount)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ interface NotificationSender {
fun reserveNotificationOfPriceChange(
priceChangeTemplate: PriceChangeNotificationInfo,
receiverPhoneNumbers: List<String>,
)
): Int

fun checkBalance(reservationCount: Int)
}

0 comments on commit b46019b

Please sign in to comment.