From 6325dfaf60cbaed5bdf00711e26d9b2a7de2129e Mon Sep 17 00:00:00 2001 From: Junho Hwang <72647031+juno-junho@users.noreply.github.com> Date: Sun, 31 Dec 2023 01:04:16 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A9=94=EC=9D=BC=20=EC=8B=9C=EC=8A=A4?= =?UTF-8?q?=ED=85=9C=20=EB=A6=AC=ED=8E=99=ED=86=A0=EB=A7=81=20(#301)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spaceclub/form/service/SubmitService.java | 3 +- .../notification/mail/domain/MailHistory.java | 90 +++++++++++++ .../notification/mail/domain/MailTracker.java | 122 ------------------ .../TemplateName.java} | 13 +- .../repository/MailTrackerRepository.java | 6 +- .../mail/repository/TemplateRepository.java | 5 +- .../mail/service/ContextCreator.java | 23 +--- .../mail/service/MailService.java | 55 ++++++-- .../mail/service/event/MailEvent.java | 2 +- .../service/vo/EventStatusChangeMailInfo.java | 36 +++++- .../mail/service/vo/WelcomeMailInfo.java | 17 ++- .../user/repository/UserRepository.java | 3 + .../spaceclub/user/service/UserProvider.java | 2 + .../spaceclub/user/service/UserService.java | 5 + .../club/controller/ClubControllerTest.java | 1 - ...t.java => TemplateNameRepositoryTest.java} | 29 +++-- 16 files changed, 229 insertions(+), 183 deletions(-) create mode 100644 src/main/java/com/spaceclub/notification/mail/domain/MailHistory.java delete mode 100644 src/main/java/com/spaceclub/notification/mail/domain/MailTracker.java rename src/main/java/com/spaceclub/notification/mail/{service/vo/Template.java => domain/TemplateName.java} (66%) rename src/test/java/com/spaceclub/notification/mail/repository/{TemplateRepositoryTest.java => TemplateNameRepositoryTest.java} (72%) diff --git a/src/main/java/com/spaceclub/form/service/SubmitService.java b/src/main/java/com/spaceclub/form/service/SubmitService.java index 8b4b7443..a4e280bb 100644 --- a/src/main/java/com/spaceclub/form/service/SubmitService.java +++ b/src/main/java/com/spaceclub/form/service/SubmitService.java @@ -70,10 +70,11 @@ public void updateStatus(FormSubmitUpdateInfo updateInfo) { eventProvider.minusParticipants(event, eventUser.getTicketCount()); } + // 신청상태가 이렇게 변했다. 라는 이벤트 발행 EventUser updatedEventUser = eventUser.updateStatus(updateInfo.status()); // 폼 상태 변경 시 메일 발송 - String userEmailAddress = userProvider.getProfile(eventUser.getUserId()).email(); + String userEmailAddress = userProvider.getEmailAddress(eventUser.getUserId()); applicationEventPublisher.publishEvent(MailEvent.createMailEvent(userEmailAddress, event.getClubName(), event.getTitle(), updateInfo.status().toString())); eventUserRepository.save(updatedEventUser); diff --git a/src/main/java/com/spaceclub/notification/mail/domain/MailHistory.java b/src/main/java/com/spaceclub/notification/mail/domain/MailHistory.java new file mode 100644 index 00000000..1df64ab6 --- /dev/null +++ b/src/main/java/com/spaceclub/notification/mail/domain/MailHistory.java @@ -0,0 +1,90 @@ +package com.spaceclub.notification.mail.domain; + +import jakarta.persistence.Column; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; +import lombok.extern.slf4j.Slf4j; + +import java.time.LocalDateTime; + +import static jakarta.persistence.GenerationType.IDENTITY; +import static lombok.AccessLevel.PROTECTED; + +@Slf4j +@Entity +@ToString +@EqualsAndHashCode(of = "id") +@NoArgsConstructor(access = PROTECTED) +public class MailHistory { + + @Id + @Getter + @Column(name = "mail_history_id") + @GeneratedValue(strategy = IDENTITY) + private Long id; + + @Getter + private String addresses; + + @Getter + private String title; + + @Getter + @Enumerated(EnumType.STRING) + private TemplateName templateName; + + @Getter + private Long templateId; + + @Embedded + private AdditionalInfo additionalInfo; + + private LocalDateTime sentAt; + + private boolean isSent; + + @Builder + private MailHistory( + String addresses, + String title, + TemplateName templateName, + LocalDateTime sentAt, + boolean isSent, + AdditionalInfo additionalInfo + ) { + this.addresses = addresses; + this.title = title; + this.templateName = templateName; + this.templateId = templateName.getTemplateId(); + this.sentAt = sentAt; + this.isSent = isSent; + log.info("MailTracker created: {}", this); + } + + public void changeToSent() { + this.sentAt = LocalDateTime.now(); + this.isSent = true; + } + + public String getClubName() { + return additionalInfo.getClubName(); + } + + public String getEventName() { + return additionalInfo.getEventName(); + } + + public String getEventStatus() { + return additionalInfo.getEventStatus(); + } + +} diff --git a/src/main/java/com/spaceclub/notification/mail/domain/MailTracker.java b/src/main/java/com/spaceclub/notification/mail/domain/MailTracker.java deleted file mode 100644 index b575c83b..00000000 --- a/src/main/java/com/spaceclub/notification/mail/domain/MailTracker.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.spaceclub.notification.mail.domain; - -import com.spaceclub.notification.mail.service.vo.EventStatusChangeMailInfo; -import com.spaceclub.notification.mail.service.vo.MailInfo; -import com.spaceclub.notification.mail.service.vo.Template; -import com.spaceclub.notification.mail.service.vo.WelcomeMailInfo; -import jakarta.persistence.Column; -import jakarta.persistence.Embedded; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import lombok.Builder; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.ToString; -import lombok.extern.slf4j.Slf4j; - -import java.time.LocalDateTime; - -import static jakarta.persistence.GenerationType.IDENTITY; -import static lombok.AccessLevel.PROTECTED; - -@Slf4j -@Entity -@ToString -@EqualsAndHashCode(of = "id") -@NoArgsConstructor(access = PROTECTED) -public class MailTracker { - - @Id - @Getter - @Column(name = "mail_tracker_id") - @GeneratedValue(strategy = IDENTITY) - private Long id; - - @Getter - private String addresses; - - @Getter - private String title; - - @Getter - private String templateName; - - @Getter - private Long templateId; - - @Embedded - private AdditionalInfo additionalInfo; - - private LocalDateTime sentAt; - - private boolean isSent; - - @Builder - private MailTracker( - String addresses, - String title, - String template, - LocalDateTime sentAt, - boolean isSent, - AdditionalInfo additionalInfo - ) { - this.addresses = addresses; - this.title = title; - this.templateName = template; - this.templateId = setTemplateId(template); - this.sentAt = sentAt; - this.isSent = isSent; - log.info("MailTracker created: {}", this); - } - - public static MailTracker from(MailInfo mailInfo, boolean isSent) { - if (mailInfo instanceof EventStatusChangeMailInfo eventStatusChangeMailInfo) { - return MailTracker.builder() - .addresses(String.join(",", mailInfo.email())) - .title(mailInfo.title()) - .template(mailInfo.templateName()) - .sentAt(LocalDateTime.now()) - .isSent(isSent) - .additionalInfo( - new AdditionalInfo(eventStatusChangeMailInfo.getClubName(), - eventStatusChangeMailInfo.getEventName(), - eventStatusChangeMailInfo.getEventStatus() - ) - ) - .build(); - } - return MailTracker.builder() - .addresses(String.join(",", mailInfo.email())) - .title(mailInfo.title()) - .template(mailInfo.templateName()) - .sentAt(LocalDateTime.now()) - .isSent(isSent) - .build(); - } - - private Long setTemplateId(String template) { - Template foundTemplate = Template.findByTemplateName(template); - - return switch (foundTemplate) { - case WELCOME -> 1L; - case EVENT_STATUS_CHANGED -> 2L; - }; - } - - public void changeToSent() { - this.sentAt = LocalDateTime.now(); - this.isSent = true; - } - - public MailInfo toMailInfo() { - Template template = Template.findByTemplateName(templateName); - - return switch (template) { - case WELCOME -> WelcomeMailInfo.from(addresses); - case EVENT_STATUS_CHANGED -> EventStatusChangeMailInfo.of(addresses, additionalInfo.getClubName(), additionalInfo.getEventName(), additionalInfo.getEventStatus()); - }; - } - -} diff --git a/src/main/java/com/spaceclub/notification/mail/service/vo/Template.java b/src/main/java/com/spaceclub/notification/mail/domain/TemplateName.java similarity index 66% rename from src/main/java/com/spaceclub/notification/mail/service/vo/Template.java rename to src/main/java/com/spaceclub/notification/mail/domain/TemplateName.java index f7ed718d..8bf1dde2 100644 --- a/src/main/java/com/spaceclub/notification/mail/service/vo/Template.java +++ b/src/main/java/com/spaceclub/notification/mail/domain/TemplateName.java @@ -1,4 +1,4 @@ -package com.spaceclub.notification.mail.service.vo; +package com.spaceclub.notification.mail.domain; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -7,20 +7,23 @@ @Getter @RequiredArgsConstructor -public enum Template { +public enum TemplateName { - WELCOME("[Space Club] 스페이스 클럽에 가입해주셔서 감사합니다.", "welcome"), - EVENT_STATUS_CHANGED("[Space Club] 신청한 행사 상태 변경 알림", "event-status-change"), + WELCOME("[Space Club] 스페이스 클럽에 가입해주셔서 감사합니다.", "welcome", 1L), + EVENT_STATUS_CHANGED("[Space Club] 신청한 행사 상태 변경 알림", "event-status-change", 2L), ; private final String title; private final String templateName; - public static Template findByTemplateName(String templateName) { + private final Long templateId; + + public static TemplateName findByTemplateName(String templateName) { return Arrays.stream(values()) .filter(template -> template.templateName.equals(templateName)) .findFirst() .orElseThrow(); } + } diff --git a/src/main/java/com/spaceclub/notification/mail/repository/MailTrackerRepository.java b/src/main/java/com/spaceclub/notification/mail/repository/MailTrackerRepository.java index e839ec35..8d3b1891 100644 --- a/src/main/java/com/spaceclub/notification/mail/repository/MailTrackerRepository.java +++ b/src/main/java/com/spaceclub/notification/mail/repository/MailTrackerRepository.java @@ -1,14 +1,14 @@ package com.spaceclub.notification.mail.repository; -import com.spaceclub.notification.mail.domain.MailTracker; +import com.spaceclub.notification.mail.domain.MailHistory; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; -public interface MailTrackerRepository extends JpaRepository { +public interface MailTrackerRepository extends JpaRepository { int countByIsSentFalse(); - Slice findAllByIsSentFalse(Pageable pageable); + Slice findAllByIsSentFalse(Pageable pageable); } diff --git a/src/main/java/com/spaceclub/notification/mail/repository/TemplateRepository.java b/src/main/java/com/spaceclub/notification/mail/repository/TemplateRepository.java index 32aca4f0..f736b823 100644 --- a/src/main/java/com/spaceclub/notification/mail/repository/TemplateRepository.java +++ b/src/main/java/com/spaceclub/notification/mail/repository/TemplateRepository.java @@ -1,6 +1,7 @@ package com.spaceclub.notification.mail.repository; import com.spaceclub.notification.mail.domain.Template; +import com.spaceclub.notification.mail.domain.TemplateName; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -8,8 +9,8 @@ public interface TemplateRepository extends JpaRepository { @Query("select distinct t.template from Template t " + - "join fetch MailTracker m on m.templateId = t.id " + + "join fetch MailHistory m on m.templateId = t.id " + "where m.templateName = :templateName") - String findTemplateByTemplateName(@Param("templateName") String templateName); + String findTemplateByTemplateName(@Param("templateName") TemplateName templateName); } diff --git a/src/main/java/com/spaceclub/notification/mail/service/ContextCreator.java b/src/main/java/com/spaceclub/notification/mail/service/ContextCreator.java index 570d280e..05072102 100644 --- a/src/main/java/com/spaceclub/notification/mail/service/ContextCreator.java +++ b/src/main/java/com/spaceclub/notification/mail/service/ContextCreator.java @@ -1,9 +1,6 @@ package com.spaceclub.notification.mail.service; import com.spaceclub.notification.mail.MailProperties; -import com.spaceclub.notification.mail.service.vo.EventStatusChangeMailInfo; -import com.spaceclub.notification.mail.service.vo.MailInfo; -import com.spaceclub.notification.mail.service.vo.WelcomeMailInfo; import org.thymeleaf.context.Context; import java.util.Map; @@ -24,24 +21,10 @@ public ContextCreator(MailProperties mailProperties) { context.setVariables(emailValues); } - public Context createContext(MailInfo mailInfo) { - if (mailInfo instanceof WelcomeMailInfo) { - return this.context; - } - if (mailInfo instanceof EventStatusChangeMailInfo eventStatusChangeMailInfo) { - addAdditionalInfo(eventStatusChangeMailInfo); - return this.context; - } - throw new IllegalArgumentException("Unknown mailInfo type: " + mailInfo.getClass().getName()); - } - - private void addAdditionalInfo(EventStatusChangeMailInfo eventStatusChangeMailInfo) { - Map additionalInfo = Map.of( - "clubName", eventStatusChangeMailInfo.getClubName(), - "eventName", eventStatusChangeMailInfo.getEventName(), - "eventStatus", eventStatusChangeMailInfo.getEventStatus() - ); + public Context createContext(Map additionalInfo) { this.context.setVariables(additionalInfo); + + return this.context; } } diff --git a/src/main/java/com/spaceclub/notification/mail/service/MailService.java b/src/main/java/com/spaceclub/notification/mail/service/MailService.java index ab01b260..ae744bb0 100644 --- a/src/main/java/com/spaceclub/notification/mail/service/MailService.java +++ b/src/main/java/com/spaceclub/notification/mail/service/MailService.java @@ -1,11 +1,14 @@ package com.spaceclub.notification.mail.service; import com.spaceclub.notification.mail.MailProperties; -import com.spaceclub.notification.mail.domain.MailTracker; +import com.spaceclub.notification.mail.domain.MailHistory; +import com.spaceclub.notification.mail.domain.TemplateName; import com.spaceclub.notification.mail.repository.MailTrackerRepository; import com.spaceclub.notification.mail.repository.TemplateRepository; import com.spaceclub.notification.mail.service.event.MailEvent; +import com.spaceclub.notification.mail.service.vo.EventStatusChangeMailInfo; import com.spaceclub.notification.mail.service.vo.MailInfo; +import com.spaceclub.notification.mail.service.vo.WelcomeMailInfo; import jakarta.mail.MessagingException; import jakarta.mail.internet.MimeMessage; import lombok.RequiredArgsConstructor; @@ -24,6 +27,9 @@ import org.thymeleaf.context.Context; import org.thymeleaf.spring6.SpringTemplateEngine; +import java.util.Collections; +import java.util.Map; + import static com.fasterxml.jackson.core.JsonEncoding.UTF8; import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; @@ -47,10 +53,13 @@ public class MailService { @TransactionalEventListener public void sendEmail(MailEvent mailEvent) { MailInfo mailInfo = mailEvent.mailInfo(); - Context context = new ContextCreator(mailProperties).createContext(mailInfo); - String templateName = mailInfo.templateName(); + TemplateName templateName = TemplateName.findByTemplateName(mailInfo.templateName()); String templateHtml = templateRepository.findTemplateByTemplateName(templateName); + + Map additionalInfo = getAdditionalInfo(templateName, mailInfo); + Context context = new ContextCreator(mailProperties).createContext(additionalInfo); + String html = templateEngine.process(templateHtml, context); boolean isSent = true; @@ -61,7 +70,11 @@ public void sendEmail(MailEvent mailEvent) { log.error("메일 전송 실패", e); isSent = false; } finally { - MailTracker mailHistory = MailTracker.from(mailInfo, isSent); + MailHistory mailHistory = switch (templateName) { + case WELCOME -> WelcomeMailInfo.createMailHistory(mailInfo, isSent); + case EVENT_STATUS_CHANGED -> EventStatusChangeMailInfo.from(mailInfo, isSent); + }; + mailTrackerRepository.save(mailHistory); } log.debug("메일 발송 완료!"); @@ -71,16 +84,17 @@ public void sendEmail(MailEvent mailEvent) { @Transactional(propagation = REQUIRES_NEW) public void sendEmail(int partitionNumber, int chunkSize) { Pageable pageRequest = PageRequest.of(partitionNumber, chunkSize); - Slice mailTrackers = mailTrackerRepository.findAllByIsSentFalse(pageRequest); + Slice mailTrackers = mailTrackerRepository.findAllByIsSentFalse(pageRequest); mailTrackers.forEach(this::sendEachMail); } - private void sendEachMail(MailTracker mailTracker) { - MailInfo mailInfo = mailTracker.toMailInfo(); - Context context = new ContextCreator(mailProperties).createContext(mailInfo); + private void sendEachMail(MailHistory mailHistory) { + MailInfo mailInfo = getInfo(mailHistory); + Map additionalInfo = getAdditionalInfo(mailHistory.getTemplateName(), mailInfo); + Context context = new ContextCreator(mailProperties).createContext(additionalInfo); - String templateHtml = templateRepository.findById(mailTracker.getTemplateId()).orElseThrow().getTemplate(); + String templateHtml = templateRepository.findById(mailHistory.getTemplateId()).orElseThrow().getTemplate(); String html = templateEngine.process(templateHtml, context); try { @@ -90,10 +104,31 @@ private void sendEachMail(MailTracker mailTracker) { log.error("메일 전송 실패", e); return; } - mailTracker.changeToSent(); + mailHistory.changeToSent(); log.debug("메일 재전송 완료"); } + private Map getAdditionalInfo(TemplateName templateName, MailInfo mailInfo) { + return switch (templateName) { + case WELCOME -> Collections.emptyMap(); + case EVENT_STATUS_CHANGED -> { + EventStatusChangeMailInfo eventStatusChangeMailInfo = (EventStatusChangeMailInfo) mailInfo; + yield Map.of( + "clubName", eventStatusChangeMailInfo.getClubName(), + "eventName", eventStatusChangeMailInfo.getEventName(), + "eventStatus", eventStatusChangeMailInfo.getEventStatus() + ); + } + }; + } + + private MailInfo getInfo(MailHistory mailHistory) { + return switch (mailHistory.getTemplateName()) { + case WELCOME -> WelcomeMailInfo.from(mailHistory.getAddresses()); + case EVENT_STATUS_CHANGED -> EventStatusChangeMailInfo.from(mailHistory); + }; + } + private MimeMessage createMailMessage(MailInfo mailInfo, String html) throws MessagingException { MimeMessage message = emailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, UTF8.getJavaName()); diff --git a/src/main/java/com/spaceclub/notification/mail/service/event/MailEvent.java b/src/main/java/com/spaceclub/notification/mail/service/event/MailEvent.java index d6ff8d33..73eac70d 100644 --- a/src/main/java/com/spaceclub/notification/mail/service/event/MailEvent.java +++ b/src/main/java/com/spaceclub/notification/mail/service/event/MailEvent.java @@ -13,7 +13,7 @@ public static MailEvent createMailEvent(String email) { } public static MailEvent createMailEvent(String email, String clubName, String eventName, String eventStatus) { - EventStatusChangeMailInfo mailInfo = EventStatusChangeMailInfo.of(email, clubName, eventName, eventStatus); + EventStatusChangeMailInfo mailInfo = EventStatusChangeMailInfo.from(email, clubName, eventName, eventStatus); return new MailEvent(mailInfo); } diff --git a/src/main/java/com/spaceclub/notification/mail/service/vo/EventStatusChangeMailInfo.java b/src/main/java/com/spaceclub/notification/mail/service/vo/EventStatusChangeMailInfo.java index 4c26bd41..d615ad7e 100644 --- a/src/main/java/com/spaceclub/notification/mail/service/vo/EventStatusChangeMailInfo.java +++ b/src/main/java/com/spaceclub/notification/mail/service/vo/EventStatusChangeMailInfo.java @@ -1,8 +1,13 @@ package com.spaceclub.notification.mail.service.vo; +import com.spaceclub.notification.mail.domain.AdditionalInfo; +import com.spaceclub.notification.mail.domain.MailHistory; +import com.spaceclub.notification.mail.domain.TemplateName; import lombok.Getter; -import static com.spaceclub.notification.mail.service.vo.Template.*; +import java.time.LocalDateTime; + +import static com.spaceclub.notification.mail.domain.TemplateName.*; @Getter public class EventStatusChangeMailInfo extends MailInfo { @@ -20,13 +25,38 @@ private EventStatusChangeMailInfo(String[] addresses, String clubName, String ev this.eventStatus = eventStatus; } - public static EventStatusChangeMailInfo of(String email, String clubName, String eventName, String eventStatus) { + public static EventStatusChangeMailInfo from(String email, String clubName, String eventName, String eventStatus) { String[] addressArray = email.split(DELIMITER); - String statusName = ParticipationStatus.getStatusName(eventStatus); + return new EventStatusChangeMailInfo(addressArray, clubName, eventName, statusName); } + public static EventStatusChangeMailInfo from(MailHistory mailHistory) { + String[] addressArray = mailHistory.getAddresses().split(DELIMITER); + String statusName = ParticipationStatus.getStatusName(mailHistory.getEventStatus()); + + return new EventStatusChangeMailInfo(addressArray, mailHistory.getClubName(), mailHistory.getEventName(), statusName); + } + + public static MailHistory from(MailInfo mailInfo, boolean isSent) { + EventStatusChangeMailInfo eventStatusChangeMailInfo = (EventStatusChangeMailInfo) mailInfo; + + return MailHistory.builder() + .addresses(String.join(DELIMITER, mailInfo.email())) + .title(mailInfo.title()) + .templateName(TemplateName.findByTemplateName(mailInfo.templateName())) + .sentAt(LocalDateTime.now()) + .isSent(isSent) + .additionalInfo( + new AdditionalInfo(eventStatusChangeMailInfo.getClubName(), + eventStatusChangeMailInfo.getEventName(), + eventStatusChangeMailInfo.getEventStatus() + ) + ) + .build(); + } + @Override public String title() { return EVENT_STATUS_CHANGED.getTitle(); diff --git a/src/main/java/com/spaceclub/notification/mail/service/vo/WelcomeMailInfo.java b/src/main/java/com/spaceclub/notification/mail/service/vo/WelcomeMailInfo.java index 2e014528..992a02d6 100644 --- a/src/main/java/com/spaceclub/notification/mail/service/vo/WelcomeMailInfo.java +++ b/src/main/java/com/spaceclub/notification/mail/service/vo/WelcomeMailInfo.java @@ -1,6 +1,11 @@ package com.spaceclub.notification.mail.service.vo; -import static com.spaceclub.notification.mail.service.vo.Template.WELCOME; +import com.spaceclub.notification.mail.domain.MailHistory; +import com.spaceclub.notification.mail.domain.TemplateName; + +import java.time.LocalDateTime; + +import static com.spaceclub.notification.mail.domain.TemplateName.WELCOME; public class WelcomeMailInfo extends MailInfo { @@ -16,6 +21,16 @@ public static WelcomeMailInfo from(String email) { return new WelcomeMailInfo(addressArray); } + public static MailHistory createMailHistory(MailInfo mailInfo, boolean isSent) { + return MailHistory.builder() + .addresses(String.join(DELIMITER, mailInfo.email())) + .title(mailInfo.title()) + .templateName(TemplateName.findByTemplateName(mailInfo.templateName())) + .sentAt(LocalDateTime.now()) + .isSent(isSent) + .build(); + } + @Override public String title() { return WELCOME.getTitle(); diff --git a/src/main/java/com/spaceclub/user/repository/UserRepository.java b/src/main/java/com/spaceclub/user/repository/UserRepository.java index bf1c3932..9db8dd5d 100644 --- a/src/main/java/com/spaceclub/user/repository/UserRepository.java +++ b/src/main/java/com/spaceclub/user/repository/UserRepository.java @@ -28,4 +28,7 @@ Optional findByEmailAndOauthUserName( """) List findAllUserToDelete(@Param("threeDaysAgoFromNow") LocalDateTime threeDaysAgoFromNow); + @Query("select u.email.email from User u where u.id = :userId and u.email.emailConsent = true") + Optional findEmail(Long userId); + } diff --git a/src/main/java/com/spaceclub/user/service/UserProvider.java b/src/main/java/com/spaceclub/user/service/UserProvider.java index 33ebc609..193db56e 100644 --- a/src/main/java/com/spaceclub/user/service/UserProvider.java +++ b/src/main/java/com/spaceclub/user/service/UserProvider.java @@ -8,4 +8,6 @@ public interface UserProvider { void validateUser(Long userId); + String getEmailAddress(Long userId); + } diff --git a/src/main/java/com/spaceclub/user/service/UserService.java b/src/main/java/com/spaceclub/user/service/UserService.java index 2b7397b1..48c604d2 100644 --- a/src/main/java/com/spaceclub/user/service/UserService.java +++ b/src/main/java/com/spaceclub/user/service/UserService.java @@ -54,6 +54,11 @@ public void validateUser(Long userId) { if (!userRepository.existsById(userId)) throw new IllegalArgumentException(USER_NOT_FOUND.toString()); } + @Override + public String getEmailAddress(Long userId) { + return userRepository.findEmail(userId).orElseThrow(() -> new IllegalStateException("유저가 동의하지 않습니다.")); + } + public User getUser(Long userId) { return userRepository.findById(userId) .orElseThrow(() -> new IllegalArgumentException(USER_NOT_FOUND.toString())); diff --git a/src/test/java/com/spaceclub/club/controller/ClubControllerTest.java b/src/test/java/com/spaceclub/club/controller/ClubControllerTest.java index 8030164c..c6dd8894 100644 --- a/src/test/java/com/spaceclub/club/controller/ClubControllerTest.java +++ b/src/test/java/com/spaceclub/club/controller/ClubControllerTest.java @@ -87,7 +87,6 @@ class ClubControllerTest { @Test @WithMockUser void 클럽_생성에_성공한다() throws Exception { - // given given(clubService.createClub(any(Club.class), any(), any(MultipartFile.class))).willReturn( Club.builder() diff --git a/src/test/java/com/spaceclub/notification/mail/repository/TemplateRepositoryTest.java b/src/test/java/com/spaceclub/notification/mail/repository/TemplateNameRepositoryTest.java similarity index 72% rename from src/test/java/com/spaceclub/notification/mail/repository/TemplateRepositoryTest.java rename to src/test/java/com/spaceclub/notification/mail/repository/TemplateNameRepositoryTest.java index eb1c9062..d3dec594 100644 --- a/src/test/java/com/spaceclub/notification/mail/repository/TemplateRepositoryTest.java +++ b/src/test/java/com/spaceclub/notification/mail/repository/TemplateNameRepositoryTest.java @@ -1,7 +1,8 @@ package com.spaceclub.notification.mail.repository; -import com.spaceclub.notification.mail.domain.MailTracker; +import com.spaceclub.notification.mail.domain.MailHistory; import com.spaceclub.notification.mail.domain.Template; +import com.spaceclub.notification.mail.domain.TemplateName; import jakarta.persistence.EntityManager; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; @@ -17,7 +18,7 @@ @DataJpaTest @DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) -class TemplateRepositoryTest { +class TemplateNameRepositoryTest { @Autowired EntityManager em; @@ -31,31 +32,31 @@ void setUp() { .template("template 2") .build(); - MailTracker mailTracker1 = MailTracker.builder() + MailHistory mailHistory1 = MailHistory.builder() .addresses("abcd@naver.com") .title("title 1") - .template("welcome") + .templateName(TemplateName.WELCOME) .sentAt(LocalDateTime.now()) .isSent(true) .build(); - MailTracker mailTracker2 = MailTracker.builder() + MailHistory mailHistory2 = MailHistory.builder() .addresses("zxcv@naver.com") .title("title 2") - .template("event-status-change") + .templateName(TemplateName.EVENT_STATUS_CHANGED) .sentAt(LocalDateTime.now()) .isSent(true) .build(); - MailTracker mailTracker3 = MailTracker.builder() + MailHistory mailHistory3 = MailHistory.builder() .addresses("asdf@naver.com") .title("title 1") - .template("welcome") + .templateName(TemplateName.WELCOME) .sentAt(LocalDateTime.now()) .isSent(false) .build(); - em.persist(mailTracker1); - em.persist(mailTracker2); - em.persist(mailTracker3); + em.persist(mailHistory1); + em.persist(mailHistory2); + em.persist(mailHistory3); em.persist(template1); em.persist(template2); @@ -65,11 +66,11 @@ void setUp() { } @ParameterizedTest - @CsvSource({"event-status-change, template 2", "welcome, template 1"}) - void findByTemplateName(String templateName, String expectedTemplate) { + @CsvSource({"EVENT_STATUS_CHANGED,template 2", "WELCOME,template 1"}) + void findByTemplateName(TemplateName templateName, String expectedTemplate) { // when String singleResult = em.createQuery("select distinct t.template from Template t " + - "join fetch MailTracker m on m.templateId = t.id " + + "join fetch MailHistory m on m.templateId = t.id " + "where m.templateName = :templateName", String.class) .setParameter("templateName", templateName) .getSingleResult();