diff --git a/src/main/java/clap/server/adapter/outbound/api/AgitClient.java b/src/main/java/clap/server/adapter/outbound/api/AgitClient.java index 4938aeb1..6435e014 100644 --- a/src/main/java/clap/server/adapter/outbound/api/AgitClient.java +++ b/src/main/java/clap/server/adapter/outbound/api/AgitClient.java @@ -2,12 +2,10 @@ import clap.server.adapter.outbound.api.dto.PushNotificationTemplate; import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType; -import clap.server.application.port.inbound.domain.TaskService; import clap.server.application.port.outbound.webhook.SendAgitPort; -import clap.server.application.service.task.UpdateTaskService; import clap.server.common.annotation.architecture.ExternalApiAdapter; import clap.server.domain.model.task.Task; -import clap.server.exception.ApplicationException; +import clap.server.exception.AdapterException; import clap.server.exception.code.NotificationErrorCode; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -29,10 +27,9 @@ public class AgitClient implements SendAgitPort { private final AgitTemplateBuilder agitTemplateBuilder; private final ObjectMapper objectMapper; - private final TaskService taskService; @Override - public void sendAgit(PushNotificationTemplate request, Task task) { + public Long sendAgit(PushNotificationTemplate request, Task task) { HttpEntity entity = agitTemplateBuilder.createAgitEntity(request, task); @@ -40,20 +37,20 @@ public void sendAgit(PushNotificationTemplate request, Task task) { if (request.notificationType() == NotificationType.TASK_REQUESTED) { ResponseEntity responseEntity = restTemplate.exchange( AGIT_WEBHOOK_URL, HttpMethod.POST, entity, String.class); - updateAgitPostId(responseEntity, task); + return getAgitPostId(responseEntity); } else { restTemplate.exchange(AGIT_WEBHOOK_URL, HttpMethod.POST, entity, String.class); + return null; } } - private void updateAgitPostId(ResponseEntity responseEntity, Task task) { + private Long getAgitPostId(ResponseEntity responseEntity) { try { JsonNode jsonNode = objectMapper.readTree(responseEntity.getBody()); - task.updateAgitPostId(jsonNode.get("id").asLong()); - taskService.upsert(task); + return jsonNode.get("id").asLong(); } catch (JsonProcessingException e) { - throw new ApplicationException(NotificationErrorCode.AGIT_SEND_FAILED); + throw new AdapterException(NotificationErrorCode.AGIT_SEND_FAILED); } } } diff --git a/src/main/java/clap/server/adapter/outbound/infrastructure/sse/SendSseService.java b/src/main/java/clap/server/adapter/outbound/infrastructure/sse/SendSseService.java new file mode 100644 index 00000000..5785653e --- /dev/null +++ b/src/main/java/clap/server/adapter/outbound/infrastructure/sse/SendSseService.java @@ -0,0 +1,29 @@ +package clap.server.adapter.outbound.infrastructure.sse; + +import clap.server.adapter.inbound.web.dto.notification.request.SseRequest; +import clap.server.adapter.outbound.infrastructure.sse.repository.EmitterRepository; +import clap.server.application.port.outbound.webhook.SendSsePort; +import clap.server.common.annotation.architecture.InfrastructureAdapter; +import clap.server.exception.AdapterException; +import clap.server.exception.code.NotificationErrorCode; +import lombok.RequiredArgsConstructor; +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; + +@InfrastructureAdapter +@RequiredArgsConstructor +public class SendSseService implements SendSsePort { + + private final EmitterRepository emitterRepository; + + @Override + public void send(SseRequest request) { + SseEmitter sseEmitter = emitterRepository.get(request.receiverId()); + try { + sseEmitter.send(SseEmitter.event() + .id(String.valueOf(request.receiverId())) + .data(request)); + } catch (Exception e) { + throw new AdapterException(NotificationErrorCode.SSE_SEND_FAILED); + } + } +} diff --git a/src/main/java/clap/server/adapter/outbound/infrastructure/sse/SseAdapter.java b/src/main/java/clap/server/adapter/outbound/infrastructure/sse/SseAdapter.java index 3be28ab2..e0ebd34d 100644 --- a/src/main/java/clap/server/adapter/outbound/infrastructure/sse/SseAdapter.java +++ b/src/main/java/clap/server/adapter/outbound/infrastructure/sse/SseAdapter.java @@ -2,14 +2,13 @@ import clap.server.adapter.outbound.infrastructure.sse.repository.EmitterRepository; import clap.server.application.port.outbound.notification.CommandSsePort; -import clap.server.application.port.outbound.notification.LoadSsePort; import clap.server.common.annotation.architecture.InfrastructureAdapter; import lombok.RequiredArgsConstructor; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; @InfrastructureAdapter @RequiredArgsConstructor -public class SseAdapter implements LoadSsePort, CommandSsePort { +public class SseAdapter implements CommandSsePort { private final EmitterRepository emitterRepository; @@ -22,9 +21,4 @@ public void save(Long receiverId, SseEmitter emitter) { public void delete(Long receiverId) { emitterRepository.delete(receiverId); } - - @Override - public SseEmitter get(Long receiverId) { - return emitterRepository.get(receiverId); - } } diff --git a/src/main/java/clap/server/application/port/outbound/webhook/SendAgitPort.java b/src/main/java/clap/server/application/port/outbound/webhook/SendAgitPort.java index 5d9005e4..9a0f0b3c 100644 --- a/src/main/java/clap/server/application/port/outbound/webhook/SendAgitPort.java +++ b/src/main/java/clap/server/application/port/outbound/webhook/SendAgitPort.java @@ -4,5 +4,5 @@ import clap.server.domain.model.task.Task; public interface SendAgitPort { - void sendAgit(PushNotificationTemplate request, Task task); + Long sendAgit(PushNotificationTemplate request, Task task); } diff --git a/src/main/java/clap/server/application/port/inbound/notification/SendSseUsecase.java b/src/main/java/clap/server/application/port/outbound/webhook/SendSsePort.java similarity index 55% rename from src/main/java/clap/server/application/port/inbound/notification/SendSseUsecase.java rename to src/main/java/clap/server/application/port/outbound/webhook/SendSsePort.java index 985dc2e2..5d38852e 100644 --- a/src/main/java/clap/server/application/port/inbound/notification/SendSseUsecase.java +++ b/src/main/java/clap/server/application/port/outbound/webhook/SendSsePort.java @@ -1,8 +1,8 @@ -package clap.server.application.port.inbound.notification; +package clap.server.application.port.outbound.webhook; import clap.server.adapter.inbound.web.dto.notification.request.SseRequest; -public interface SendSseUsecase { +public interface SendSsePort { void send(SseRequest request); } diff --git a/src/main/java/clap/server/application/service/history/PostCommentService.java b/src/main/java/clap/server/application/service/history/PostCommentService.java index ec60baee..2e3b6a70 100644 --- a/src/main/java/clap/server/application/service/history/PostCommentService.java +++ b/src/main/java/clap/server/application/service/history/PostCommentService.java @@ -4,22 +4,22 @@ import clap.server.adapter.outbound.persistense.entity.member.constant.MemberRole; import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType; import clap.server.adapter.outbound.persistense.entity.task.constant.TaskHistoryType; -import clap.server.application.port.inbound.history.SaveCommentAttachmentUsecase; -import clap.server.application.port.inbound.history.SaveCommentUsecase; import clap.server.application.port.inbound.domain.MemberService; import clap.server.application.port.inbound.domain.TaskService; +import clap.server.application.port.inbound.history.SaveCommentAttachmentUsecase; +import clap.server.application.port.inbound.history.SaveCommentUsecase; import clap.server.application.port.outbound.s3.S3UploadPort; import clap.server.application.port.outbound.task.CommandAttachmentPort; import clap.server.application.port.outbound.task.CommandCommentPort; import clap.server.application.port.outbound.taskhistory.CommandTaskHistoryPort; import clap.server.application.service.webhook.SendNotificationService; import clap.server.common.annotation.architecture.ApplicationService; -import clap.server.domain.policy.attachment.FilePathPolicy; import clap.server.domain.model.member.Member; import clap.server.domain.model.task.Attachment; import clap.server.domain.model.task.Comment; import clap.server.domain.model.task.Task; import clap.server.domain.model.task.TaskHistory; +import clap.server.domain.policy.attachment.FilePathPolicy; import lombok.RequiredArgsConstructor; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; diff --git a/src/main/java/clap/server/application/service/webhook/NotificationSender.java b/src/main/java/clap/server/application/service/webhook/NotificationSender.java new file mode 100644 index 00000000..0591d323 --- /dev/null +++ b/src/main/java/clap/server/application/service/webhook/NotificationSender.java @@ -0,0 +1,7 @@ +package clap.server.application.service.webhook; + +import clap.server.adapter.outbound.api.dto.PushNotificationTemplate; + +public interface NotificationSender { + void send(PushNotificationTemplate template); +} \ No newline at end of file diff --git a/src/main/java/clap/server/application/service/webhook/SendAgitService.java b/src/main/java/clap/server/application/service/webhook/SendAgitService.java index 905e343a..d22a7f79 100644 --- a/src/main/java/clap/server/application/service/webhook/SendAgitService.java +++ b/src/main/java/clap/server/application/service/webhook/SendAgitService.java @@ -1,18 +1,26 @@ package clap.server.application.service.webhook; import clap.server.adapter.outbound.api.dto.PushNotificationTemplate; +import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType; +import clap.server.application.port.inbound.domain.TaskService; import clap.server.application.port.outbound.webhook.SendAgitPort; -import clap.server.common.annotation.architecture.ApplicationService; import clap.server.domain.model.task.Task; import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; -@ApplicationService +@Service @RequiredArgsConstructor public class SendAgitService { private final SendAgitPort agitPort; + private final TaskService taskService; public void sendAgit(PushNotificationTemplate request, Task task) { - agitPort.sendAgit(request, task); + Long agitPostId = agitPort.sendAgit(request, task); + + if (request.notificationType().equals(NotificationType.TASK_REQUESTED)) { + task.updateAgitPostId(agitPostId); + taskService.upsert(task); + } } } diff --git a/src/main/java/clap/server/application/service/webhook/SendKaKaoWorkService.java b/src/main/java/clap/server/application/service/webhook/SendKaKaoWorkService.java index 6a193093..a341c1d0 100644 --- a/src/main/java/clap/server/application/service/webhook/SendKaKaoWorkService.java +++ b/src/main/java/clap/server/application/service/webhook/SendKaKaoWorkService.java @@ -2,16 +2,16 @@ import clap.server.adapter.outbound.api.dto.PushNotificationTemplate; import clap.server.application.port.outbound.webhook.SendKaKaoWorkPort; -import clap.server.common.annotation.architecture.ApplicationService; import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; -@ApplicationService +@Service @RequiredArgsConstructor -public class SendKaKaoWorkService { +public class SendKaKaoWorkService implements NotificationSender { private final SendKaKaoWorkPort sendKaKaoWorkPort; - public void sendKaKaoWork(PushNotificationTemplate request) { + public void send(PushNotificationTemplate request) { sendKaKaoWorkPort.sendKakaoWork(request); } } diff --git a/src/main/java/clap/server/application/service/webhook/SendNotificationService.java b/src/main/java/clap/server/application/service/webhook/SendNotificationService.java index 169f40c1..e28203fe 100644 --- a/src/main/java/clap/server/application/service/webhook/SendNotificationService.java +++ b/src/main/java/clap/server/application/service/webhook/SendNotificationService.java @@ -4,6 +4,7 @@ import clap.server.adapter.outbound.api.dto.PushNotificationTemplate; import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType; import clap.server.application.port.outbound.notification.CommandNotificationPort; +import clap.server.application.port.outbound.webhook.SendSsePort; import clap.server.common.annotation.architecture.ApplicationService; import clap.server.domain.model.member.Member; import clap.server.domain.model.notification.Notification; @@ -12,13 +13,14 @@ import org.springframework.scheduling.annotation.Async; import java.util.concurrent.CompletableFuture; + import static clap.server.domain.model.notification.Notification.createTaskNotification; @ApplicationService @RequiredArgsConstructor public class SendNotificationService { - private final SendSseService sendSseService; + private final SendSsePort sendSsePort; private final SendAgitService sendAgitService; private final SendWebhookEmailService sendWebhookEmailService; private final SendKaKaoWorkService sendKaKaoWorkService; @@ -49,18 +51,18 @@ public void sendPushNotification(Member receiver, NotificationType notificationT }); CompletableFuture sendSseFuture = CompletableFuture.runAsync(() -> { - sendSseService.send(sseRequest); + sendSsePort.send(sseRequest); }); CompletableFuture sendEmailFuture = CompletableFuture.runAsync(() -> { if (receiver.getEmailNotificationEnabled()) { - sendWebhookEmailService.sendEmail(pushNotificationTemplate); + sendWebhookEmailService.send(pushNotificationTemplate); } }); CompletableFuture sendKakaoWorkFuture = CompletableFuture.runAsync(() -> { if (receiver.getKakaoworkNotificationEnabled()) { - sendKaKaoWorkService.sendKaKaoWork(pushNotificationTemplate); + sendKaKaoWorkService.send(pushNotificationTemplate); } }); @@ -80,7 +82,6 @@ public void sendAgitNotification(NotificationType notificationType, message, commenterName ); - sendAgitService.sendAgit(pushNotificationTemplate, task); } } diff --git a/src/main/java/clap/server/application/service/webhook/SendSseService.java b/src/main/java/clap/server/application/service/webhook/SendSseService.java deleted file mode 100644 index a50bba72..00000000 --- a/src/main/java/clap/server/application/service/webhook/SendSseService.java +++ /dev/null @@ -1,29 +0,0 @@ -package clap.server.application.service.webhook; - -import clap.server.adapter.inbound.web.dto.notification.request.SseRequest; -import clap.server.application.port.inbound.notification.SendSseUsecase; -import clap.server.application.port.outbound.notification.LoadSsePort; -import clap.server.common.annotation.architecture.ApplicationService; -import clap.server.exception.ApplicationException; -import clap.server.exception.code.NotificationErrorCode; -import lombok.RequiredArgsConstructor; -import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; - -@ApplicationService -@RequiredArgsConstructor -public class SendSseService implements SendSseUsecase { - - private final LoadSsePort loadSsePort; - - @Override - public void send(SseRequest request) { - SseEmitter sseEmitter = loadSsePort.get(request.receiverId()); - try { - sseEmitter.send(SseEmitter.event() - .id(String.valueOf(request.receiverId())) - .data(request)); - } catch (Exception e) { - throw new ApplicationException(NotificationErrorCode.SSE_SEND_FAILED); - } - } -} diff --git a/src/main/java/clap/server/application/service/webhook/SendWebhookEmailService.java b/src/main/java/clap/server/application/service/webhook/SendWebhookEmailService.java index 1ea80790..ee7fd241 100644 --- a/src/main/java/clap/server/application/service/webhook/SendWebhookEmailService.java +++ b/src/main/java/clap/server/application/service/webhook/SendWebhookEmailService.java @@ -2,16 +2,16 @@ import clap.server.adapter.outbound.api.dto.PushNotificationTemplate; import clap.server.application.port.outbound.webhook.SendWebhookEmailPort; -import clap.server.common.annotation.architecture.ApplicationService; import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; -@ApplicationService +@Service @RequiredArgsConstructor -public class SendWebhookEmailService { +public class SendWebhookEmailService implements NotificationSender { private final SendWebhookEmailPort sendWebhookEmailPort; - public void sendEmail(PushNotificationTemplate request) { + public void send(PushNotificationTemplate request) { sendWebhookEmailPort.sendWebhookEmail(request); } }