Skip to content

Commit

Permalink
[FIX] admin api for retrying dead letter
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Apr 9, 2024
1 parent 1e7c867 commit 1196c59
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.twtw.backend.domain.notification.controller;

import com.twtw.backend.domain.notification.messagequeue.FcmProducer;

import lombok.RequiredArgsConstructor;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequiredArgsConstructor
@RequestMapping("notifications")
public class NotificationController {

private final FcmProducer fcmProducer;

@PreAuthorize("hasRole('ADMIN')")
@PostMapping
public ResponseEntity<Void> sendNotification(@RequestBody final List<UUID> ids) {
fcmProducer.sendFailedNotification(ids);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NotificationRequest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class Notification implements Auditable {
@Column(nullable = false)
private String idInfo;

@Column(nullable = false)
private String deviceToken;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private NotificationType type;
Expand All @@ -52,10 +55,12 @@ public Notification(
final String title,
final String body,
final String idInfo,
final String deviceToken,
final NotificationType type) {
this.title = title;
this.body = body;
this.idInfo = idInfo;
this.deviceToken = deviceToken;
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.twtw.backend.domain.notification.mapper;

import com.twtw.backend.domain.notification.dto.NotificationRequest;
import com.twtw.backend.domain.notification.entity.Notification;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants.ComponentModel;

@Mapper(componentModel = ComponentModel.SPRING)
public interface NotificationMapper {

@Mapping(target = "notificationId", source = "notification.id")
@Mapping(target = "deviceToken", source = "notification.notificationType")
@Mapping(target = "title", source = "notification.title")
@Mapping(target = "body", source = "notification.body")
@Mapping(target = "id", source = "notification.idInfo")
NotificationRequest toNotificationRequest(final Notification notification);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.twtw.backend.domain.notification.dto.NotificationRequest;
import com.twtw.backend.domain.notification.entity.Notification;
import com.twtw.backend.domain.notification.entity.NotificationType;
import com.twtw.backend.domain.notification.mapper.NotificationMapper;
import com.twtw.backend.domain.notification.repository.NotificationRepository;
import com.twtw.backend.global.constant.RabbitMQConstant;

Expand All @@ -11,6 +12,7 @@
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

@Component
Expand All @@ -19,6 +21,7 @@ public class FcmProducer {

private final RabbitTemplate rabbitTemplate;
private final NotificationRepository notificationRepository;
private final NotificationMapper notificationMapper;

public void sendNotification(final NotificationRequest request, final NotificationType type) {
final UUID id =
Expand All @@ -28,14 +31,25 @@ public void sendNotification(final NotificationRequest request, final Notificati
request.getTitle(),
request.getBody(),
request.getId(),
request.getDeviceToken(),
type))
.getId();

request.setNotificationId(id.toString());

sendToQueue(request);
}

private void sendToQueue(final NotificationRequest request) {
rabbitTemplate.convertAndSend(
RabbitMQConstant.NOTIFICATION_EXCHANGE.getName(),
RabbitMQConstant.NOTIFICATION_ROUTING_KEY.getName(),
request);
}

public void sendFailedNotification(final List<UUID> ids) {
notificationRepository.findAllByIdIn(ids).stream()
.map(notificationMapper::toNotificationRequest)
.forEach(this::sendToQueue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.UUID;

public interface JpaNotificationRepository
extends JpaRepository<Notification, UUID>, NotificationRepository {}
extends JpaRepository<Notification, UUID>, NotificationRepository {

List<Notification> findAllByIdIn(final List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -12,4 +13,6 @@ public interface NotificationRepository {
Notification save(final Notification notification);

Optional<Notification> findById(final UUID id);

List<Notification> findAllByIdIn(final List<UUID> ids);
}

0 comments on commit 1196c59

Please sign in to comment.