Skip to content

Commit d442aae

Browse files
authored
Merge pull request #351 from TaskFlow-CLAP/CLAP-297
CLAP-297 웹훅 관련 applicaton layer 리팩토링
2 parents 158bed9 + 803c3d4 commit d442aae

File tree

12 files changed

+75
-68
lines changed

12 files changed

+75
-68
lines changed

src/main/java/clap/server/adapter/outbound/api/AgitClient.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import clap.server.adapter.outbound.api.dto.PushNotificationTemplate;
44
import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType;
5-
import clap.server.application.port.inbound.domain.TaskService;
65
import clap.server.application.port.outbound.webhook.SendAgitPort;
7-
import clap.server.application.service.task.UpdateTaskService;
86
import clap.server.common.annotation.architecture.ExternalApiAdapter;
97
import clap.server.domain.model.task.Task;
10-
import clap.server.exception.ApplicationException;
8+
import clap.server.exception.AdapterException;
119
import clap.server.exception.code.NotificationErrorCode;
1210
import com.fasterxml.jackson.core.JsonProcessingException;
1311
import com.fasterxml.jackson.databind.JsonNode;
@@ -29,31 +27,30 @@ public class AgitClient implements SendAgitPort {
2927

3028
private final AgitTemplateBuilder agitTemplateBuilder;
3129
private final ObjectMapper objectMapper;
32-
private final TaskService taskService;
3330

3431
@Override
35-
public void sendAgit(PushNotificationTemplate request, Task task) {
32+
public Long sendAgit(PushNotificationTemplate request, Task task) {
3633

3734
HttpEntity<String> entity = agitTemplateBuilder.createAgitEntity(request, task);
3835

3936
RestTemplate restTemplate = new RestTemplate();
4037
if (request.notificationType() == NotificationType.TASK_REQUESTED) {
4138
ResponseEntity<String> responseEntity = restTemplate.exchange(
4239
AGIT_WEBHOOK_URL, HttpMethod.POST, entity, String.class);
43-
updateAgitPostId(responseEntity, task);
40+
return getAgitPostId(responseEntity);
4441
}
4542
else {
4643
restTemplate.exchange(AGIT_WEBHOOK_URL, HttpMethod.POST, entity, String.class);
44+
return null;
4745
}
4846
}
4947

50-
private void updateAgitPostId(ResponseEntity<String> responseEntity, Task task) {
48+
private Long getAgitPostId(ResponseEntity<String> responseEntity) {
5149
try {
5250
JsonNode jsonNode = objectMapper.readTree(responseEntity.getBody());
53-
task.updateAgitPostId(jsonNode.get("id").asLong());
54-
taskService.upsert(task);
51+
return jsonNode.get("id").asLong();
5552
} catch (JsonProcessingException e) {
56-
throw new ApplicationException(NotificationErrorCode.AGIT_SEND_FAILED);
53+
throw new AdapterException(NotificationErrorCode.AGIT_SEND_FAILED);
5754
}
5855
}
5956
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package clap.server.adapter.outbound.infrastructure.sse;
2+
3+
import clap.server.adapter.inbound.web.dto.notification.request.SseRequest;
4+
import clap.server.adapter.outbound.infrastructure.sse.repository.EmitterRepository;
5+
import clap.server.application.port.outbound.webhook.SendSsePort;
6+
import clap.server.common.annotation.architecture.InfrastructureAdapter;
7+
import clap.server.exception.AdapterException;
8+
import clap.server.exception.code.NotificationErrorCode;
9+
import lombok.RequiredArgsConstructor;
10+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
11+
12+
@InfrastructureAdapter
13+
@RequiredArgsConstructor
14+
public class SendSseService implements SendSsePort {
15+
16+
private final EmitterRepository emitterRepository;
17+
18+
@Override
19+
public void send(SseRequest request) {
20+
SseEmitter sseEmitter = emitterRepository.get(request.receiverId());
21+
try {
22+
sseEmitter.send(SseEmitter.event()
23+
.id(String.valueOf(request.receiverId()))
24+
.data(request));
25+
} catch (Exception e) {
26+
throw new AdapterException(NotificationErrorCode.SSE_SEND_FAILED);
27+
}
28+
}
29+
}

src/main/java/clap/server/adapter/outbound/infrastructure/sse/SseAdapter.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import clap.server.adapter.outbound.infrastructure.sse.repository.EmitterRepository;
44
import clap.server.application.port.outbound.notification.CommandSsePort;
5-
import clap.server.application.port.outbound.notification.LoadSsePort;
65
import clap.server.common.annotation.architecture.InfrastructureAdapter;
76
import lombok.RequiredArgsConstructor;
87
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
98

109
@InfrastructureAdapter
1110
@RequiredArgsConstructor
12-
public class SseAdapter implements LoadSsePort, CommandSsePort {
11+
public class SseAdapter implements CommandSsePort {
1312

1413
private final EmitterRepository emitterRepository;
1514

@@ -22,9 +21,4 @@ public void save(Long receiverId, SseEmitter emitter) {
2221
public void delete(Long receiverId) {
2322
emitterRepository.delete(receiverId);
2423
}
25-
26-
@Override
27-
public SseEmitter get(Long receiverId) {
28-
return emitterRepository.get(receiverId);
29-
}
3024
}

src/main/java/clap/server/application/port/outbound/webhook/SendAgitPort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
import clap.server.domain.model.task.Task;
55

66
public interface SendAgitPort {
7-
void sendAgit(PushNotificationTemplate request, Task task);
7+
Long sendAgit(PushNotificationTemplate request, Task task);
88
}

src/main/java/clap/server/application/port/inbound/notification/SendSseUsecase.java renamed to src/main/java/clap/server/application/port/outbound/webhook/SendSsePort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package clap.server.application.port.inbound.notification;
1+
package clap.server.application.port.outbound.webhook;
22

33
import clap.server.adapter.inbound.web.dto.notification.request.SseRequest;
44

5-
public interface SendSseUsecase {
5+
public interface SendSsePort {
66

77
void send(SseRequest request);
88
}

src/main/java/clap/server/application/service/history/PostCommentService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
import clap.server.adapter.outbound.persistense.entity.member.constant.MemberRole;
55
import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType;
66
import clap.server.adapter.outbound.persistense.entity.task.constant.TaskHistoryType;
7-
import clap.server.application.port.inbound.history.SaveCommentAttachmentUsecase;
8-
import clap.server.application.port.inbound.history.SaveCommentUsecase;
97
import clap.server.application.port.inbound.domain.MemberService;
108
import clap.server.application.port.inbound.domain.TaskService;
9+
import clap.server.application.port.inbound.history.SaveCommentAttachmentUsecase;
10+
import clap.server.application.port.inbound.history.SaveCommentUsecase;
1111
import clap.server.application.port.outbound.s3.S3UploadPort;
1212
import clap.server.application.port.outbound.task.CommandAttachmentPort;
1313
import clap.server.application.port.outbound.task.CommandCommentPort;
1414
import clap.server.application.port.outbound.taskhistory.CommandTaskHistoryPort;
1515
import clap.server.application.service.webhook.SendNotificationService;
1616
import clap.server.common.annotation.architecture.ApplicationService;
17-
import clap.server.domain.policy.attachment.FilePathPolicy;
1817
import clap.server.domain.model.member.Member;
1918
import clap.server.domain.model.task.Attachment;
2019
import clap.server.domain.model.task.Comment;
2120
import clap.server.domain.model.task.Task;
2221
import clap.server.domain.model.task.TaskHistory;
22+
import clap.server.domain.policy.attachment.FilePathPolicy;
2323
import lombok.RequiredArgsConstructor;
2424
import org.springframework.transaction.annotation.Transactional;
2525
import org.springframework.web.multipart.MultipartFile;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package clap.server.application.service.webhook;
2+
3+
import clap.server.adapter.outbound.api.dto.PushNotificationTemplate;
4+
5+
public interface NotificationSender {
6+
void send(PushNotificationTemplate template);
7+
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package clap.server.application.service.webhook;
22

33
import clap.server.adapter.outbound.api.dto.PushNotificationTemplate;
4+
import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType;
5+
import clap.server.application.port.inbound.domain.TaskService;
46
import clap.server.application.port.outbound.webhook.SendAgitPort;
5-
import clap.server.common.annotation.architecture.ApplicationService;
67
import clap.server.domain.model.task.Task;
78
import lombok.RequiredArgsConstructor;
9+
import org.springframework.stereotype.Service;
810

9-
@ApplicationService
11+
@Service
1012
@RequiredArgsConstructor
1113
public class SendAgitService {
1214

1315
private final SendAgitPort agitPort;
16+
private final TaskService taskService;
1417

1518
public void sendAgit(PushNotificationTemplate request, Task task) {
16-
agitPort.sendAgit(request, task);
19+
Long agitPostId = agitPort.sendAgit(request, task);
20+
21+
if (request.notificationType().equals(NotificationType.TASK_REQUESTED)) {
22+
task.updateAgitPostId(agitPostId);
23+
taskService.upsert(task);
24+
}
1725
}
1826
}

src/main/java/clap/server/application/service/webhook/SendKaKaoWorkService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import clap.server.adapter.outbound.api.dto.PushNotificationTemplate;
44
import clap.server.application.port.outbound.webhook.SendKaKaoWorkPort;
5-
import clap.server.common.annotation.architecture.ApplicationService;
65
import lombok.RequiredArgsConstructor;
6+
import org.springframework.stereotype.Service;
77

8-
@ApplicationService
8+
@Service
99
@RequiredArgsConstructor
10-
public class SendKaKaoWorkService {
10+
public class SendKaKaoWorkService implements NotificationSender {
1111

1212
private final SendKaKaoWorkPort sendKaKaoWorkPort;
1313

14-
public void sendKaKaoWork(PushNotificationTemplate request) {
14+
public void send(PushNotificationTemplate request) {
1515
sendKaKaoWorkPort.sendKakaoWork(request);
1616
}
1717
}

src/main/java/clap/server/application/service/webhook/SendNotificationService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import clap.server.adapter.outbound.api.dto.PushNotificationTemplate;
55
import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType;
66
import clap.server.application.port.outbound.notification.CommandNotificationPort;
7+
import clap.server.application.port.outbound.webhook.SendSsePort;
78
import clap.server.common.annotation.architecture.ApplicationService;
89
import clap.server.domain.model.member.Member;
910
import clap.server.domain.model.notification.Notification;
@@ -12,13 +13,14 @@
1213
import org.springframework.scheduling.annotation.Async;
1314

1415
import java.util.concurrent.CompletableFuture;
16+
1517
import static clap.server.domain.model.notification.Notification.createTaskNotification;
1618

1719
@ApplicationService
1820
@RequiredArgsConstructor
1921
public class SendNotificationService {
2022

21-
private final SendSseService sendSseService;
23+
private final SendSsePort sendSsePort;
2224
private final SendAgitService sendAgitService;
2325
private final SendWebhookEmailService sendWebhookEmailService;
2426
private final SendKaKaoWorkService sendKaKaoWorkService;
@@ -49,18 +51,18 @@ public void sendPushNotification(Member receiver, NotificationType notificationT
4951
});
5052

5153
CompletableFuture<Void> sendSseFuture = CompletableFuture.runAsync(() -> {
52-
sendSseService.send(sseRequest);
54+
sendSsePort.send(sseRequest);
5355
});
5456

5557
CompletableFuture<Void> sendEmailFuture = CompletableFuture.runAsync(() -> {
5658
if (receiver.getEmailNotificationEnabled()) {
57-
sendWebhookEmailService.sendEmail(pushNotificationTemplate);
59+
sendWebhookEmailService.send(pushNotificationTemplate);
5860
}
5961
});
6062

6163
CompletableFuture<Void> sendKakaoWorkFuture = CompletableFuture.runAsync(() -> {
6264
if (receiver.getKakaoworkNotificationEnabled()) {
63-
sendKaKaoWorkService.sendKaKaoWork(pushNotificationTemplate);
65+
sendKaKaoWorkService.send(pushNotificationTemplate);
6466
}
6567
});
6668

@@ -80,7 +82,6 @@ public void sendAgitNotification(NotificationType notificationType,
8082
message,
8183
commenterName
8284
);
83-
8485
sendAgitService.sendAgit(pushNotificationTemplate, task);
8586
}
8687
}

0 commit comments

Comments
 (0)