From 47e1d7968403a324780335f8ab3b36ee4efc144a Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 13:20:27 +0900 Subject: [PATCH 01/13] =?UTF-8?q?feat=20:=20Notification=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #5649 --- .../notification/domain/Notification.java | 37 +++++++++++++++++++ .../domain/NotificationRepository.java | 7 ++++ .../notification/domain/NotificationType.java | 14 +++++++ .../emm-sale/src/main/resources/schema.sql | 28 ++++++++++---- .../emm-sale/src/test/resources/data-test.sql | 1 + 5 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 backend/emm-sale/src/main/java/com/emmsale/notification/domain/Notification.java create mode 100644 backend/emm-sale/src/main/java/com/emmsale/notification/domain/NotificationRepository.java create mode 100644 backend/emm-sale/src/main/java/com/emmsale/notification/domain/NotificationType.java diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/domain/Notification.java b/backend/emm-sale/src/main/java/com/emmsale/notification/domain/Notification.java new file mode 100644 index 000000000..313fb2af2 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/domain/Notification.java @@ -0,0 +1,37 @@ +package com.emmsale.notification.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Notification { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Enumerated(EnumType.STRING) + @Column(name = "type", nullable = false) + private NotificationType type; + + @Column(columnDefinition = "MEDIUMTEXT") + private String jsonData; + + private boolean isRead; + + public Notification(final NotificationType type, final String jsonData) { + this.type = type; + this.jsonData = jsonData; + this.isRead = false; + } +} diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/domain/NotificationRepository.java b/backend/emm-sale/src/main/java/com/emmsale/notification/domain/NotificationRepository.java new file mode 100644 index 000000000..5f5440697 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/domain/NotificationRepository.java @@ -0,0 +1,7 @@ +package com.emmsale.notification.domain; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface NotificationRepository extends JpaRepository { + +} diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/domain/NotificationType.java b/backend/emm-sale/src/main/java/com/emmsale/notification/domain/NotificationType.java new file mode 100644 index 000000000..9c01454b8 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/domain/NotificationType.java @@ -0,0 +1,14 @@ +package com.emmsale.notification.domain; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public enum NotificationType { + + COMMENT("comment"), + EVENT("event"); + + private final String type; +} diff --git a/backend/emm-sale/src/main/resources/schema.sql b/backend/emm-sale/src/main/resources/schema.sql index 73364c08e..98a40437c 100644 --- a/backend/emm-sale/src/main/resources/schema.sql +++ b/backend/emm-sale/src/main/resources/schema.sql @@ -17,6 +17,7 @@ drop table if exists kerdy.message; drop table if exists kerdy.room; drop table if exists kerdy.feed; drop table if exists kerdy.image; +drop table if exists kerdy.notification; create table activity ( @@ -30,11 +31,11 @@ create table event id bigint auto_increment primary key, created_at datetime(6), updated_at datetime(6), - end_date datetime(6) not null, + end_date datetime(6) not null, information_url varchar(255) not null, location varchar(255) not null, name varchar(255) not null, - start_date datetime(6) not null, + start_date datetime(6) not null, image_url varchar(255), type varchar(20) not null ); @@ -60,7 +61,7 @@ create table comment is_deleted bit not null, event_id bigint not null, member_id bigint not null, - parent_id bigint null + parent_id bigint null ); create table member_activity @@ -128,7 +129,8 @@ alter table event_member add column updated_at datetime(6); -- 2023.08.08 17:04 -rename table notification TO request_notification; +rename +table notification TO request_notification; create table update_notification ( @@ -143,8 +145,8 @@ create table update_notification create table block ( id bigint auto_increment primary key, - block_member_id bigint not null, - request_member_id bigint not null, + block_member_id bigint not null, + request_member_id bigint not null, created_at datetime(6) null, updated_at datetime(6) null ); @@ -219,8 +221,8 @@ create table feed content varchar(1000) not null, event_id bigint not null, is_deleted bit not null, - created_at datetime(6) null, - updated_at datetime(6) null + created_at datetime(6) null, + updated_at datetime(6) null ); alter table comment rename column event_id to feed_id; @@ -246,3 +248,13 @@ alter table event -- 2023-09-20 20:25 alter table event add column organization varchar(50) not null; + +-- 2023-09-27 10:54 +create table notification +( + id bigint auto_increment primary key, + type varchar(20) not null, + json_data mediumtext not null, + created_at datetime(6), + is_read bit not null +) diff --git a/backend/emm-sale/src/test/resources/data-test.sql b/backend/emm-sale/src/test/resources/data-test.sql index a01c23cea..ee8870afb 100644 --- a/backend/emm-sale/src/test/resources/data-test.sql +++ b/backend/emm-sale/src/test/resources/data-test.sql @@ -17,6 +17,7 @@ truncate table message; truncate table room; truncate table feed; truncate table image; +truncate table notification; insert into activity(id, type, name) values (1, 'CLUB', 'YAPP'); From 24b4dce9c4f22431bf0238da215d0281a6cf94a6 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 13:24:05 +0900 Subject: [PATCH 02/13] =?UTF-8?q?feat=20:=20=EB=8C=93=EA=B8=80=20=EC=95=8C?= =?UTF-8?q?=EB=A6=BC=20Message=20Generator,=20Message=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../dto/CommentNotificationMessage.java | 35 ++++++++++++ .../CommentNotificationMessageGenerator.java | 55 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 backend/emm-sale/src/main/java/com/emmsale/notification/application/dto/CommentNotificationMessage.java create mode 100644 backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/CommentNotificationMessageGenerator.java diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/dto/CommentNotificationMessage.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/dto/CommentNotificationMessage.java new file mode 100644 index 000000000..cff19fbe8 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/dto/CommentNotificationMessage.java @@ -0,0 +1,35 @@ +package com.emmsale.notification.application.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public class CommentNotificationMessage { + + @JsonProperty("validate_only") + private final boolean validateOnly; + private final Message message; + + @RequiredArgsConstructor + @Getter + public static class Message { + + private final Data data; + private final String token; + } + + @RequiredArgsConstructor + @Getter + public static class Data { + + private final String receiverId; + private final String redirectId; + private final String notificationType; + private final String createdAt; + private final String content; + private final String writer; + private final String writerImageUrl; + } +} diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/CommentNotificationMessageGenerator.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/CommentNotificationMessageGenerator.java new file mode 100644 index 000000000..6dcbedce7 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/CommentNotificationMessageGenerator.java @@ -0,0 +1,55 @@ +package com.emmsale.notification.application.generator; + +import static com.emmsale.notification.exception.NotificationExceptionType.BAD_REQUEST_MEMBER_ID; +import static com.emmsale.notification.exception.NotificationExceptionType.CONVERTING_JSON_ERROR; + +import com.emmsale.member.domain.MemberRepository; +import com.emmsale.notification.application.dto.CommentNotificationMessage; +import com.emmsale.notification.application.dto.CommentNotificationMessage.Data; +import com.emmsale.notification.application.dto.CommentNotificationMessage.Message; +import com.emmsale.notification.domain.Notification; +import com.emmsale.notification.exception.NotificationException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@RequiredArgsConstructor +@Slf4j +public class CommentNotificationMessageGenerator implements NotificationMessageGenerator { + + private final Notification notification; + + @Override + public String makeMessage( + final String targetToken, + final ObjectMapper objectMapper, + final MemberRepository memberRepository + ) { + + final String jsonData = notification.getJsonData(); + + try { + + final Data data = objectMapper.readValue(jsonData, Data.class); + + validateIsExistedReceiver(memberRepository, Long.valueOf(data.getReceiverId())); + + final CommentNotificationMessage message = new CommentNotificationMessage( + DEFAULT_VALIDATE_ONLY, new Message(data, targetToken) + ); + + return objectMapper.writeValueAsString(message); + + } catch (JsonProcessingException e) { + throw new NotificationException(CONVERTING_JSON_ERROR); + } + } + + private void validateIsExistedReceiver(final MemberRepository memberRepository, + final Long receiverId) { + if (!memberRepository.existsById(receiverId)) { + throw new NotificationException(BAD_REQUEST_MEMBER_ID); + } + } +} From a0bb735639590d09502ce4da002bf465da7b9c03 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 13:27:39 +0900 Subject: [PATCH 03/13] =?UTF-8?q?feat=20:=20=EB=8C=93=EA=B8=80=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=8B=9C=20UpdateNotification=EC=9D=B4=20=EC=95=84?= =?UTF-8?q?=EB=8B=8C=20CommentNotificationEvent=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../application/CommentCommandService.java | 2 +- .../CommentNotificationEvent.java | 35 +++++++++++++++++++ .../event_publisher/EventPublisher.java | 10 ++++++ .../event_publisher/EventPublisherTest.java | 16 ++++----- 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java diff --git a/backend/emm-sale/src/main/java/com/emmsale/comment/application/CommentCommandService.java b/backend/emm-sale/src/main/java/com/emmsale/comment/application/CommentCommandService.java index 6433a234a..4237e3d25 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/comment/application/CommentCommandService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/comment/application/CommentCommandService.java @@ -50,7 +50,7 @@ public CommentResponse create( final Comment savedComment = commentRepository.save(comment); - eventPublisher.publish(savedComment, member); + eventPublisher.publish2(savedComment, member); return CommentResponse.from(savedComment); } diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java new file mode 100644 index 000000000..f0225a109 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java @@ -0,0 +1,35 @@ +package com.emmsale.event_publisher; + +import com.emmsale.comment.domain.Comment; +import com.emmsale.member.domain.Member; +import java.time.LocalDateTime; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public class CommentNotificationEvent { + + private static final String UPDATE_NOTIFICATION_COMMENT_TYPE = "comment"; + + private final Long receiverId; + private final Long redirectId; + private final LocalDateTime createdAt; + private final String content; + private final String writer; + private final String writerImageUrl; + + public static CommentNotificationEvent of(final Comment comment, final Long redirectId) { + final Member member = comment.getMember(); + + return new CommentNotificationEvent( + member.getId(), + redirectId, + LocalDateTime.now(), + comment.getContent(), + member.getName(), + member.getImageUrl() + ); + } + +} diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java index ef4ffd581..ee4c047dd 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java @@ -35,6 +35,16 @@ public void publish(final Comment trigger, final Member loginMember) { .forEach(applicationEventPublisher::publishEvent); } + public void publish2(final Comment trigger, final Member loginMember) { + final Set notificationCommentCandidates = trigger.getParent() + .map(parent -> findRelatedCommentsExcludingLoginMember(loginMember, parent)) + .orElse(Collections.emptySet()); + + notificationCommentCandidates.stream() + .map(it -> CommentNotificationEvent.of(it, trigger.getId())) + .forEach(applicationEventPublisher::publishEvent); + } + private Set findRelatedCommentsExcludingLoginMember( final Member loginMember, final Comment parent diff --git a/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java b/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java index 6105a7cfb..68062d22d 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java @@ -187,11 +187,11 @@ void test_publish_comment_parent_children() throws Exception { Comment.createChild(feed, 부모_댓글, 로그인_사용자, "내용4")); //when - eventPublisher.publish(알림_트리거_댓글, 로그인_사용자); + eventPublisher.publish2(알림_트리거_댓글, 로그인_사용자); //then verify(applicationEventPublisher, times(2)) - .publishEvent(any(UpdateNotificationEvent.class)); + .publishEvent(any(CommentNotificationEvent.class)); } @Test @@ -212,7 +212,7 @@ void test_publish_comment_parent_children_not_notification() throws Exception { ); //when - eventPublisher.publish(알림_트리거_댓글, 댓글_작성자1); + eventPublisher.publish2(알림_트리거_댓글, 댓글_작성자1); //then verify(applicationEventPublisher, times(0)) @@ -231,7 +231,7 @@ void test_publish_root_comment_no_notification() throws Exception { ); //when - eventPublisher.publish(알림_트리거_댓글, 댓글_작성자1); + eventPublisher.publish2(알림_트리거_댓글, 댓글_작성자1); //then verify(applicationEventPublisher, times(0)) @@ -266,11 +266,11 @@ void test_publish_comment_not_notification_deletedComment() throws Exception { ); //when - eventPublisher.publish(알림_트리거_댓글, 로그인_사용자); + eventPublisher.publish2(알림_트리거_댓글, 로그인_사용자); //then verify(applicationEventPublisher, times(1)) - .publishEvent(any(UpdateNotificationEvent.class)); + .publishEvent(any(CommentNotificationEvent.class)); } @Test @@ -301,11 +301,11 @@ void test_publish_comment_not_notification_deletedComment2() throws Exception { ); //when - eventPublisher.publish(알림_트리거_댓글, 로그인_사용자); + eventPublisher.publish2(알림_트리거_댓글, 로그인_사용자); //then verify(applicationEventPublisher, times(2)) - .publishEvent(any(UpdateNotificationEvent.class)); + .publishEvent(any(CommentNotificationEvent.class)); } @Test From b699754d93c9d666be47549e585f000db60d9159 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 13:28:42 +0900 Subject: [PATCH 04/13] =?UTF-8?q?refactor=20:=20EventListener=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EA=B8=B0=EC=A1=B4=20UpdateNotificatioEvent?= =?UTF-8?q?=EA=B0=80=20=EC=95=84=EB=8B=8C=20CommentNotificationEvent?= =?UTF-8?q?=EB=A5=BC=20listen=20=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../FirebaseCloudMessageClient.java | 9 ++++++ .../NotificationEventListener.java | 30 +++++++++++++++++++ .../NotificationEventListenerTest.java | 9 +++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/FirebaseCloudMessageClient.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/FirebaseCloudMessageClient.java index 42447ec2a..a8a3e687e 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/notification/application/FirebaseCloudMessageClient.java +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/FirebaseCloudMessageClient.java @@ -6,12 +6,14 @@ import com.emmsale.event_publisher.MessageNotificationEvent; import com.emmsale.member.domain.MemberRepository; +import com.emmsale.notification.application.generator.CommentNotificationMessageGenerator; import com.emmsale.notification.application.generator.MessageNotificationMessageGenerator; import com.emmsale.notification.application.generator.NotificationMessageGenerator; import com.emmsale.notification.application.generator.RequestNotificationMessageGenerator; import com.emmsale.notification.application.generator.UpdateNotificationMessageGenerator; import com.emmsale.notification.domain.FcmToken; import com.emmsale.notification.domain.FcmTokenRepository; +import com.emmsale.notification.domain.Notification; import com.emmsale.notification.domain.RequestNotification; import com.emmsale.notification.domain.UpdateNotification; import com.emmsale.notification.exception.NotificationException; @@ -71,6 +73,13 @@ public void sendMessageTo(final MessageNotificationEvent messageNotificationEven ); } + public void sendMessageTo(final Notification notification, final Long receiverId) { + sendMessageTo( + receiverId, + new CommentNotificationMessageGenerator(notification) + ); + } + private void sendMessageTo( final Long receiverId, final NotificationMessageGenerator messageGenerator diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/NotificationEventListener.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/NotificationEventListener.java index 0f79a88c9..96cdc2679 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/notification/application/NotificationEventListener.java +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/NotificationEventListener.java @@ -1,10 +1,16 @@ package com.emmsale.notification.application; +import com.emmsale.event_publisher.CommentNotificationEvent; import com.emmsale.event_publisher.MessageNotificationEvent; import com.emmsale.event_publisher.UpdateNotificationEvent; +import com.emmsale.notification.domain.Notification; +import com.emmsale.notification.domain.NotificationRepository; +import com.emmsale.notification.domain.NotificationType; import com.emmsale.notification.domain.UpdateNotification; import com.emmsale.notification.domain.UpdateNotificationRepository; import com.emmsale.notification.domain.UpdateNotificationType; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -19,6 +25,8 @@ public class NotificationEventListener { private final UpdateNotificationRepository updateNotificationRepository; private final FirebaseCloudMessageClient firebaseCloudMessageClient; + private final NotificationRepository notificationRepository; + private final ObjectMapper objectMapper; @Transactional(propagation = Propagation.REQUIRES_NEW) @TransactionalEventListener @@ -40,6 +48,28 @@ public void createUpdateNotification(final UpdateNotificationEvent updateNotific } } + @Transactional(propagation = Propagation.REQUIRES_NEW) + @TransactionalEventListener + public void createCommentNotification(final CommentNotificationEvent commentNotificationEvent) { + try { + final String jsonData = objectMapper.writeValueAsString(commentNotificationEvent); + + final Notification notification = notificationRepository.save( + new Notification(NotificationType.COMMENT, jsonData) + ); + + firebaseCloudMessageClient.sendMessageTo( + notification, + commentNotificationEvent.getReceiverId() + ); + + } catch (JsonProcessingException e) { + log.error("json 에러"); + } catch (Exception e) { + log.error("파이어베이스 관련 에러, 알림 재요청 필요, {}", e.getMessage(), e); + } + } + @Transactional(propagation = Propagation.REQUIRES_NEW) @TransactionalEventListener public void createMessageNotification(final MessageNotificationEvent messageNotificationEvent) { diff --git a/backend/emm-sale/src/test/java/com/emmsale/notification/application/NotificationEventListenerTest.java b/backend/emm-sale/src/test/java/com/emmsale/notification/application/NotificationEventListenerTest.java index caa74f96c..f86abb9e3 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/notification/application/NotificationEventListenerTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/notification/application/NotificationEventListenerTest.java @@ -11,8 +11,10 @@ import com.emmsale.event_publisher.MessageNotificationEvent; import com.emmsale.event_publisher.UpdateNotificationEvent; import com.emmsale.helper.ServiceIntegrationTestHelper; +import com.emmsale.notification.domain.NotificationRepository; import com.emmsale.notification.domain.UpdateNotification; import com.emmsale.notification.domain.UpdateNotificationRepository; +import com.fasterxml.jackson.databind.ObjectMapper; import java.time.LocalDateTime; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -26,13 +28,18 @@ class NotificationEventListenerTest extends ServiceIntegrationTestHelper { @Autowired private UpdateNotificationRepository updateNotificationRepository; private FirebaseCloudMessageClient mockingFirebaseCloudMessageClient; + @Autowired + private NotificationRepository notificationRepository; + @Autowired + private ObjectMapper objectMapper; @BeforeEach void setUp() { mockingFirebaseCloudMessageClient = mock(FirebaseCloudMessageClient.class); notificationEventListener = new NotificationEventListener( - updateNotificationRepository, mockingFirebaseCloudMessageClient + updateNotificationRepository, mockingFirebaseCloudMessageClient, + notificationRepository, objectMapper ); } From 3ae0d428d6b7831b2ab6e015e6b6314a4fceb372 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 13:29:52 +0900 Subject: [PATCH 05/13] =?UTF-8?q?refactor=20:=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20Sfl4j=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../generator/CommentNotificationMessageGenerator.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/CommentNotificationMessageGenerator.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/CommentNotificationMessageGenerator.java index 6dcbedce7..aec6302b7 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/CommentNotificationMessageGenerator.java +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/CommentNotificationMessageGenerator.java @@ -12,10 +12,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; @RequiredArgsConstructor -@Slf4j public class CommentNotificationMessageGenerator implements NotificationMessageGenerator { private final Notification notification; From 263a1338b7fc8705e90ce767edd6abb4accacfd9 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 13:56:07 +0900 Subject: [PATCH 06/13] =?UTF-8?q?feat=20:=20EventNotification=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20MessgaeGenerator,=20Message=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../FirebaseCloudMessageClient.java | 12 ++++- .../NotificationEventListener.java | 23 ++++++++ .../dto/EventNotificationMessage.java | 33 ++++++++++++ .../EventNotificationMessageGenerator.java | 52 +++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 backend/emm-sale/src/main/java/com/emmsale/notification/application/dto/EventNotificationMessage.java create mode 100644 backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/EventNotificationMessageGenerator.java diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/FirebaseCloudMessageClient.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/FirebaseCloudMessageClient.java index a8a3e687e..2f53a4e67 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/notification/application/FirebaseCloudMessageClient.java +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/FirebaseCloudMessageClient.java @@ -7,6 +7,7 @@ import com.emmsale.event_publisher.MessageNotificationEvent; import com.emmsale.member.domain.MemberRepository; import com.emmsale.notification.application.generator.CommentNotificationMessageGenerator; +import com.emmsale.notification.application.generator.EventNotificationMessageGenerator; import com.emmsale.notification.application.generator.MessageNotificationMessageGenerator; import com.emmsale.notification.application.generator.NotificationMessageGenerator; import com.emmsale.notification.application.generator.RequestNotificationMessageGenerator; @@ -14,6 +15,7 @@ import com.emmsale.notification.domain.FcmToken; import com.emmsale.notification.domain.FcmTokenRepository; import com.emmsale.notification.domain.Notification; +import com.emmsale.notification.domain.NotificationType; import com.emmsale.notification.domain.RequestNotification; import com.emmsale.notification.domain.UpdateNotification; import com.emmsale.notification.exception.NotificationException; @@ -21,6 +23,8 @@ import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.List; +import java.util.Map; +import java.util.function.Function; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -43,6 +47,12 @@ public class FirebaseCloudMessageClient { private static final String POSTFIX_FCM_REQUEST_URL = "/messages:send"; private static final String FIREBASE_KEY_PATH = "kerdy-submodule/firebase-kerdy.json"; private static final String GOOGLE_AUTH_URL = "https://www.googleapis.com/auth/cloud-platform"; + private static final Map> GENERATOR_MAP = + Map.of( + NotificationType.EVENT, EventNotificationMessageGenerator::new, + NotificationType.COMMENT, CommentNotificationMessageGenerator::new + ); + private final ObjectMapper objectMapper; private final MemberRepository memberRepository; @@ -76,7 +86,7 @@ public void sendMessageTo(final MessageNotificationEvent messageNotificationEven public void sendMessageTo(final Notification notification, final Long receiverId) { sendMessageTo( receiverId, - new CommentNotificationMessageGenerator(notification) + GENERATOR_MAP.get(notification.getType()).apply(notification) ); } diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/NotificationEventListener.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/NotificationEventListener.java index 96cdc2679..3c765c3e0 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/notification/application/NotificationEventListener.java +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/NotificationEventListener.java @@ -1,6 +1,7 @@ package com.emmsale.notification.application; import com.emmsale.event_publisher.CommentNotificationEvent; +import com.emmsale.event_publisher.EventNotificationEvent; import com.emmsale.event_publisher.MessageNotificationEvent; import com.emmsale.event_publisher.UpdateNotificationEvent; import com.emmsale.notification.domain.Notification; @@ -70,6 +71,28 @@ public void createCommentNotification(final CommentNotificationEvent commentNoti } } + @Transactional(propagation = Propagation.REQUIRES_NEW) + @TransactionalEventListener + public void createEventNotification(final EventNotificationEvent eventNotificationEvent) { + try { + final String jsonData = objectMapper.writeValueAsString(eventNotificationEvent); + + final Notification notification = notificationRepository.save( + new Notification(NotificationType.EVENT, jsonData) + ); + + firebaseCloudMessageClient.sendMessageTo( + notification, + eventNotificationEvent.getReceiverId() + ); + + } catch (JsonProcessingException e) { + log.error("json 에러"); + } catch (Exception e) { + log.error("파이어베이스 관련 에러, 알림 재요청 필요, {}", e.getMessage(), e); + } + } + @Transactional(propagation = Propagation.REQUIRES_NEW) @TransactionalEventListener public void createMessageNotification(final MessageNotificationEvent messageNotificationEvent) { diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/dto/EventNotificationMessage.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/dto/EventNotificationMessage.java new file mode 100644 index 000000000..62dac6c87 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/dto/EventNotificationMessage.java @@ -0,0 +1,33 @@ +package com.emmsale.notification.application.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public class EventNotificationMessage { + + @JsonProperty("validate_only") + private final boolean validateOnly; + private final Message message; + + @RequiredArgsConstructor + @Getter + public static class Message { + + private final Data data; + private final String token; + } + + @RequiredArgsConstructor + @Getter + public static class Data { + + private final String receiverId; + private final String redirectId; + private final String notificationType; + private final String createdAt; + private final String title; + } +} diff --git a/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/EventNotificationMessageGenerator.java b/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/EventNotificationMessageGenerator.java new file mode 100644 index 000000000..8ee14dfbe --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/notification/application/generator/EventNotificationMessageGenerator.java @@ -0,0 +1,52 @@ +package com.emmsale.notification.application.generator; + +import static com.emmsale.notification.application.dto.EventNotificationMessage.Data; +import static com.emmsale.notification.exception.NotificationExceptionType.BAD_REQUEST_MEMBER_ID; +import static com.emmsale.notification.exception.NotificationExceptionType.CONVERTING_JSON_ERROR; + +import com.emmsale.member.domain.MemberRepository; +import com.emmsale.notification.application.dto.EventNotificationMessage; +import com.emmsale.notification.application.dto.EventNotificationMessage.Message; +import com.emmsale.notification.domain.Notification; +import com.emmsale.notification.exception.NotificationException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class EventNotificationMessageGenerator implements NotificationMessageGenerator { + + private final Notification notification; + + @Override + public String makeMessage( + final String targetToken, + final ObjectMapper objectMapper, + final MemberRepository memberRepository + ) { + final String jsonData = notification.getJsonData(); + + try { + + final Data data = objectMapper.readValue(jsonData, Data.class); + + validateIsExistedReceiver(memberRepository, Long.valueOf(data.getReceiverId())); + + final EventNotificationMessage message = new EventNotificationMessage( + DEFAULT_VALIDATE_ONLY, new Message(data, targetToken) + ); + + return objectMapper.writeValueAsString(message); + + } catch (JsonProcessingException e) { + throw new NotificationException(CONVERTING_JSON_ERROR); + } + } + + private void validateIsExistedReceiver(final MemberRepository memberRepository, + final Long receiverId) { + if (!memberRepository.existsById(receiverId)) { + throw new NotificationException(BAD_REQUEST_MEMBER_ID); + } + } +} From 3d7548a0f00bf1a99a4ccf811dfaa5015b2ee76e Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 13:57:18 +0900 Subject: [PATCH 07/13] =?UTF-8?q?refactor=20:=20Event=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=20=EC=8B=9C=20EventNotificationEvent=20=EB=B0=9C=ED=96=89?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../event/application/EventService.java | 2 +- .../EventNotificationEvent.java | 29 +++++++++++++++++++ .../event_publisher/EventPublisher.java | 24 +++++++++++++++ .../event_publisher/EventPublisherTest.java | 12 ++++---- 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventNotificationEvent.java diff --git a/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java b/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java index 9c224395d..acdebae8c 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java @@ -192,7 +192,7 @@ public EventDetailResponse addEvent(final EventDetailRequest request, .map(Image::getName) .collect(toList()); - eventPublisher.publish(event); + eventPublisher.publish2(event); return EventDetailResponse.from(event, today, imageUrls); } diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventNotificationEvent.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventNotificationEvent.java new file mode 100644 index 000000000..a8da579b5 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventNotificationEvent.java @@ -0,0 +1,29 @@ +package com.emmsale.event_publisher; + +import com.emmsale.event.domain.Event; +import java.time.LocalDateTime; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public class EventNotificationEvent { + + private static final String UPDATE_NOTIFICATION_EVENT_TYPE = "event"; + + private final Long receiverId; + private final Long redirectId; + private final String notificationType; + private final LocalDateTime createdAt; + private final String title; + + public static EventNotificationEvent of(final Event event, final Long receiverId) { + return new EventNotificationEvent( + receiverId, + event.getId(), + UPDATE_NOTIFICATION_EVENT_TYPE, + LocalDateTime.now(), + event.getName() + ); + } +} diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java index ee4c047dd..66fb7879d 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java @@ -86,6 +86,30 @@ private void publishEvent(final Event event, final List members) { ); } + public void publish2(final Event event) { + final List tagIds = event.getTags() + .stream() + .map(it -> it.getTag().getId()) + .collect(Collectors.toList()); + + final Set memberIds = interestTagRepository.findInterestTagsByTagIdIn(tagIds) + .stream() + .collect(Collectors.groupingBy(it -> it.getMember().getId())) + .keySet(); + + final List members = memberRepository.findAllByIdIn(memberIds); + + publishEvent2(event, members); + } + + private void publishEvent2(final Event event, final List members) { + members.forEach( + it -> applicationEventPublisher.publishEvent( + EventNotificationEvent.of(event, it.getId()) + ) + ); + } + public void publish(final Message message, final Long receiverId) { final MessageNotificationEvent event = MessageNotificationEvent.of(message, receiverId); applicationEventPublisher.publishEvent(event); diff --git a/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java b/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java index 68062d22d..13e025ba5 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java @@ -95,17 +95,17 @@ void test_publish_event() throws Exception { final Event savedEvent = eventRepository.save(event); //when - eventPublisher.publish(savedEvent); + eventPublisher.publish2(savedEvent); //then - final ArgumentCaptor captor = ArgumentCaptor.forClass( - UpdateNotificationEvent.class); + final ArgumentCaptor captor = ArgumentCaptor.forClass( + EventNotificationEvent.class); verify(applicationEventPublisher, times(2)).publishEvent(captor.capture()); final List actual = captor.getAllValues() .stream() - .map(UpdateNotificationEvent::getReceiverId) + .map(EventNotificationEvent::getReceiverId) .collect(Collectors.toList()); assertThat(actual).containsExactlyInAnyOrderElementsOf(expected); @@ -135,7 +135,7 @@ void test_publish_event_no_notification_event_has_no_interest_tag() throws Excep final Event savedEvent = eventRepository.save(event); //when - eventPublisher.publish(savedEvent); + eventPublisher.publish2(savedEvent); //then verify(applicationEventPublisher, times(0)) @@ -157,7 +157,7 @@ void test_publish_event_no_notification_member_has_no_interest_tag() throws Exce final Event savedEvent = eventRepository.save(event); //when - eventPublisher.publish(savedEvent); + eventPublisher.publish2(savedEvent); //then verify(applicationEventPublisher, times(0)) From a7a3f5dc353e5b0287282c1ece63c862f526acb7 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 14:01:23 +0900 Subject: [PATCH 08/13] =?UTF-8?q?refactor=20:=20=EA=B8=B0=EC=A1=B4?= =?UTF-8?q?=EC=97=90=20=EC=9E=88=EB=8D=98=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../application/CommentCommandService.java | 2 +- .../event/application/EventService.java | 2 +- .../event_publisher/EventPublisher.java | 34 ------------------- .../event_publisher/EventPublisherTest.java | 16 ++++----- 4 files changed, 10 insertions(+), 44 deletions(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/comment/application/CommentCommandService.java b/backend/emm-sale/src/main/java/com/emmsale/comment/application/CommentCommandService.java index 4237e3d25..6433a234a 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/comment/application/CommentCommandService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/comment/application/CommentCommandService.java @@ -50,7 +50,7 @@ public CommentResponse create( final Comment savedComment = commentRepository.save(comment); - eventPublisher.publish2(savedComment, member); + eventPublisher.publish(savedComment, member); return CommentResponse.from(savedComment); } diff --git a/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java b/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java index acdebae8c..9c224395d 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event/application/EventService.java @@ -192,7 +192,7 @@ public EventDetailResponse addEvent(final EventDetailRequest request, .map(Image::getName) .collect(toList()); - eventPublisher.publish2(event); + eventPublisher.publish(event); return EventDetailResponse.from(event, today, imageUrls); } diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java index 66fb7879d..924356480 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java @@ -30,16 +30,6 @@ public void publish(final Comment trigger, final Member loginMember) { .map(parent -> findRelatedCommentsExcludingLoginMember(loginMember, parent)) .orElse(Collections.emptySet()); - notificationCommentCandidates.stream() - .map(it -> UpdateNotificationEvent.of(it, trigger.getId())) - .forEach(applicationEventPublisher::publishEvent); - } - - public void publish2(final Comment trigger, final Member loginMember) { - final Set notificationCommentCandidates = trigger.getParent() - .map(parent -> findRelatedCommentsExcludingLoginMember(loginMember, parent)) - .orElse(Collections.emptySet()); - notificationCommentCandidates.stream() .map(it -> CommentNotificationEvent.of(it, trigger.getId())) .forEach(applicationEventPublisher::publishEvent); @@ -79,30 +69,6 @@ public void publish(final Event event) { } private void publishEvent(final Event event, final List members) { - members.forEach( - it -> applicationEventPublisher.publishEvent( - UpdateNotificationEvent.of(event, it.getId()) - ) - ); - } - - public void publish2(final Event event) { - final List tagIds = event.getTags() - .stream() - .map(it -> it.getTag().getId()) - .collect(Collectors.toList()); - - final Set memberIds = interestTagRepository.findInterestTagsByTagIdIn(tagIds) - .stream() - .collect(Collectors.groupingBy(it -> it.getMember().getId())) - .keySet(); - - final List members = memberRepository.findAllByIdIn(memberIds); - - publishEvent2(event, members); - } - - private void publishEvent2(final Event event, final List members) { members.forEach( it -> applicationEventPublisher.publishEvent( EventNotificationEvent.of(event, it.getId()) diff --git a/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java b/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java index 13e025ba5..fef2a5e4d 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/event_publisher/EventPublisherTest.java @@ -95,7 +95,7 @@ void test_publish_event() throws Exception { final Event savedEvent = eventRepository.save(event); //when - eventPublisher.publish2(savedEvent); + eventPublisher.publish(savedEvent); //then final ArgumentCaptor captor = ArgumentCaptor.forClass( @@ -135,7 +135,7 @@ void test_publish_event_no_notification_event_has_no_interest_tag() throws Excep final Event savedEvent = eventRepository.save(event); //when - eventPublisher.publish2(savedEvent); + eventPublisher.publish(savedEvent); //then verify(applicationEventPublisher, times(0)) @@ -157,7 +157,7 @@ void test_publish_event_no_notification_member_has_no_interest_tag() throws Exce final Event savedEvent = eventRepository.save(event); //when - eventPublisher.publish2(savedEvent); + eventPublisher.publish(savedEvent); //then verify(applicationEventPublisher, times(0)) @@ -187,7 +187,7 @@ void test_publish_comment_parent_children() throws Exception { Comment.createChild(feed, 부모_댓글, 로그인_사용자, "내용4")); //when - eventPublisher.publish2(알림_트리거_댓글, 로그인_사용자); + eventPublisher.publish(알림_트리거_댓글, 로그인_사용자); //then verify(applicationEventPublisher, times(2)) @@ -212,7 +212,7 @@ void test_publish_comment_parent_children_not_notification() throws Exception { ); //when - eventPublisher.publish2(알림_트리거_댓글, 댓글_작성자1); + eventPublisher.publish(알림_트리거_댓글, 댓글_작성자1); //then verify(applicationEventPublisher, times(0)) @@ -231,7 +231,7 @@ void test_publish_root_comment_no_notification() throws Exception { ); //when - eventPublisher.publish2(알림_트리거_댓글, 댓글_작성자1); + eventPublisher.publish(알림_트리거_댓글, 댓글_작성자1); //then verify(applicationEventPublisher, times(0)) @@ -266,7 +266,7 @@ void test_publish_comment_not_notification_deletedComment() throws Exception { ); //when - eventPublisher.publish2(알림_트리거_댓글, 로그인_사용자); + eventPublisher.publish(알림_트리거_댓글, 로그인_사용자); //then verify(applicationEventPublisher, times(1)) @@ -301,7 +301,7 @@ void test_publish_comment_not_notification_deletedComment2() throws Exception { ); //when - eventPublisher.publish2(알림_트리거_댓글, 로그인_사용자); + eventPublisher.publish(알림_트리거_댓글, 로그인_사용자); //then verify(applicationEventPublisher, times(2)) From f6303ae493b86f43f0f1cf63092419049db741ee Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 14:14:13 +0900 Subject: [PATCH 09/13] =?UTF-8?q?test=20:=20=ED=86=B5=EA=B3=BC=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EB=AA=BB=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=84=B1=EA=B3=B5=EC=8B=9C=ED=82=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../CommentCommandServiceEventIntegrationTest.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/emm-sale/src/test/java/com/emmsale/comment/application/CommentCommandServiceEventIntegrationTest.java b/backend/emm-sale/src/test/java/com/emmsale/comment/application/CommentCommandServiceEventIntegrationTest.java index 1509d3327..137c57626 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/comment/application/CommentCommandServiceEventIntegrationTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/comment/application/CommentCommandServiceEventIntegrationTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.times; @@ -19,6 +20,8 @@ import com.emmsale.helper.ServiceIntegrationTestHelper; import com.emmsale.member.domain.Member; import com.emmsale.member.domain.MemberRepository; +import com.emmsale.notification.domain.Notification; +import com.emmsale.notification.domain.NotificationRepository; import com.emmsale.notification.domain.UpdateNotification; import com.emmsale.notification.domain.UpdateNotificationRepository; import org.junit.jupiter.api.BeforeEach; @@ -40,6 +43,8 @@ class CommentCommandServiceEventIntegrationTest extends ServiceIntegrationTestHe private MemberRepository memberRepository; @Autowired private UpdateNotificationRepository updateNotificationRepository; + @Autowired + private NotificationRepository notificationRepository; private Member 댓글_작성자1; private Member 댓글_작성자2; private Feed feed; @@ -70,8 +75,8 @@ void test_publish_comment() throws Exception { //then assertAll( () -> verify(firebaseCloudMessageClient, times(1)).sendMessageTo( - any(UpdateNotification.class)), - () -> assertEquals(1, updateNotificationRepository.findAll().size()) + any(Notification.class), anyLong()), + () -> assertEquals(1, notificationRepository.findAll().size()) ); } @@ -93,8 +98,8 @@ void test_publish_comment_error_firebase() throws Exception { //then assertAll( () -> verify(firebaseCloudMessageClient, times(1)).sendMessageTo( - any(UpdateNotification.class)), - () -> assertEquals(1, updateNotificationRepository.findAll().size()) + any(Notification.class), anyLong()), + () -> assertEquals(1, notificationRepository.findAll().size()) ); } } From f6701dc50cd28a922678dde2a7ab3be0a418644e Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 15:32:23 +0900 Subject: [PATCH 10/13] =?UTF-8?q?refactor=20:=20=EC=95=8C=EB=A6=BC=20Type?= =?UTF-8?q?=20=EB=8C=80=EB=AC=B8=EC=9E=90=EB=A1=9C=20=EB=AA=85=EC=8B=9C?= =?UTF-8?q?=ED=95=B4=EC=A3=BC=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../com/emmsale/event_publisher/CommentNotificationEvent.java | 4 +++- .../com/emmsale/event_publisher/EventNotificationEvent.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java index f0225a109..d810ce901 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java @@ -10,11 +10,12 @@ @Getter public class CommentNotificationEvent { - private static final String UPDATE_NOTIFICATION_COMMENT_TYPE = "comment"; + private static final String UPDATE_NOTIFICATION_COMMENT_TYPE = "COMMENT"; private final Long receiverId; private final Long redirectId; private final LocalDateTime createdAt; + private final String notificationType; private final String content; private final String writer; private final String writerImageUrl; @@ -26,6 +27,7 @@ public static CommentNotificationEvent of(final Comment comment, final Long redi member.getId(), redirectId, LocalDateTime.now(), + UPDATE_NOTIFICATION_COMMENT_TYPE, comment.getContent(), member.getName(), member.getImageUrl() diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventNotificationEvent.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventNotificationEvent.java index a8da579b5..26af55ca3 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventNotificationEvent.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventNotificationEvent.java @@ -9,7 +9,7 @@ @Getter public class EventNotificationEvent { - private static final String UPDATE_NOTIFICATION_EVENT_TYPE = "event"; + private static final String UPDATE_NOTIFICATION_EVENT_TYPE = "EVENT"; private final Long receiverId; private final Long redirectId; From a14e17c888f3f2ce6b91a7611b45f3a5b45764cf Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 15:46:52 +0900 Subject: [PATCH 11/13] =?UTF-8?q?refactor=20:=20json=5Fdata=EC=97=90=20cre?= =?UTF-8?q?atedAt=EC=9D=B4=20=EC=9E=88=EA=B8=B0=20=EB=95=8C=EB=AC=B8?= =?UTF-8?q?=EC=97=90=20notification=EC=9D=98=20created=5Fat=20=EC=B9=BC?= =?UTF-8?q?=EB=9F=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- backend/emm-sale/src/main/resources/schema.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/emm-sale/src/main/resources/schema.sql b/backend/emm-sale/src/main/resources/schema.sql index 98a40437c..0ec760bef 100644 --- a/backend/emm-sale/src/main/resources/schema.sql +++ b/backend/emm-sale/src/main/resources/schema.sql @@ -255,6 +255,5 @@ create table notification id bigint auto_increment primary key, type varchar(20) not null, json_data mediumtext not null, - created_at datetime(6), is_read bit not null ) From 7d0c8daf0feee697eabe017de3a3542ca2f5b94f Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 15:47:32 +0900 Subject: [PATCH 12/13] =?UTF-8?q?fix=20:=20=EB=8C=93=EA=B8=80=20=EC=95=8C?= =?UTF-8?q?=EB=A6=BC=20=EC=8B=9C,=20=ED=98=84=EC=9E=AC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EB=90=9C=20=EB=8C=93=EA=B8=80=EC=9D=98=20=EB=82=B4?= =?UTF-8?q?=EC=9A=A9=EC=9D=B4=20=EB=93=A4=EC=96=B4=EA=B0=80=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../emmsale/event_publisher/CommentNotificationEvent.java | 6 +++--- .../java/com/emmsale/event_publisher/EventPublisher.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java index d810ce901..fa250aa35 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java @@ -20,15 +20,15 @@ public class CommentNotificationEvent { private final String writer; private final String writerImageUrl; - public static CommentNotificationEvent of(final Comment comment, final Long redirectId) { + public static CommentNotificationEvent of(final Comment comment, final Comment trigger) { final Member member = comment.getMember(); return new CommentNotificationEvent( member.getId(), - redirectId, + trigger.getId(), LocalDateTime.now(), UPDATE_NOTIFICATION_COMMENT_TYPE, - comment.getContent(), + trigger.getContent(), member.getName(), member.getImageUrl() ); diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java index 924356480..741117877 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/EventPublisher.java @@ -31,7 +31,7 @@ public void publish(final Comment trigger, final Member loginMember) { .orElse(Collections.emptySet()); notificationCommentCandidates.stream() - .map(it -> CommentNotificationEvent.of(it, trigger.getId())) + .map(it -> CommentNotificationEvent.of(it, trigger)) .forEach(applicationEventPublisher::publishEvent); } From b22fd80d33745b3f7b033de896bcb2feb5d6a466 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Wed, 27 Sep 2023 15:57:44 +0900 Subject: [PATCH 13/13] =?UTF-8?q?fix=20:=20=EB=8C=93=EA=B8=80=20=EC=95=8C?= =?UTF-8?q?=EB=A6=BC=20=EC=8B=9C,=20=EC=9E=91=EC=84=B1=EC=9E=90=EC=99=80?= =?UTF-8?q?=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20URL=EC=9D=80=20=ED=98=84?= =?UTF-8?q?=EC=9E=AC=20=EB=8C=93=EA=B8=80=EC=9D=84=20=EC=9E=91=EC=84=B1?= =?UTF-8?q?=ED=95=9C=20Member=EC=9D=98=20=EC=A0=95=EB=B3=B4=EC=9D=B4?= =?UTF-8?q?=EB=AF=80=EB=A1=9C=20trigger=EC=97=90=EC=84=9C=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #649 --- .../emmsale/event_publisher/CommentNotificationEvent.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java index fa250aa35..869e0ccec 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java +++ b/backend/emm-sale/src/main/java/com/emmsale/event_publisher/CommentNotificationEvent.java @@ -22,6 +22,7 @@ public class CommentNotificationEvent { public static CommentNotificationEvent of(final Comment comment, final Comment trigger) { final Member member = comment.getMember(); + final Member triggerMember = trigger.getMember(); return new CommentNotificationEvent( member.getId(), @@ -29,9 +30,8 @@ public static CommentNotificationEvent of(final Comment comment, final Comment t LocalDateTime.now(), UPDATE_NOTIFICATION_COMMENT_TYPE, trigger.getContent(), - member.getName(), - member.getImageUrl() + triggerMember.getName(), + triggerMember.getImageUrl() ); } - }