Skip to content

Commit

Permalink
feat: unclassified Vaccination Discord Webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
h-beeen committed Jan 5, 2025
1 parent b00320c commit 2f4e0f3
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 4 deletions.
2 changes: 1 addition & 1 deletion API-CONFIG
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1")

annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")

// discord
implementation("dev.kord:kord-core:0.15.0")
}

kotlin {
Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/kr/co/vacgom/api/TestController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.co.vacgom.api

import kr.co.vacgom.api.global.presentation.GlobalPath.BASE_V3
import kr.co.vacgom.api.user.application.UserTokenService
import kr.co.vacgom.api.user.repository.UserRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping(BASE_V3 + "/TEST")
class TestController(
private val userRepository: UserRepository,
private val userTokenService: UserTokenService
) {

@PostMapping
fun test(): ResponseEntity<String> {
val user = userRepository.findAll().get(0)
val accessToken = userTokenService.createAccessToken(user.id, user.role)
return ResponseEntity.ok(accessToken)
}
}
65 changes: 65 additions & 0 deletions src/main/kotlin/kr/co/vacgom/api/global/discord/DiscordSender.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package kr.co.vacgom.api.global.discord

import com.fasterxml.jackson.databind.ObjectMapper
import kr.co.vacgom.api.baby.domain.Baby
import kr.co.vacgom.api.vaccine.domain.UnclassifiedVaccination
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@Component
class DiscordSender(
@Value("\${discord.deployWebhookURL}")
private val webhookURL: String,

@Value("\${discord.welcomeWebhookURL}")
private val welcomeURL: String,

private val objectMapper: ObjectMapper,

private val restTemplate: RestTemplate
) {
fun sendVaccinationError(
baby: Baby,
unclassifiedVaccination: UnclassifiedVaccination
) {
val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_JSON

val now = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분 ss초")
val formattedNow = now.format(formatter)

val embeds = mapOf(
"title" to "**[ \uD83D\uDEA8 긴급 \uD83D\uDEA8 ] ERR-001** 미분류 백신 추가 필요",
"description" to "**미분류 백신** : ${unclassifiedVaccination.name}\n\n" +
"**요청시간** : " + formattedNow + "\n\n" +
"**LOG**\n\n" +
"```json\n{\n" +
"\t\"id\" : ${unclassifiedVaccination.id},\n" +
"\t\"name\" : ${unclassifiedVaccination.name},\n" +
"\t\"doseRound\" : ${unclassifiedVaccination.doseRound},\n" +
"\t\"doseRoundDescription\" : ${unclassifiedVaccination.doseRoundDescription},\n" +
"\t\"vaccinatedAt\" : ${unclassifiedVaccination.vaccinatedAt},\n" +
"\t\"facility\" : ${unclassifiedVaccination.facility},\n" +
"\t\"manufacturer\" : ${unclassifiedVaccination.manufacturer},\n" +
"\t\"productName\" : ${unclassifiedVaccination.productName},\n" +
"\t\"lotNumber\" : ${unclassifiedVaccination.lotNumber},\n" +
"\t\"babyId\" : ${unclassifiedVaccination.baby.id}\n}```",
"color" to "15548997"
)
val payload = mapOf(
"content" to null,
"embeds" to listOf(embeds)
)

val jsonPayload = objectMapper.writeValueAsString(payload)
val entity = HttpEntity(jsonPayload, headers)
restTemplate.postForLocation(webhookURL, entity)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.co.vacgom.api.global.discord

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.client.RestTemplate

@Configuration
class RestTemplateConfig {

@Bean
fun restTemplate(): RestTemplate {
return RestTemplate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface UserRepository {
fun findBySocialId(socialId: String): User?
fun findById(userId: UUID): User?
fun deleteById(userId: UUID)
fun findAll(): List<User>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.*
@Repository
class UserRepositoryAdapter(
private val userJpaRepository: UserJpaRepository
): UserRepository {
) : UserRepository {
override fun save(user: User): User {
return userJpaRepository.save(user)
}
Expand All @@ -24,4 +24,8 @@ class UserRepositoryAdapter(
override fun deleteById(userId: UUID) {
userJpaRepository.deleteById(userId)
}

override fun findAll(): List<User> {
return userJpaRepository.findAll()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.co.vacgom.api.vaccine.application

import kr.co.vacgom.api.baby.repository.BabyRepository
import kr.co.vacgom.api.global.discord.DiscordSender
import kr.co.vacgom.api.global.exception.error.BusinessException
import kr.co.vacgom.api.vaccine.domain.UnclassifiedVaccination
import kr.co.vacgom.api.vaccine.domain.Vaccination
Expand All @@ -17,7 +18,8 @@ class VaccinationService(
private val unclassifiedVaccinationRepository: UnclassifiedVaccinationRepository,
private val vaccinationRepository: VaccinationRepository,
private val vaccineRepository: VaccineRepository,
private val babyRepository: BabyRepository
private val babyRepository: BabyRepository,
private val discordSender: DiscordSender
) {
fun createVaccinations(request: VaccinationDto.Request.Create) {

Expand Down Expand Up @@ -53,7 +55,8 @@ class VaccinationService(
baby = baby
)

unclassifiedVaccinationRepository.save(unclassifiedVaccination)
val savedUnclassifiedVaccination = unclassifiedVaccinationRepository.save(unclassifiedVaccination)
discordSender.sendVaccinationError(baby, savedUnclassifiedVaccination)
}
}
}
Expand Down

0 comments on commit 2f4e0f3

Please sign in to comment.