-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 백신 접종내역 생성 * feat: unclassified Vaccination Discord Webhook * chore: 테스트용 토큰 발급 API 구현 (#45) --------- Co-authored-by: Haebin <[email protected]>
- Loading branch information
1 parent
56a27af
commit 96749e9
Showing
26 changed files
with
321 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package kr.co.vacgom.api | ||
|
||
import kr.co.vacgom.api.auth.oauth.enums.SocialLoginProvider | ||
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.RequestParam | ||
import org.springframework.web.bind.annotation.RestController | ||
import kotlin.random.Random | ||
|
||
@RestController | ||
@RequestMapping(BASE_V3 + "/TEST") | ||
class TestController( | ||
private val userRepository: UserRepository, | ||
private val userTokenService: UserTokenService | ||
) { | ||
|
||
@PostMapping | ||
fun test(@RequestParam tokenType: String): ResponseEntity<String> { | ||
val user = userRepository.findAll()[0] | ||
|
||
when (tokenType) { | ||
"ACCESS" -> { | ||
val accessToken = userTokenService.createAccessToken(user.id, user.role) | ||
return ResponseEntity.ok(accessToken) | ||
} | ||
"REFRESH" -> { | ||
val refreshToken = userTokenService.createRefreshToken(user.id) | ||
return ResponseEntity.ok(refreshToken) | ||
} | ||
"REGISTER" -> { | ||
val socialId = "testSocialId${Random(System.currentTimeMillis()).nextInt()}" | ||
val registerToken = userTokenService.createRegisterToken(socialId, SocialLoginProvider.KAKAO.name) | ||
return ResponseEntity.ok(registerToken) | ||
} | ||
} | ||
|
||
return ResponseEntity.internalServerError().build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/kotlin/kr/co/vacgom/api/global/discord/DiscordSender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/kr/co/vacgom/api/global/discord/RestTemplateConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 55 additions & 1 deletion
56
src/main/kotlin/kr/co/vacgom/api/vaccine/application/VaccinationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
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 | ||
import kr.co.vacgom.api.vaccine.presentation.dto.VaccinationDto | ||
import kr.co.vacgom.api.vaccine.repository.UnclassifiedVaccinationRepository | ||
import kr.co.vacgom.api.vaccine.repository.VaccinationRepository | ||
import kr.co.vacgom.api.vaccine.repository.vaccine.VaccineRepository | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
@Transactional | ||
class VaccinationService { | ||
class VaccinationService( | ||
private val unclassifiedVaccinationRepository: UnclassifiedVaccinationRepository, | ||
private val vaccinationRepository: VaccinationRepository, | ||
private val vaccineRepository: VaccineRepository, | ||
private val babyRepository: BabyRepository, | ||
private val discordSender: DiscordSender | ||
) { | ||
fun createVaccinations(request: VaccinationDto.Request.Create) { | ||
|
||
val baby = babyRepository.findById(request.babyId) | ||
|
||
request.vaccinationRequests.forEach { | ||
try { | ||
val vaccine = vaccineRepository.findByName(it.name) | ||
|
||
val classifiedVaccination = Vaccination( | ||
doseRound = it.doseRound, | ||
doseRoundDescription = it.doseRoundDescription, | ||
vaccinatedAt = it.vaccinatedAt, | ||
facility = it.facility, | ||
manufacturer = it.manufacturer, | ||
productName = it.productName, | ||
lotNumber = it.lotNumber, | ||
vaccine = vaccine, | ||
baby = baby | ||
) | ||
|
||
vaccinationRepository.save(classifiedVaccination) | ||
} catch (businessException: BusinessException) { | ||
val unclassifiedVaccination = UnclassifiedVaccination( | ||
name = it.name, | ||
doseRound = it.doseRound, | ||
doseRoundDescription = it.doseRoundDescription, | ||
vaccinatedAt = it.vaccinatedAt, | ||
facility = it.facility, | ||
manufacturer = it.manufacturer, | ||
productName = it.productName, | ||
lotNumber = it.lotNumber, | ||
baby = baby | ||
) | ||
|
||
val savedUnclassifiedVaccination = unclassifiedVaccinationRepository.save(unclassifiedVaccination) | ||
discordSender.sendVaccinationError(baby, savedUnclassifiedVaccination) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/kr/co/vacgom/api/vaccine/domain/constants/VaccineType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package kr.co.vacgom.api.vaccine.domain.constants | ||
|
||
enum class VaccineType { | ||
NATIONAL, | ||
NATION, | ||
GENERAL, | ||
EVENT | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/kr/co/vacgom/api/vaccine/exception/VaccinationError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package kr.co.vacgom.api.vaccine.exception | ||
|
||
import kr.co.vacgom.api.global.exception.error.ErrorCode | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.HttpStatus.NOT_FOUND | ||
|
||
enum class VaccinationError( | ||
override val message: String, | ||
override val status: HttpStatus, | ||
override val code: String, | ||
) : ErrorCode { | ||
VACCINE_NOT_FOUND("해당 백신이 존재하지 않습니다.", NOT_FOUND, "V_001") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.