diff --git a/build.gradle b/build.gradle index 9b9f2294..104b0e16 100644 --- a/build.gradle +++ b/build.gradle @@ -86,7 +86,8 @@ dependencies { // development implementation 'org.springframework.boot:spring-boot-starter-actuator' - developmentOnly 'org.springframework.boot:spring-boot-devtools' + // @see https://github.com/awspring/spring-cloud-aws/issues/657 +// developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-docker-compose' // External diff --git a/src/main/java/com/tune_fun/v1/account/adapter/output/persistence/device/DeviceCustomRepositoryImpl.java b/src/main/java/com/tune_fun/v1/account/adapter/output/persistence/device/DeviceCustomRepositoryImpl.java index 7b351923..844c41cd 100644 --- a/src/main/java/com/tune_fun/v1/account/adapter/output/persistence/device/DeviceCustomRepositoryImpl.java +++ b/src/main/java/com/tune_fun/v1/account/adapter/output/persistence/device/DeviceCustomRepositoryImpl.java @@ -52,7 +52,7 @@ public List fetchNotificationApprovedDevice( ) ) .from(DEVICE) - .join(DEVICE.account, ACCOUNT).fetchJoin() + .join(DEVICE.account, ACCOUNT) .where(predicate) .fetch(); } diff --git a/src/main/java/com/tune_fun/v1/account/domain/value/NotificationApprovedDevice.java b/src/main/java/com/tune_fun/v1/account/domain/value/NotificationApprovedDevice.java index ea238359..95eaa73c 100644 --- a/src/main/java/com/tune_fun/v1/account/domain/value/NotificationApprovedDevice.java +++ b/src/main/java/com/tune_fun/v1/account/domain/value/NotificationApprovedDevice.java @@ -1,10 +1,18 @@ package com.tune_fun.v1.account.domain.value; -public record NotificationApprovedDevice( - String id, - String username, - String nickname, - String fcmToken, - String deviceToken -) { +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode +public final class NotificationApprovedDevice { + private Long id; + private String username; + private String nickname; + private String fcmToken; + private String deviceToken; } diff --git a/src/main/java/com/tune_fun/v1/external/aws/sqs/SqsProvider.java b/src/main/java/com/tune_fun/v1/external/aws/sqs/SqsProvider.java index 3f8b4f51..1f8bbe81 100644 --- a/src/main/java/com/tune_fun/v1/external/aws/sqs/SqsProvider.java +++ b/src/main/java/com/tune_fun/v1/external/aws/sqs/SqsProvider.java @@ -8,9 +8,6 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; -import static org.springframework.messaging.MessageHeaders.CONTENT_TYPE; - @Component @RequiredArgsConstructor public class SqsProvider { @@ -23,7 +20,6 @@ public SendResult sendMessageRangedQueue(@NotBlank final String queueName, @N return sqsTemplate.send(to -> to .queue(sqsProducer.queueName()) - .header(CONTENT_TYPE, APPLICATION_JSON_VALUE) .payload(message) ); } diff --git a/src/main/java/com/tune_fun/v1/vote/adapter/input/message/VoteMessageConsumer.java b/src/main/java/com/tune_fun/v1/vote/adapter/input/message/VoteMessageConsumer.java index 927fb3c9..be401846 100644 --- a/src/main/java/com/tune_fun/v1/vote/adapter/input/message/VoteMessageConsumer.java +++ b/src/main/java/com/tune_fun/v1/vote/adapter/input/message/VoteMessageConsumer.java @@ -1,7 +1,9 @@ package com.tune_fun.v1.vote.adapter.input.message; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.firebase.messaging.FirebaseMessagingException; import com.tune_fun.v1.vote.application.port.input.usecase.SendVotePaperRegisterFcmUseCase; -import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperUploadEvent; +import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperRegisterEvent; import io.awspring.cloud.sqs.annotation.SqsListener; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -14,10 +16,10 @@ public class VoteMessageConsumer { private final SendVotePaperRegisterFcmUseCase sendVotePaperRegisterFcmUseCase; - @SqsListener("send-vote-paper-upload-notification-dev") - public void consumeVotePaperUploadEvent(final ProduceVotePaperUploadEvent produceVotePaperUploadEvent) { - log.info("send-vote-paper-upload-notification-dev: {}", produceVotePaperUploadEvent.id()); - sendVotePaperRegisterFcmUseCase.send(produceVotePaperUploadEvent); + @SqsListener(value = "send-vote-paper-upload-notification-dev") + public void consumeVotePaperUploadEvent(final ProduceVotePaperRegisterEvent produceVotePaperRegisterEvent) throws JsonProcessingException, FirebaseMessagingException { + log.info("send-vote-paper-upload-notification-dev: {}", produceVotePaperRegisterEvent.id()); + sendVotePaperRegisterFcmUseCase.send(produceVotePaperRegisterEvent); } } diff --git a/src/main/java/com/tune_fun/v1/vote/adapter/input/rest/VotePaperController.java b/src/main/java/com/tune_fun/v1/vote/adapter/input/rest/VotePaperController.java index c57807f9..5e8712d3 100644 --- a/src/main/java/com/tune_fun/v1/vote/adapter/input/rest/VotePaperController.java +++ b/src/main/java/com/tune_fun/v1/vote/adapter/input/rest/VotePaperController.java @@ -1,5 +1,6 @@ package com.tune_fun.v1.vote.adapter.input.rest; +import com.fasterxml.jackson.core.JsonProcessingException; import com.tune_fun.v1.account.domain.value.CurrentUser; import com.tune_fun.v1.common.config.Uris; import com.tune_fun.v1.common.hexagon.WebAdapter; @@ -29,7 +30,7 @@ public class VotePaperController { @PreAuthorize("hasRole('ARTIST')") @PostMapping(value = Uris.REGISTER_VOTE_PAPER) public ResponseEntity> registerVotePaper(@Valid @RequestBody final VotePaperCommands.Register command, - @CurrentUser final User user) { + @CurrentUser final User user) throws JsonProcessingException { registerVotePaperUseCase.register(command, user); return responseMapper.ok(MessageCode.CREATED); } diff --git a/src/main/java/com/tune_fun/v1/vote/adapter/output/firebase/VoteFcmAdapter.java b/src/main/java/com/tune_fun/v1/vote/adapter/output/firebase/VoteFcmAdapter.java index 7ff2c2c1..8281c206 100644 --- a/src/main/java/com/tune_fun/v1/vote/adapter/output/firebase/VoteFcmAdapter.java +++ b/src/main/java/com/tune_fun/v1/vote/adapter/output/firebase/VoteFcmAdapter.java @@ -1,7 +1,10 @@ package com.tune_fun.v1.vote.adapter.output.firebase; +import com.google.firebase.messaging.FirebaseMessagingException; import com.tune_fun.v1.external.firebase.FirebaseMessagingMediator; +import com.tune_fun.v1.external.firebase.FirebaseMto; import com.tune_fun.v1.vote.application.port.output.SendVoteFcmPort; +import com.tune_fun.v1.vote.domain.behavior.SendVotePaperRegisterFcm; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -12,4 +15,11 @@ public class VoteFcmAdapter implements SendVoteFcmPort { private final FirebaseMessagingMediator firebaseMessagingMediator; + private final VoteFirebaseMessagingMapper voteFirebaseMessagingMapper; + + @Override + public void notification(final SendVotePaperRegisterFcm behavior) throws FirebaseMessagingException { + FirebaseMto.ByTokens mto = voteFirebaseMessagingMapper.fromSendVotePaperRegisterFcmBehavior(behavior); + firebaseMessagingMediator.sendMulticastMessageByTokens(mto); + } } diff --git a/src/main/java/com/tune_fun/v1/vote/adapter/output/firebase/VoteFirebaseMessagingMapper.java b/src/main/java/com/tune_fun/v1/vote/adapter/output/firebase/VoteFirebaseMessagingMapper.java new file mode 100644 index 00000000..d2a1722d --- /dev/null +++ b/src/main/java/com/tune_fun/v1/vote/adapter/output/firebase/VoteFirebaseMessagingMapper.java @@ -0,0 +1,15 @@ +package com.tune_fun.v1.vote.adapter.output.firebase; + +import com.tune_fun.v1.common.config.BaseMapperConfig; +import com.tune_fun.v1.external.firebase.FirebaseMto; +import com.tune_fun.v1.vote.domain.behavior.SendVotePaperRegisterFcm; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +@Mapper(config = BaseMapperConfig.class) +public abstract class VoteFirebaseMessagingMapper { + + @Mapping(target = "tokens", source = "fcmTokens") + public abstract FirebaseMto.ByTokens fromSendVotePaperRegisterFcmBehavior(final SendVotePaperRegisterFcm behavior); + +} diff --git a/src/main/java/com/tune_fun/v1/vote/adapter/output/message/VoteMessageBrokerAdapter.java b/src/main/java/com/tune_fun/v1/vote/adapter/output/message/VoteMessageBrokerAdapter.java index 886d0513..74a34eb2 100644 --- a/src/main/java/com/tune_fun/v1/vote/adapter/output/message/VoteMessageBrokerAdapter.java +++ b/src/main/java/com/tune_fun/v1/vote/adapter/output/message/VoteMessageBrokerAdapter.java @@ -2,7 +2,8 @@ import com.tune_fun.v1.external.aws.sqs.SqsProvider; import com.tune_fun.v1.vote.application.port.output.ProduceVotePaperUploadEventPort; -import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperUploadEvent; +import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperRegisterEvent; +import io.awspring.cloud.sqs.operations.SendResult; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -15,7 +16,7 @@ public class VoteMessageBrokerAdapter implements ProduceVotePaperUploadEventPort @Override - public void produceVotePaperUploadEvent(final ProduceVotePaperUploadEvent produceVotePaperUploadEvent) { - sqsProvider.sendMessageRangedQueue(VOTE_PAPER_UPLOAD_QUEUE, produceVotePaperUploadEvent); + public SendResult produceVotePaperUploadEvent(final ProduceVotePaperRegisterEvent produceVotePaperRegisterEvent) { + return sqsProvider.sendMessageRangedQueue(VOTE_PAPER_UPLOAD_QUEUE, produceVotePaperRegisterEvent); } } diff --git a/src/main/java/com/tune_fun/v1/vote/adapter/output/persistence/VotePaperMapper.java b/src/main/java/com/tune_fun/v1/vote/adapter/output/persistence/VotePaperMapper.java index 8f2121c5..b9e4204e 100644 --- a/src/main/java/com/tune_fun/v1/vote/adapter/output/persistence/VotePaperMapper.java +++ b/src/main/java/com/tune_fun/v1/vote/adapter/output/persistence/VotePaperMapper.java @@ -16,6 +16,7 @@ public abstract class VotePaperMapper { @Mapping(target = "option", source = "option", qualifiedByName = "toValue") + @Mapping(target = "author", source = "author.nickname") public abstract RegisteredVotePaper registeredVotePaper(final VotePaperJpaEntity votePaperJpaEntity); @Named("toValue") diff --git a/src/main/java/com/tune_fun/v1/vote/application/port/input/usecase/RegisterVotePaperUseCase.java b/src/main/java/com/tune_fun/v1/vote/application/port/input/usecase/RegisterVotePaperUseCase.java index 1a6e5a56..b9588f42 100644 --- a/src/main/java/com/tune_fun/v1/vote/application/port/input/usecase/RegisterVotePaperUseCase.java +++ b/src/main/java/com/tune_fun/v1/vote/application/port/input/usecase/RegisterVotePaperUseCase.java @@ -1,11 +1,12 @@ package com.tune_fun.v1.vote.application.port.input.usecase; +import com.fasterxml.jackson.core.JsonProcessingException; import com.tune_fun.v1.vote.application.port.input.command.VotePaperCommands; import org.springframework.security.core.userdetails.User; @FunctionalInterface public interface RegisterVotePaperUseCase { - void register(final VotePaperCommands.Register command, User user); + void register(final VotePaperCommands.Register command, User user) throws JsonProcessingException; } diff --git a/src/main/java/com/tune_fun/v1/vote/application/port/input/usecase/SendVotePaperRegisterFcmUseCase.java b/src/main/java/com/tune_fun/v1/vote/application/port/input/usecase/SendVotePaperRegisterFcmUseCase.java index af5c8a49..d3ba22ac 100644 --- a/src/main/java/com/tune_fun/v1/vote/application/port/input/usecase/SendVotePaperRegisterFcmUseCase.java +++ b/src/main/java/com/tune_fun/v1/vote/application/port/input/usecase/SendVotePaperRegisterFcmUseCase.java @@ -1,7 +1,9 @@ package com.tune_fun.v1.vote.application.port.input.usecase; -import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperUploadEvent; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.firebase.messaging.FirebaseMessagingException; +import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperRegisterEvent; public interface SendVotePaperRegisterFcmUseCase { - void send(final ProduceVotePaperUploadEvent produceVotePaperUploadEvent); + void send(final ProduceVotePaperRegisterEvent produceVotePaperRegisterEvent) throws JsonProcessingException, FirebaseMessagingException; } diff --git a/src/main/java/com/tune_fun/v1/vote/application/port/output/ProduceVotePaperUploadEventPort.java b/src/main/java/com/tune_fun/v1/vote/application/port/output/ProduceVotePaperUploadEventPort.java index a4ad5e60..5b778549 100644 --- a/src/main/java/com/tune_fun/v1/vote/application/port/output/ProduceVotePaperUploadEventPort.java +++ b/src/main/java/com/tune_fun/v1/vote/application/port/output/ProduceVotePaperUploadEventPort.java @@ -1,9 +1,10 @@ package com.tune_fun.v1.vote.application.port.output; -import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperUploadEvent; +import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperRegisterEvent; +import io.awspring.cloud.sqs.operations.SendResult; public interface ProduceVotePaperUploadEventPort { - void produceVotePaperUploadEvent(final ProduceVotePaperUploadEvent produceVotePaperUploadEvent); + SendResult produceVotePaperUploadEvent(final ProduceVotePaperRegisterEvent produceVotePaperRegisterEvent); } diff --git a/src/main/java/com/tune_fun/v1/vote/application/port/output/SendVoteFcmPort.java b/src/main/java/com/tune_fun/v1/vote/application/port/output/SendVoteFcmPort.java index 319f1444..d88d0e4f 100644 --- a/src/main/java/com/tune_fun/v1/vote/application/port/output/SendVoteFcmPort.java +++ b/src/main/java/com/tune_fun/v1/vote/application/port/output/SendVoteFcmPort.java @@ -1,4 +1,8 @@ package com.tune_fun.v1.vote.application.port.output; +import com.google.firebase.messaging.FirebaseMessagingException; +import com.tune_fun.v1.vote.domain.behavior.SendVotePaperRegisterFcm; + public interface SendVoteFcmPort { + void notification(final SendVotePaperRegisterFcm sendVotePaperRegisterFcmBehavior) throws FirebaseMessagingException; } diff --git a/src/main/java/com/tune_fun/v1/vote/application/service/RegisterVotePaperService.java b/src/main/java/com/tune_fun/v1/vote/application/service/RegisterVotePaperService.java index 582954e4..44354e55 100644 --- a/src/main/java/com/tune_fun/v1/vote/application/service/RegisterVotePaperService.java +++ b/src/main/java/com/tune_fun/v1/vote/application/service/RegisterVotePaperService.java @@ -1,18 +1,22 @@ package com.tune_fun.v1.vote.application.service; +import com.fasterxml.jackson.core.JsonProcessingException; import com.tune_fun.v1.common.exception.CommonApplicationException; import com.tune_fun.v1.common.hexagon.UseCase; +import com.tune_fun.v1.common.util.ObjectUtil; import com.tune_fun.v1.vote.application.port.input.command.VotePaperCommands; import com.tune_fun.v1.vote.application.port.input.usecase.RegisterVotePaperUseCase; import com.tune_fun.v1.vote.application.port.output.LoadVotePaperPort; import com.tune_fun.v1.vote.application.port.output.ProduceVotePaperUploadEventPort; import com.tune_fun.v1.vote.application.port.output.SaveVoteChoicePort; import com.tune_fun.v1.vote.application.port.output.SaveVotePaperPort; -import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperUploadEvent; +import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperRegisterEvent; import com.tune_fun.v1.vote.domain.behavior.SaveVoteChoice; import com.tune_fun.v1.vote.domain.behavior.SaveVotePaper; import com.tune_fun.v1.vote.domain.value.RegisteredVotePaper; +import io.awspring.cloud.sqs.operations.SendResult; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Service; @@ -22,6 +26,7 @@ import static com.tune_fun.v1.common.response.MessageCode.VOTE_POLICY_ONE_VOTE_PAPER_PER_USER; +@Slf4j @Service @UseCase @RequiredArgsConstructor @@ -36,17 +41,21 @@ public class RegisterVotePaperService implements RegisterVotePaperUseCase { private final VoteBehaviorMapper voteBehaviorMapper; + private final ObjectUtil objectUtil; + @Transactional @Override - public void register(final VotePaperCommands.Register command, final User user) { + public void register(final VotePaperCommands.Register command, final User user) throws JsonProcessingException { validateRegistrableVotePaperCount(user); RegisteredVotePaper registeredVotePaper = saveVotePaper(command); saveVoteChoiceByRegisteredVotePaper(command, registeredVotePaper); // TODO : Scheduling 정책 수립 -// ProduceVotePaperUploadEvent produceVotePaperUploadEventBehavior = getProduceVotePaperUploadEventBehavior(registeredVotePaper); -// produceVotePaperUploadEventPort.produceVotePaperUploadEvent(produceVotePaperUploadEventBehavior); + ProduceVotePaperRegisterEvent produceVotePaperRegisterEventBehavior = getProduceVotePaperUploadEventBehavior(registeredVotePaper); + SendResult sendResult = produceVotePaperUploadEventPort.produceVotePaperUploadEvent(produceVotePaperRegisterEventBehavior); + + log.info("sendResult: {}", objectUtil.objectToPrettyJson(sendResult.message().getPayload())); } public void validateRegistrableVotePaperCount(final User user) { @@ -66,8 +75,7 @@ public void saveVoteChoiceByRegisteredVotePaper(VotePaperCommands.Register comma saveVoteChoicePort.saveVoteChoice(registeredVotePaper.id(), saveVoteChoicesBehavior); } - private static @NotNull ProduceVotePaperUploadEvent getProduceVotePaperUploadEventBehavior(RegisteredVotePaper registeredVotePaper) { - return new ProduceVotePaperUploadEvent(registeredVotePaper.uuid(), registeredVotePaper.title(), registeredVotePaper.content(), - registeredVotePaper.voteStartAt(), registeredVotePaper.voteEndAt()); + private static @NotNull ProduceVotePaperRegisterEvent getProduceVotePaperUploadEventBehavior(RegisteredVotePaper registeredVotePaper) { + return new ProduceVotePaperRegisterEvent(registeredVotePaper.uuid(), registeredVotePaper.author(), registeredVotePaper.title(), registeredVotePaper.content()); } } diff --git a/src/main/java/com/tune_fun/v1/vote/application/service/SendVotePaperRegisterFcmService.java b/src/main/java/com/tune_fun/v1/vote/application/service/SendVotePaperRegisterFcmService.java index bd009ebe..ad401b06 100644 --- a/src/main/java/com/tune_fun/v1/vote/application/service/SendVotePaperRegisterFcmService.java +++ b/src/main/java/com/tune_fun/v1/vote/application/service/SendVotePaperRegisterFcmService.java @@ -1,33 +1,47 @@ package com.tune_fun.v1.vote.application.service; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.firebase.messaging.FirebaseMessagingException; import com.tune_fun.v1.account.application.port.output.device.LoadDevicePort; import com.tune_fun.v1.account.domain.value.NotificationApprovedDevice; import com.tune_fun.v1.common.hexagon.UseCase; +import com.tune_fun.v1.common.util.ObjectUtil; import com.tune_fun.v1.vote.application.port.input.usecase.SendVotePaperRegisterFcmUseCase; import com.tune_fun.v1.vote.application.port.output.SendVoteFcmPort; -import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperUploadEvent; +import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperRegisterEvent; +import com.tune_fun.v1.vote.domain.behavior.SendVotePaperRegisterFcm; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; + +@Slf4j @Service @UseCase @RequiredArgsConstructor public class SendVotePaperRegisterFcmService implements SendVotePaperRegisterFcmUseCase { private final LoadDevicePort loadDevicePort; - private final SendVoteFcmPort sendVoteFcmPort; - @Transactional(propagation = Propagation.REQUIRES_NEW) + private final VoteBehaviorMapper voteBehaviorMapper; + + private final ObjectUtil objectUtil; + + @Transactional(propagation = REQUIRES_NEW) @Override - public void send(ProduceVotePaperUploadEvent produceVotePaperUploadEvent) { + public void send(ProduceVotePaperRegisterEvent produceVotePaperRegisterEvent) throws JsonProcessingException, FirebaseMessagingException { List notificationApprovedDevices = loadDevicePort. loadNotificationApprovedDevice(true, null, null); - // TODO : FCM 발송 로직 구현 + log.info("notificationApprovedDevices: {}", objectUtil.objectToPrettyJson(notificationApprovedDevices)); + + SendVotePaperRegisterFcm sendVotePaperRegisterFcmBehavior = voteBehaviorMapper + .sendVotePaperRegisterFcm(produceVotePaperRegisterEvent, notificationApprovedDevices); + sendVoteFcmPort.notification(sendVotePaperRegisterFcmBehavior); } } diff --git a/src/main/java/com/tune_fun/v1/vote/application/service/VoteBehaviorMapper.java b/src/main/java/com/tune_fun/v1/vote/application/service/VoteBehaviorMapper.java index 65f924fb..9f932d15 100644 --- a/src/main/java/com/tune_fun/v1/vote/application/service/VoteBehaviorMapper.java +++ b/src/main/java/com/tune_fun/v1/vote/application/service/VoteBehaviorMapper.java @@ -1,16 +1,22 @@ package com.tune_fun.v1.vote.application.service; +import com.tune_fun.v1.account.domain.value.NotificationApprovedDevice; import com.tune_fun.v1.common.config.BaseMapperConfig; import com.tune_fun.v1.vote.application.port.input.command.VotePaperCommands; +import com.tune_fun.v1.vote.domain.behavior.ProduceVotePaperRegisterEvent; import com.tune_fun.v1.vote.domain.behavior.SaveVoteChoice; import com.tune_fun.v1.vote.domain.behavior.SaveVotePaper; +import com.tune_fun.v1.vote.domain.behavior.SendVotePaperRegisterFcm; import org.mapstruct.IterableMapping; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Named; +import java.util.List; import java.util.Set; +import static java.util.stream.Collectors.toSet; + @Mapper(config = BaseMapperConfig.class) public abstract class VoteBehaviorMapper { @@ -25,4 +31,19 @@ public abstract class VoteBehaviorMapper { @Mapping(target = "offerDurationMs", source = "durationMs") @Mapping(target = "offerReleaseDate", source = "releaseDate") public abstract SaveVoteChoice saveVoteChoice(final VotePaperCommands.Offer offer); + + @Mapping(target = "title", source = "event", qualifiedByName = "title") + @Mapping(target = "body", source = "event.title") + @Mapping(target = "fcmTokens", source = "devices", qualifiedByName = "fcmTokens") + public abstract SendVotePaperRegisterFcm sendVotePaperRegisterFcm(final ProduceVotePaperRegisterEvent event, final List devices); + + @Named("title") + public String title(final ProduceVotePaperRegisterEvent event) { + return String.format("[%s]님이 투표를 게재하였습니다.", event.author()); + } + + @Named("fcmTokens") + public Set fcmTokens(final List devices) { + return devices.stream().map(NotificationApprovedDevice::getFcmToken).collect(toSet()); + } } diff --git a/src/main/java/com/tune_fun/v1/vote/domain/behavior/ProduceVotePaperRegisterEvent.java b/src/main/java/com/tune_fun/v1/vote/domain/behavior/ProduceVotePaperRegisterEvent.java new file mode 100644 index 00000000..1faf9bf8 --- /dev/null +++ b/src/main/java/com/tune_fun/v1/vote/domain/behavior/ProduceVotePaperRegisterEvent.java @@ -0,0 +1,4 @@ +package com.tune_fun.v1.vote.domain.behavior; + +public record ProduceVotePaperRegisterEvent(String id, String author, String title, String content) { +} diff --git a/src/main/java/com/tune_fun/v1/vote/domain/behavior/ProduceVotePaperUploadEvent.java b/src/main/java/com/tune_fun/v1/vote/domain/behavior/ProduceVotePaperUploadEvent.java deleted file mode 100644 index 11c011b1..00000000 --- a/src/main/java/com/tune_fun/v1/vote/domain/behavior/ProduceVotePaperUploadEvent.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.tune_fun.v1.vote.domain.behavior; - -import java.time.LocalDateTime; - -public record ProduceVotePaperUploadEvent( - String id, - String title, - String content, - LocalDateTime voteStartAt, - LocalDateTime voteEndAt -) { -} diff --git a/src/main/java/com/tune_fun/v1/vote/domain/behavior/SendVotePaperRegisterFcm.java b/src/main/java/com/tune_fun/v1/vote/domain/behavior/SendVotePaperRegisterFcm.java new file mode 100644 index 00000000..0cc2f8f7 --- /dev/null +++ b/src/main/java/com/tune_fun/v1/vote/domain/behavior/SendVotePaperRegisterFcm.java @@ -0,0 +1,12 @@ +package com.tune_fun.v1.vote.domain.behavior; + +import java.util.Map; +import java.util.Set; + +public record SendVotePaperRegisterFcm( + String title, + String body, + Map data, + Set fcmTokens +) { +} diff --git a/src/main/java/com/tune_fun/v1/vote/domain/value/RegisteredVotePaper.java b/src/main/java/com/tune_fun/v1/vote/domain/value/RegisteredVotePaper.java index 8cdf9e31..5848a776 100644 --- a/src/main/java/com/tune_fun/v1/vote/domain/value/RegisteredVotePaper.java +++ b/src/main/java/com/tune_fun/v1/vote/domain/value/RegisteredVotePaper.java @@ -5,6 +5,7 @@ public record RegisteredVotePaper( Long id, String uuid, + String author, String title, String content, String option,