Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔀::WebHook 로직 추가 #235

Merged
merged 15 commits into from
Dec 27, 2023
Empty file added Task
Empty file.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ dependencies {
implementation(Dependencies.KOTLIN_REFLET)
implementation(Dependencies.KOTLIN_JDK)

// jackson
implementation(Dependencies.KOTLIN_SERIALIZATION)

// aws
implementation(Dependencies.AWS_S3)

Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ object Dependencies {
// kotlin
const val KOTLIN_REFLET = "org.jetbrains.kotlin:kotlin-reflect"
const val KOTLIN_JDK = "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
const val KOTLIN_SERIALIZATION = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"

// spring
const val SPRING_JPA = "org.springframework.boot:spring-boot-starter-data-jpa"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.msg.gauth.domain.auth.event

data class SignupLoggingEvent(
val email: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.msg.gauth.domain.auth.handler

import com.msg.gauth.domain.auth.event.SignupLoggingEvent
import com.msg.gauth.global.thirdparty.discord.properties.DiscordProperties
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import org.springframework.context.event.EventListener
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.transaction.event.TransactionPhase
import org.springframework.transaction.event.TransactionalEventListener
import org.springframework.web.client.RestTemplate

@Component
class AuthEventHandler(
private val discordProperties: DiscordProperties
) {
@EventListener
fun signupLoggingHandler(signupLoggingEvent: SignupLoggingEvent) {
val fields = listOf(
JsonObject(
mapOf(
"name" to JsonPrimitive("이메일"),
"value" to JsonPrimitive(signupLoggingEvent.email)
)
)
)
val embed = JsonObject(
mapOf(
"title" to JsonPrimitive("회원가입 알림"),
"description" to JsonPrimitive("회원가입을 시도하였습니다."),
"color" to JsonPrimitive(48127),
"fields" to JsonArray(fields)
)
)
val jsonObject = JsonObject(
mapOf(
"content" to JsonPrimitive("[알림] 회원가입을 시도하였습니다."),
"embeds" to JsonArray(listOf(embed))
)
)

val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_JSON

val restTemplate = RestTemplate()
val entity = HttpEntity(jsonObject.toString(), headers)
restTemplate.postForObject(discordProperties.url, entity, String::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.msg.gauth.domain.auth.service

import com.msg.gauth.domain.auth.event.SignupLoggingEvent
import com.msg.gauth.domain.auth.presentation.dto.request.SignUpDto
import com.msg.gauth.domain.email.repository.EmailAuthRepository
import com.msg.gauth.domain.user.User
Expand All @@ -11,14 +12,16 @@ import com.msg.gauth.domain.user.repository.UserRepository
import com.msg.gauth.domain.user.repository.UserRoleRepository
import com.msg.gauth.global.annotation.service.TransactionalService
import com.msg.gauth.global.exception.exceptions.DuplicateEmailException
import org.springframework.context.ApplicationEventPublisher
import org.springframework.security.crypto.password.PasswordEncoder

@TransactionalService
class SignUpService(
private val userRepository: UserRepository,
private val userRoleRepository: UserRoleRepository,
private val passwordEncoder: PasswordEncoder,
private val emailAuthRepository: EmailAuthRepository
private val emailAuthRepository: EmailAuthRepository,
private val applicationEventPublisher: ApplicationEventPublisher
) {
fun execute(signUpDto: SignUpDto): Long {
if (userRepository.existsByEmail(signUpDto.email)) {
Expand Down Expand Up @@ -47,6 +50,8 @@ class SignUpService(

saveUserRole(user)

applicationEventPublisher.publishEvent(SignupLoggingEvent(user.email))

return savedUser.id
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.msg.gauth.global.thirdparty.discord.properties

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding

@ConstructorBinding
@ConfigurationProperties(prefix = "discord.webhook")
data class DiscordProperties(
val url: String
)
5 changes: 4 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ spring:
max-file-size: ${MAX_FILE_SIZE}
max-request-size: ${MAX_REQUEST_SIZE}

discord:
webhook:
url: ${WEBHOOK_URL}

cloud:
aws:
credentials:
Expand All @@ -53,7 +57,6 @@ cloud:
stack:
auto: false


messages:
basename: i18n/exception
encoding: UTF-8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class GauthBackendApplicationTests {

@Test
fun contextLoads() {
}
Expand Down