Skip to content
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
// notification
implementation 'com.github.maricn:logback-slack-appender:1.4.0'
implementation 'net.logstash.logback:logstash-logback-encoder:8.0'
implementation 'com.slack.api:slack-api-client:1.44.2'

// querydsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gg.agit.konect.domain.user.event;

public record UserRegisterEvent(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이벤트는 이미 발생한 사건을 표현하므로, 네이밍은 과거형을 사용하는 것이 적절하다고 합니다!
링크

String email
) {
public static UserRegisterEvent from(String email) {
return new UserRegisterEvent(email);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gg.agit.konect.domain.user.event;

public record UserWithdrawEvent(
String email
) {
public static UserWithdrawEvent from(String email) {
return new UserWithdrawEvent(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.List;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
Expand All @@ -21,6 +22,8 @@
import gg.agit.konect.domain.user.dto.UserInfoResponse;
import gg.agit.konect.domain.user.dto.UserUpdateRequest;
import gg.agit.konect.domain.user.enums.Provider;
import gg.agit.konect.domain.user.event.UserRegisterEvent;
import gg.agit.konect.domain.user.event.UserWithdrawEvent;
import gg.agit.konect.domain.user.model.UnRegisteredUser;
import gg.agit.konect.domain.user.model.User;
import gg.agit.konect.domain.user.repository.UnRegisteredUserRepository;
Expand All @@ -43,6 +46,7 @@ public class UserService {
private final ChatMessageRepository chatMessageRepository;
private final ChatRoomRepository chatRoomRepository;
private final StudyTimeQueryService studyTimeQueryService;
private final ApplicationEventPublisher applicationEventPublisher;

@Transactional
public Integer signup(String email, Provider provider, SignupRequest request) {
Expand Down Expand Up @@ -74,6 +78,7 @@ public Integer signup(String email, Provider provider, SignupRequest request) {

unRegisteredUserRepository.delete(tempUser);

applicationEventPublisher.publishEvent(UserRegisterEvent.from(savedUser.getEmail()));
return savedUser.getId();
}

Expand Down Expand Up @@ -149,5 +154,7 @@ public void deleteUser(Integer userId) {
clubApplyRepository.deleteByUserId(userId);
clubMemberRepository.deleteByUserId(userId);
userRepository.delete(user);

applicationEventPublisher.publishEvent(UserWithdrawEvent.from(user.getEmail()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gg.agit.konect.infrastructure.slack.client;

import static org.springframework.http.MediaType.APPLICATION_JSON;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class SlackClient {

private final RestTemplate restTemplate;

public void sendMessage(String message, String url) {
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON);

Map<String, Object> payload = new HashMap<>();
payload.put("text", message);

HttpEntity<Map<String, Object>> request = new HttpEntity<>(payload, headers);
restTemplate.postForEntity(
url,
request,
String.class
);
} catch (Exception e) {
log.error("Slack 메시지 전송 중 오류 발생", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gg.agit.konect.infrastructure.slack.config;

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

@ConfigurationProperties(prefix = "slack")
public record SlackProperties(
Webhooks webhooks
) {
public record Webhooks(
String error,
String event
) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gg.agit.konect.infrastructure.slack.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum SlackMessageTemplate {

USER_REGISTER(
"""
`%s님이 가입하셨습니다.`
"""
),
USER_WITHDRAWAL(
"""
`%s님이 탈퇴하셨습니다.`
"""
),
;

private final String template;

public String format(Object... args) {
return String.format(template, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gg.agit.konect.infrastructure.slack.listener;

import static org.springframework.transaction.event.TransactionPhase.AFTER_COMMIT;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import gg.agit.konect.domain.user.event.UserRegisterEvent;
import gg.agit.konect.domain.user.event.UserWithdrawEvent;
import gg.agit.konect.infrastructure.slack.service.SlackNotificationService;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class UserSlackListener {

private final SlackNotificationService slackNotificationService;

@Async
@TransactionalEventListener(phase = AFTER_COMMIT)
Copy link
Contributor

@dh2906 dh2906 Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

단순 궁금증인데 서비스 레이어에서 트랜잭션 메소드가 커밋되고 난 뒤 이벤트를 수행하는 구조가 맞나요??? 👀

public void handleUserWithdraw(UserWithdrawEvent event) {
slackNotificationService.notifyUserWithdraw(event.email());
}

@Async
@TransactionalEventListener(phase = AFTER_COMMIT)
public void handleUserRegister(UserRegisterEvent event) {
slackNotificationService.notifyUserRegister(event.email());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gg.agit.konect.infrastructure.slack.service;

import static gg.agit.konect.infrastructure.slack.enums.SlackMessageTemplate.USER_REGISTER;
import static gg.agit.konect.infrastructure.slack.enums.SlackMessageTemplate.USER_WITHDRAWAL;

import org.springframework.stereotype.Service;

import gg.agit.konect.infrastructure.slack.client.SlackClient;
import gg.agit.konect.infrastructure.slack.config.SlackProperties;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class SlackNotificationService {

private final SlackProperties slackProperties;
private final SlackClient slackClient;

public void notifyUserWithdraw(String email) {
String message = USER_WITHDRAWAL.format(email);
slackClient.sendMessage(message, slackProperties.webhooks().event());
}

public void notifyUserRegister(String email) {
String message = USER_REGISTER.format(email);
slackClient.sendMessage(message, slackProperties.webhooks().event());
}
}