From a7f9cd0e383a4cbe94bbed5e9b7da3ba5c827022 Mon Sep 17 00:00:00 2001 From: nowgnas Date: Mon, 25 Dec 2023 01:50:03 +0900 Subject: [PATCH 1/2] :bug: Fix new order event notification --- build.gradle | 2 +- .../common/dto/NewOrderEvent.java | 77 +++++++++++++++++++ .../helper/NotificationActionHelper.java | 5 +- .../message/NotificationSQSListener.java | 42 ++++++---- 4 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 src/main/java/kr/bb/notification/common/dto/NewOrderEvent.java diff --git a/build.gradle b/build.gradle index 8ea3aec..97098df 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ dependencies { implementation 'org.springframework.cloud:spring-cloud-aws-messaging:2.2.4.RELEASE' implementation 'org.springframework.kafka:spring-kafka' implementation 'software.amazon.awssdk:sns:2.21.37' - implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312220850' + implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312241539' testImplementation 'org.mockito:mockito-core:4.8.0' diff --git a/src/main/java/kr/bb/notification/common/dto/NewOrderEvent.java b/src/main/java/kr/bb/notification/common/dto/NewOrderEvent.java new file mode 100644 index 0000000..7b7cf13 --- /dev/null +++ b/src/main/java/kr/bb/notification/common/dto/NewOrderEvent.java @@ -0,0 +1,77 @@ +package kr.bb.notification.common.dto; + +import bloomingblooms.domain.notification.NotificationKind; +import bloomingblooms.domain.notification.NotificationURL; +import bloomingblooms.domain.notification.order.OrderType; +import java.util.List; +import java.util.stream.Collectors; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class NewOrderEvent { + private List orders; + + public static List getProductDataForUpdateSaleCount(NewOrderEvent newOrderEvent) { + return newOrderEvent.getOrders().stream() + .flatMap(item -> item.getProducts().stream()) + .map( + productCount -> + ProductCount.builder() + .productId(productCount.getProductId()) + .quantity(productCount.getQuantity()) + .build()) + .collect(Collectors.toList()); + } + + @Getter + @Builder + @NoArgsConstructor(access = AccessLevel.PROTECTED) + @AllArgsConstructor + public static class NewOrderEventItem { + private Long orderId; + private String productName; + private Long storeId; + private OrderType orderType; + private List products; + + public static NotificationURL getNotificationURL(OrderType orderType) { + switch (orderType) { + case PICKUP: + return NotificationURL.ORDER_DELIVERY; + case DELIVERY: + return NotificationURL.DELIVERY; + case SUBSCRIBE: + return NotificationURL.ORDER_SCHEDULE; + } + return null; + } + + public static NotificationKind getNotificationKind(OrderType orderType) { + switch (orderType) { + case DELIVERY: + return NotificationKind.SHOPPINGMALL; + case SUBSCRIBE: + return NotificationKind.SUBSCRIBE; + case PICKUP: + return NotificationKind.PICKUP; + } + return null; + } + } + + @Getter + @Builder + @NoArgsConstructor(access = AccessLevel.PROTECTED) + @AllArgsConstructor + public static class ProductCount { + private String productId; + private Long quantity; + } +} diff --git a/src/main/java/kr/bb/notification/domain/notification/helper/NotificationActionHelper.java b/src/main/java/kr/bb/notification/domain/notification/helper/NotificationActionHelper.java index 7ac9748..9a64eda 100644 --- a/src/main/java/kr/bb/notification/domain/notification/helper/NotificationActionHelper.java +++ b/src/main/java/kr/bb/notification/domain/notification/helper/NotificationActionHelper.java @@ -2,7 +2,6 @@ import bloomingblooms.domain.notification.NotificationData; import bloomingblooms.domain.notification.delivery.DeliveryNotification; -import bloomingblooms.domain.notification.order.NewOrderNotification; import bloomingblooms.domain.notification.order.OrderCancelNotification; import bloomingblooms.domain.notification.order.SettlementNotification; import bloomingblooms.domain.notification.question.InqueryResponseNotification; @@ -10,9 +9,9 @@ import bloomingblooms.domain.notification.stock.OutOfStockNotification; import bloomingblooms.domain.resale.ResaleNotificationList; import java.util.List; +import kr.bb.notification.common.dto.NewOrderEvent.NewOrderEventItem; import kr.bb.notification.domain.notification.application.NotificationCommandService; import kr.bb.notification.domain.notification.entity.NotificationCommand.NotificationInformation; - import kr.bb.notification.domain.notification.infrastructure.sms.SendSMS; import kr.bb.notification.domain.notification.infrastructure.sse.SendSSE; import lombok.RequiredArgsConstructor; @@ -60,7 +59,7 @@ public void publishNewComerNotification(NotificationData notification) { notificationCommandService.saveSingleNotification(notification.getPublishInformation(), 1L); } - public void publishNewOrderNotification(NotificationData notification) { + public void publishNewOrderNotification(NotificationData notification) { NotificationInformation sseNotification = NotificationInformation.getSSEData( notification.getPublishInformation(), notification.getWhoToNotify().getStoreId()); diff --git a/src/main/java/kr/bb/notification/domain/notification/infrastructure/message/NotificationSQSListener.java b/src/main/java/kr/bb/notification/domain/notification/infrastructure/message/NotificationSQSListener.java index 9c72b3e..c79162d 100644 --- a/src/main/java/kr/bb/notification/domain/notification/infrastructure/message/NotificationSQSListener.java +++ b/src/main/java/kr/bb/notification/domain/notification/infrastructure/message/NotificationSQSListener.java @@ -5,7 +5,6 @@ import bloomingblooms.domain.notification.Role; import bloomingblooms.domain.notification.delivery.DeliveryNotification; import bloomingblooms.domain.notification.newcomer.NewcomerNotification; -import bloomingblooms.domain.notification.order.NewOrderNotification; import bloomingblooms.domain.notification.order.OrderCancelNotification; import bloomingblooms.domain.notification.order.SettlementNotification; import bloomingblooms.domain.notification.question.InqueryResponseNotification; @@ -13,11 +12,14 @@ import bloomingblooms.domain.notification.stock.OutOfStockNotification; import bloomingblooms.domain.resale.ResaleNotificationList; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; +import kr.bb.notification.common.dto.NewOrderEvent; +import kr.bb.notification.common.dto.NewOrderEvent.NewOrderEventItem; import kr.bb.notification.domain.notification.helper.NotificationActionHelper; - import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.aws.messaging.listener.Acknowledgment; import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy; import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener; @@ -25,6 +27,7 @@ import org.springframework.messaging.handler.annotation.Payload; import org.springframework.stereotype.Service; +@Slf4j @Service @RequiredArgsConstructor public class NotificationSQSListener { @@ -107,25 +110,32 @@ public void consumeQuestionRegisterNotificationQueue( public void consumeNewOrderNotificationQueue( @Payload String message, @Headers Map headers, Acknowledgment ack) throws JsonProcessingException { - // TODO: 주문 완료 후 타입 정의해서 보내주기 kind - NotificationData newOrderNotification = - objectMapper.readValue( - message, - objectMapper - .getTypeFactory() - .constructParametricType(NotificationData.class, NewOrderNotification.class)); - NotificationData notification = - NotificationData.notifyData( - newOrderNotification.getWhoToNotify(), - PublishNotificationInformation.updateRole( - newOrderNotification.getPublishInformation(), Role.MANAGER)); + // sns 에서 발생한 이벤트 sqs 에서 받기 + String messageFromSNS = getMessageFromSNS(message); + NewOrderEvent newOrderEvent = objectMapper.readValue(messageFromSNS, NewOrderEvent.class); - // call facade - notificationActionHelper.publishNewOrderNotification(notification); + newOrderEvent + .getOrders() + .forEach( + item -> { + NotificationData notificationData = + NotificationData.notifyData( + item, + PublishNotificationInformation.makePublishNotificationInformation( + NewOrderEventItem.getNotificationURL(item.getOrderType()), + NewOrderEventItem.getNotificationKind(item.getOrderType()), + Role.MANAGER)); + notificationActionHelper.publishNewOrderNotification(notificationData); + }); ack.acknowledge(); } + private String getMessageFromSNS(String message) throws JsonProcessingException { + JsonNode jsonNode = objectMapper.readTree(message); + return jsonNode.get("Message").asText(); + } + /** * 신규 회원 가입 심사 알림 * From 23227dad41748762566a58e5bf708ba937f75ce1 Mon Sep 17 00:00:00 2001 From: nowgnas Date: Tue, 26 Dec 2023 16:23:10 +0900 Subject: [PATCH 2/2] :bug: Fix test fail --- build.gradle | 2 +- .../common/dto/NewOrderEvent.java | 77 ------------------- .../helper/NotificationActionHelper.java | 2 +- .../message/NotificationSQSListener.java | 4 +- src/test/resources/application.yml | 2 + 5 files changed, 6 insertions(+), 81 deletions(-) delete mode 100644 src/main/java/kr/bb/notification/common/dto/NewOrderEvent.java diff --git a/build.gradle b/build.gradle index 97098df..4b61c92 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ dependencies { implementation 'org.springframework.cloud:spring-cloud-aws-messaging:2.2.4.RELEASE' implementation 'org.springframework.kafka:spring-kafka' implementation 'software.amazon.awssdk:sns:2.21.37' - implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312241539' + implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312260649' testImplementation 'org.mockito:mockito-core:4.8.0' diff --git a/src/main/java/kr/bb/notification/common/dto/NewOrderEvent.java b/src/main/java/kr/bb/notification/common/dto/NewOrderEvent.java deleted file mode 100644 index 7b7cf13..0000000 --- a/src/main/java/kr/bb/notification/common/dto/NewOrderEvent.java +++ /dev/null @@ -1,77 +0,0 @@ -package kr.bb.notification.common.dto; - -import bloomingblooms.domain.notification.NotificationKind; -import bloomingblooms.domain.notification.NotificationURL; -import bloomingblooms.domain.notification.order.OrderType; -import java.util.List; -import java.util.stream.Collectors; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@Builder -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor -public class NewOrderEvent { - private List orders; - - public static List getProductDataForUpdateSaleCount(NewOrderEvent newOrderEvent) { - return newOrderEvent.getOrders().stream() - .flatMap(item -> item.getProducts().stream()) - .map( - productCount -> - ProductCount.builder() - .productId(productCount.getProductId()) - .quantity(productCount.getQuantity()) - .build()) - .collect(Collectors.toList()); - } - - @Getter - @Builder - @NoArgsConstructor(access = AccessLevel.PROTECTED) - @AllArgsConstructor - public static class NewOrderEventItem { - private Long orderId; - private String productName; - private Long storeId; - private OrderType orderType; - private List products; - - public static NotificationURL getNotificationURL(OrderType orderType) { - switch (orderType) { - case PICKUP: - return NotificationURL.ORDER_DELIVERY; - case DELIVERY: - return NotificationURL.DELIVERY; - case SUBSCRIBE: - return NotificationURL.ORDER_SCHEDULE; - } - return null; - } - - public static NotificationKind getNotificationKind(OrderType orderType) { - switch (orderType) { - case DELIVERY: - return NotificationKind.SHOPPINGMALL; - case SUBSCRIBE: - return NotificationKind.SUBSCRIBE; - case PICKUP: - return NotificationKind.PICKUP; - } - return null; - } - } - - @Getter - @Builder - @NoArgsConstructor(access = AccessLevel.PROTECTED) - @AllArgsConstructor - public static class ProductCount { - private String productId; - private Long quantity; - } -} diff --git a/src/main/java/kr/bb/notification/domain/notification/helper/NotificationActionHelper.java b/src/main/java/kr/bb/notification/domain/notification/helper/NotificationActionHelper.java index 9a64eda..697a392 100644 --- a/src/main/java/kr/bb/notification/domain/notification/helper/NotificationActionHelper.java +++ b/src/main/java/kr/bb/notification/domain/notification/helper/NotificationActionHelper.java @@ -7,9 +7,9 @@ import bloomingblooms.domain.notification.question.InqueryResponseNotification; import bloomingblooms.domain.notification.question.QuestionRegister; import bloomingblooms.domain.notification.stock.OutOfStockNotification; +import bloomingblooms.domain.order.NewOrderEvent.NewOrderEventItem; import bloomingblooms.domain.resale.ResaleNotificationList; import java.util.List; -import kr.bb.notification.common.dto.NewOrderEvent.NewOrderEventItem; import kr.bb.notification.domain.notification.application.NotificationCommandService; import kr.bb.notification.domain.notification.entity.NotificationCommand.NotificationInformation; import kr.bb.notification.domain.notification.infrastructure.sms.SendSMS; diff --git a/src/main/java/kr/bb/notification/domain/notification/infrastructure/message/NotificationSQSListener.java b/src/main/java/kr/bb/notification/domain/notification/infrastructure/message/NotificationSQSListener.java index c79162d..0531856 100644 --- a/src/main/java/kr/bb/notification/domain/notification/infrastructure/message/NotificationSQSListener.java +++ b/src/main/java/kr/bb/notification/domain/notification/infrastructure/message/NotificationSQSListener.java @@ -10,13 +10,13 @@ import bloomingblooms.domain.notification.question.InqueryResponseNotification; import bloomingblooms.domain.notification.question.QuestionRegister; import bloomingblooms.domain.notification.stock.OutOfStockNotification; +import bloomingblooms.domain.order.NewOrderEvent; +import bloomingblooms.domain.order.NewOrderEvent.NewOrderEventItem; import bloomingblooms.domain.resale.ResaleNotificationList; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; -import kr.bb.notification.common.dto.NewOrderEvent; -import kr.bb.notification.common.dto.NewOrderEvent.NewOrderEventItem; import kr.bb.notification.domain.notification.helper.NotificationActionHelper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 1f558dc..46077a0 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -33,6 +33,8 @@ cloud: name: "" sns: arn: "" + new-order-event: + arn: "" credentials: ACCESS_KEY_ID: "test" SECRET_ACCESS_KEY: "test"