-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] add notification table and use in deadletter
- Loading branch information
Showing
12 changed files
with
159 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
backend/src/main/java/com/twtw/backend/domain/notification/entity/Notification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.twtw.backend.domain.notification.entity; | ||
|
||
import com.github.f4b6a3.ulid.UlidCreator; | ||
import com.twtw.backend.global.audit.AuditListener; | ||
import com.twtw.backend.global.audit.Auditable; | ||
import com.twtw.backend.global.audit.BaseTime; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.Id; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.UUID; | ||
|
||
@Getter | ||
@Entity | ||
@EntityListeners(AuditListener.class) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Notification implements Auditable { | ||
|
||
@Id | ||
@Column(columnDefinition = "BINARY(16)") | ||
private final UUID id = UlidCreator.getMonotonicUlid().toUuid(); | ||
|
||
@Column(nullable = false) | ||
private String title; | ||
|
||
@Column(nullable = false) | ||
private String body; | ||
|
||
@Column(nullable = false) | ||
private String idInfo; | ||
|
||
@Column(nullable = false) | ||
@Enumerated(EnumType.STRING) | ||
private NotificationType type; | ||
|
||
private Boolean isCompleted = false; | ||
|
||
@Setter private BaseTime baseTime; | ||
|
||
@Builder | ||
public Notification( | ||
final String title, | ||
final String body, | ||
final String idInfo, | ||
final NotificationType type) { | ||
this.title = title; | ||
this.body = body; | ||
this.idInfo = idInfo; | ||
this.type = type; | ||
} | ||
|
||
public void complete() { | ||
this.isCompleted = true; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
backend/src/main/java/com/twtw/backend/domain/notification/entity/NotificationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.twtw.backend.domain.notification.entity; | ||
|
||
public enum NotificationType { | ||
FRIEND_REQUEST, GROUP_REQUEST, DESTINATION_CHANGE, PLAN_REQUEST | ||
} |
32 changes: 17 additions & 15 deletions
32
backend/src/main/java/com/twtw/backend/domain/notification/messagequeue/FcmConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
package com.twtw.backend.domain.notification.messagequeue; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.rabbitmq.client.Channel; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.twtw.backend.domain.notification.dto.NotificationRequest; | ||
import com.twtw.backend.domain.notification.entity.Notification; | ||
import com.twtw.backend.domain.notification.repository.NotificationRepository; | ||
|
||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.amqp.support.AmqpHeaders; | ||
import org.springframework.messaging.handler.annotation.Header; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
@Component | ||
public class FcmConsumer { | ||
private final FirebaseMessaging firebaseMessaging; | ||
private final NotificationRepository notificationRepository; | ||
|
||
public FcmConsumer(FirebaseMessaging firebaseMessaging) { | ||
public FcmConsumer( | ||
FirebaseMessaging firebaseMessaging, NotificationRepository notificationRepository) { | ||
this.firebaseMessaging = firebaseMessaging; | ||
this.notificationRepository = notificationRepository; | ||
} | ||
|
||
@Transactional | ||
@RabbitListener(queues = "notification.queue") | ||
public void sendNotification( | ||
final NotificationRequest request, | ||
final Channel channel, | ||
@Header(AmqpHeaders.DELIVERY_TAG) final long tag) | ||
throws IOException { | ||
try { | ||
firebaseMessaging.send(request.toMessage()); | ||
} catch (final Exception e) { | ||
channel.basicNack(tag, false, false); | ||
} | ||
public void sendNotification(final NotificationRequest request) | ||
throws FirebaseMessagingException { | ||
firebaseMessaging.send(request.toMessage()); | ||
|
||
notificationRepository | ||
.findById(UUID.fromString(request.getNotificationId())) | ||
.ifPresent(Notification::complete); | ||
} | ||
} |
20 changes: 19 additions & 1 deletion
20
backend/src/main/java/com/twtw/backend/domain/notification/messagequeue/FcmProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
.../main/java/com/twtw/backend/domain/notification/repository/JpaNotificationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.twtw.backend.domain.notification.repository; | ||
|
||
import com.twtw.backend.domain.notification.entity.Notification; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.UUID; | ||
|
||
public interface JpaNotificationRepository extends JpaRepository<Notification, UUID>, NotificationRepository {} |
14 changes: 14 additions & 0 deletions
14
...src/main/java/com/twtw/backend/domain/notification/repository/NotificationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.twtw.backend.domain.notification.repository; | ||
|
||
import com.twtw.backend.domain.notification.entity.Notification; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Repository | ||
public interface NotificationRepository { | ||
Notification save(final Notification notification); | ||
Optional<Notification> findById(final UUID id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 4 additions & 5 deletions
9
backend/src/test/java/com/twtw/backend/support/exclude/ExcludeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
package com.twtw.backend.support.exclude; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doNothing; | ||
|
||
import com.twtw.backend.domain.notification.messagequeue.FcmProducer; | ||
import com.twtw.backend.support.testcontainer.ContainerTestConfig; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doNothing; | ||
|
||
@ContextConfiguration(initializers = ContainerTestConfig.class) | ||
public abstract class ExcludeTest { | ||
|
||
@MockBean private FcmProducer fcmProducer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
doNothing().when(fcmProducer).sendNotification(any()); | ||
doNothing().when(fcmProducer).sendNotification(any(), any()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters