Skip to content

Commit

Permalink
메일 시스템 리펙토링 (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
juno-junho authored Dec 30, 2023
1 parent 4c9dc20 commit 6325dfa
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 183 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/spaceclub/form/service/SubmitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
122 changes: 0 additions & 122 deletions src/main/java/com/spaceclub/notification/mail/domain/MailTracker.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.spaceclub.notification.mail.service.vo;
package com.spaceclub.notification.mail.domain;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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<MailTracker, Long> {
public interface MailTrackerRepository extends JpaRepository<MailHistory, Long> {

int countByIsSentFalse();

Slice<MailTracker> findAllByIsSentFalse(Pageable pageable);
Slice<MailHistory> findAllByIsSentFalse(Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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;

public interface TemplateRepository extends JpaRepository<Template, Long> {

@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);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, Object> additionalInfo = Map.of(
"clubName", eventStatusChangeMailInfo.getClubName(),
"eventName", eventStatusChangeMailInfo.getEventName(),
"eventStatus", eventStatusChangeMailInfo.getEventStatus()
);
public Context createContext(Map<String, Object> additionalInfo) {
this.context.setVariables(additionalInfo);

return this.context;
}

}
Loading

0 comments on commit 6325dfa

Please sign in to comment.