diff --git a/src/main/java/org/terning/terningserver/repository/user/UserRepository.java b/src/main/java/org/terning/terningserver/repository/user/UserRepository.java index 3149ef9..0606453 100644 --- a/src/main/java/org/terning/terningserver/repository/user/UserRepository.java +++ b/src/main/java/org/terning/terningserver/repository/user/UserRepository.java @@ -14,4 +14,5 @@ public interface UserRepository extends JpaRepository { Optional findByAuthIdAndAuthType(String authId, AuthType authType); + } diff --git a/src/main/java/org/terning/terningserver/service/AuthServiceImpl.java b/src/main/java/org/terning/terningserver/service/AuthServiceImpl.java index b3b8a39..009ca2b 100644 --- a/src/main/java/org/terning/terningserver/service/AuthServiceImpl.java +++ b/src/main/java/org/terning/terningserver/service/AuthServiceImpl.java @@ -43,6 +43,7 @@ public class AuthServiceImpl implements AuthService { private final ValueConfig valueConfig; private final UserRepository userRepository; private final FilterRepository filterRepository; + private final WebhookService webhookService; @Override @Transactional @@ -62,6 +63,8 @@ public SignUpResponseDto signUp(String authId, SignUpRequestDto request) { Token token = getFullToken(user); + webhookService.sendDiscordNotification(user); // 디스코드에 회원가입 알림 전송 + return createSignUpResponseDto(token, user); } diff --git a/src/main/java/org/terning/terningserver/service/WebhookService.java b/src/main/java/org/terning/terningserver/service/WebhookService.java new file mode 100644 index 0000000..64f43b3 --- /dev/null +++ b/src/main/java/org/terning/terningserver/service/WebhookService.java @@ -0,0 +1,54 @@ +package org.terning.terningserver.service; + +import lombok.RequiredArgsConstructor; +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.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.client.RestTemplate; +import org.terning.terningserver.domain.User; +import org.terning.terningserver.repository.user.UserRepository; + +import java.util.HashMap; +import java.util.Map; + +@RequiredArgsConstructor +@Service +@Transactional +public class WebhookService { + + // 디스코드 웹훅 URL 주입 + @Value("${discord.webhook.url}") + private String discordWebhookUrl; + + private final UserRepository userRepository; + + // 알림을 보내는 메서드 + public void sendDiscordNotification(User user) { + + // REST 요청을 처리하기 위한 RestTemplate 객체 생성 + RestTemplate restTemplate = new RestTemplate(); + + // 회원 수를 기존 DB에서 조회하여 총 회원 수 계산 + Long totalMembers = userRepository.count(); + + // 알림 메시지 생성 + String message = String.format("가입자명 : %s\n[%d] 번째 유저가 회원가입했습니다!", user.getName(), totalMembers); + + // HTTP 요청을 위한 헤더 설정 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + // HTTP 요청 바디에 전송할 메시지 설정 + Map body = new HashMap<>(); + body.put("content", message); + + // HTTP 요청 엔터티 생성 + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + + // 디스코드 웹훅 URL로 POST 요청을 보내어 알림 전송 + restTemplate.postForEntity(discordWebhookUrl, requestEntity, String.class); + } +}