Skip to content

Commit 8df538a

Browse files
authored
Merge pull request #161 from TimeBank-Sparta/feat/#160-notification-service-slack
feat: slack 알림 추가 및 재시도 추가
2 parents d8e5cf8 + 68dde6d commit 8df538a

16 files changed

Lines changed: 208 additions & 112 deletions

File tree

common/src/main/java/com/timebank/common/infrastructure/external/notification/dto/NotificationEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ public class NotificationEvent {
2323
private LocalDateTime sentAt;
2424
private NotificationEventType eventType; // 이벤트 구분 (CREATED, UPDATED, DELETED)
2525
private Map<String, String> payload; // (추가 정보 전달용)
26-
26+
private String slackUserEmail;
2727
}

notification-service/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ dependencies {
3838
// ✅ 공통 모듈
3939
implementation 'com.timebank:common:0.0.1-SNAPSHOT'
4040
implementation 'org.springframework.kafka:spring-kafka'
41-
41+
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
4242
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
4343
implementation 'org.springframework.boot:spring-boot-starter-mail'
4444
implementation 'org.springframework.boot:spring-boot-starter-actuator'
4545
implementation 'io.micrometer:micrometer-registry-prometheus'
46-
46+
implementation 'io.github.resilience4j:resilience4j-spring-boot3'
47+
implementation 'io.github.resilience4j:resilience4j-retry'
48+
4749
runtimeOnly 'com.mysql:mysql-connector-j'
4850
compileOnly 'org.projectlombok:lombok'
4951
annotationProcessor 'org.projectlombok:lombok'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.timebank.notification_service.application.client;
2+
3+
import com.timebank.notification_service.application.dto.SlackBotMessage;
4+
import com.timebank.notification_service.application.dto.SlackWebHookMessage;
5+
6+
public interface SlackClient {
7+
void sendMessage(SlackWebHookMessage message);
8+
9+
void sendMessage(SlackBotMessage request);
10+
11+
String getUserIdByEmail(String email);
12+
13+
}

notification-service/src/main/java/com/timebank/notification_service/application/dto/NotificationDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.timebank.notification_service.application.dto;
22

3+
import com.timebank.common.infrastructure.external.notification.dto.NotificationType;
34
import com.timebank.notification_service.domain.entity.Notification;
4-
import com.timebank.notification_service.domain.entity.NotificationType;
55

66
import jakarta.validation.constraints.NotBlank;
77
import jakarta.validation.constraints.NotNull;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.timebank.notification_service.application.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
@Builder
10+
public class SlackBotMessage {
11+
private String channel;
12+
private String text;
13+
14+
public static SlackBotMessage of(String channel, String text) {
15+
return SlackBotMessage.builder()
16+
.channel(channel)
17+
.text(text)
18+
.build();
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.timebank.notification_service.application.dto;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class SlackUserResponse {
7+
private boolean ok;
8+
private SlackUser user;
9+
10+
@Getter
11+
public static class SlackUser {
12+
private String id;
13+
private String teamId;
14+
private String name;
15+
private String realName;
16+
private SlackProfile profile;
17+
18+
@Getter
19+
public static class SlackProfile {
20+
private String email;
21+
}
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.timebank.notification_service.application.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
@Builder
10+
public class SlackWebHookMessage {
11+
private String text;
12+
13+
public static SlackWebHookMessage from(String text) {
14+
return SlackWebHookMessage.builder()
15+
.text(text)
16+
.build();
17+
}
18+
}

notification-service/src/main/java/com/timebank/notification_service/application/event/NotificationEvent.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

notification-service/src/main/java/com/timebank/notification_service/application/service/NotificationService.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import org.springframework.stereotype.Service;
1212
import org.springframework.transaction.annotation.Transactional;
1313

14+
import com.timebank.common.infrastructure.external.notification.dto.NotificationEventType;
15+
import com.timebank.notification_service.application.client.SlackClient;
1416
import com.timebank.notification_service.application.dto.NotificationDto;
15-
import com.timebank.notification_service.application.event.NotificationEvent;
17+
import com.timebank.notification_service.application.dto.SlackBotMessage;
18+
import com.timebank.notification_service.application.dto.SlackWebHookMessage;
1619
import com.timebank.notification_service.domain.entity.Notification;
17-
import com.timebank.notification_service.domain.entity.NotificationEventType;
1820
import com.timebank.notification_service.domain.repository.NotificationRepository;
1921

2022
import jakarta.persistence.EntityNotFoundException;
@@ -29,6 +31,7 @@ public class NotificationService {
2931

3032
private final NotificationRepository notificationRepository;
3133
private final KafkaTemplate<String, Object> kafkaTemplate;
34+
private final SlackClient slackClient;
3235

3336
/**
3437
* 알림 생성
@@ -46,10 +49,6 @@ public NotificationDto createNotification(NotificationDto notificationDto) {
4649
.build();
4750
notification = notificationRepository.save(notification);
4851

49-
// Kafka 이벤트 발행 (CREATED)
50-
NotificationEvent event = new NotificationEvent(notification, NotificationEventType.CREATED);
51-
kafkaTemplate.send(NotificationEventType.CREATED.getTopic(), event);
52-
5352
return NotificationDto.fromEntity(notification);
5453
}
5554

@@ -96,10 +95,6 @@ public NotificationDto markAsRead(Long notificationId) {
9695
notification.setIsRead(true);
9796
notification = notificationRepository.save(notification);
9897

99-
// Kafka 이벤트 발행 (UPDATED)
100-
NotificationEvent event = new NotificationEvent(notification, NotificationEventType.UPDATED);
101-
kafkaTemplate.send(NotificationEventType.UPDATED.getTopic(), event);
102-
10398
return NotificationDto.fromEntity(notification);
10499
}
105100

@@ -113,9 +108,9 @@ public void deleteNotification(Long notificationId) {
113108

114109
notificationRepository.delete(notification);
115110

116-
// Kafka 이벤트 발행 (DELETED)
117-
NotificationEvent event = new NotificationEvent(notification, NotificationEventType.DELETED);
118-
kafkaTemplate.send(NotificationEventType.DELETED.getTopic(), event);
111+
// // Kafka 이벤트 발행 (DELETED)
112+
// NotificationEvent event = new NotificationEvent(notification, NotificationEventType.DELETED);
113+
// kafkaTemplate.send(NotificationEventType.DELETED.getTopic(), event);
119114
}
120115

121116
/**
@@ -131,4 +126,12 @@ public List<NotificationDto> getNotificationsByUser(Long userId) {
131126
.map(NotificationDto::fromEntity)
132127
.collect(Collectors.toList());
133128
}
129+
130+
public void sendMessage(SlackWebHookMessage message, String email) {
131+
//채널 알림
132+
slackClient.sendMessage(message);
133+
134+
//사용자 DM
135+
slackClient.sendMessage(SlackBotMessage.of(slackClient.getUserIdByEmail(email), message.getText()));
136+
}
134137
}

notification-service/src/main/java/com/timebank/notification_service/domain/entity/Notification.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.timebank.notification_service.domain.entity;
22

33
import com.timebank.common.domain.Timestamped;
4+
import com.timebank.common.infrastructure.external.notification.dto.NotificationEventType;
5+
import com.timebank.common.infrastructure.external.notification.dto.NotificationType;
46

57
import jakarta.persistence.Column;
68
import jakarta.persistence.Entity;

0 commit comments

Comments
 (0)