diff --git a/src/main/java/com/example/daobe/notification/infrastructure/sqs/SqsNotificationListener.java b/src/main/java/com/example/daobe/notification/infrastructure/sqs/SqsNotificationListener.java new file mode 100644 index 00000000..45d7390c --- /dev/null +++ b/src/main/java/com/example/daobe/notification/infrastructure/sqs/SqsNotificationListener.java @@ -0,0 +1,33 @@ +package com.example.daobe.notification.infrastructure.sqs; + +import com.example.daobe.notification.application.NotificationCreateService; +import com.example.daobe.notification.infrastructure.sqs.payload.NotificationExternalEventPayload; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.awspring.cloud.sqs.annotation.SqsListener; +import io.awspring.cloud.sqs.listener.acknowledgement.Acknowledgement; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class SqsNotificationListener { + + private final ObjectMapper objectMapper; + private final NotificationCreateService notificationCreateService; + + @SqsListener(value = "${aws.sqs.joytas.queue}", id = "{sqs.joytas.group-id}") + public void handleCreateNotification(String payload, Acknowledgement acknowledgement) { + NotificationExternalEventPayload eventPayload = deserialize(payload); + notificationCreateService.createNotification(eventPayload.toCommand()); + acknowledgement.acknowledge(); + } + + private NotificationExternalEventPayload deserialize(String value) { + try { + return objectMapper.readValue(value, NotificationExternalEventPayload.class); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/com/example/daobe/notification/infrastructure/sqs/payload/NotificationExternalEventPayload.java b/src/main/java/com/example/daobe/notification/infrastructure/sqs/payload/NotificationExternalEventPayload.java new file mode 100644 index 00000000..90904ccf --- /dev/null +++ b/src/main/java/com/example/daobe/notification/infrastructure/sqs/payload/NotificationExternalEventPayload.java @@ -0,0 +1,28 @@ +package com.example.daobe.notification.infrastructure.sqs.payload; + +import com.example.daobe.notification.application.dto.command.CreateNotificationCommand; + +public record NotificationExternalEventPayload( + String eventType, + Attributes attributes +) { + + public CreateNotificationCommand toCommand() { + return new CreateNotificationCommand( + this.attributes.senderId(), + this.attributes.receiverId(), + this.attributes.domainId(), + this.attributes.domainName(), + this.eventType + ); + } + + // Nested + public record Attributes( + Long senderId, + Long receiverId, + Long domainId, + String domainName + ) { + } +}