Skip to content

Commit

Permalink
Merge pull request #254 from techeer-sv/BE/#235
Browse files Browse the repository at this point in the history
BE/#235 알림 발생 시 이메일 전송 기능 추가
  • Loading branch information
kimhalin committed Oct 2, 2023
2 parents 126c359 + b967b5c commit 655f715
Show file tree
Hide file tree
Showing 11 changed files with 504 additions and 10 deletions.
2 changes: 2 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ dependencies {
// spring-boot
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

implementation 'org.json:json:20210307'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,4 @@ public class Notification extends BaseEntity {

@Column(nullable = false)
private boolean isRead;

@Column(nullable = false)
private boolean isEmailSent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class NotificationDto {
private Member member;
private String content;
private boolean isRead = false;
private boolean isEmailSent = false;

public void setMember(Member member) {
this.member = member;
Expand All @@ -26,7 +25,6 @@ public Notification toEntity() {
.member(member)
.content(content)
.isRead(this.isRead)
.isEmailSent(this.isEmailSent)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.graphy.backend.domain.notification.service;

import com.graphy.backend.domain.notification.domain.Notification;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class MailingService {

private final JavaMailSender javaMailSender;
private final SpringTemplateEngine templateEngine;

private static final String EMAIL_TITLE_PREFIX = "[Graphy] ";

@Async
public void sendNotificationEmail(Notification notification) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");

messageHelper.setSubject(EMAIL_TITLE_PREFIX + notification.getContent());
messageHelper.setTo(notification.getMember().getEmail());

HashMap<String, String> emailValues = new HashMap<>();
emailValues.put("content", notification.getContent());
String text = setContext(emailValues);

messageHelper.setText(text, true);

messageHelper.addInline("logo", new ClassPathResource("static/images/image-2.png"));
messageHelper.addInline("notice-icon", new ClassPathResource("static/images/image-1.png"));

javaMailSender.send(message);
}

private String setContext(Map<String, String> emailValues) {
Context context = new Context();
emailValues.forEach(context::setVariable);
return templateEngine.process("email/index", context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,35 @@

import com.graphy.backend.domain.member.domain.Member;
import com.graphy.backend.domain.member.service.MemberService;
import com.graphy.backend.domain.notification.domain.Notification;
import com.graphy.backend.domain.notification.dto.NotificationDto;
import com.graphy.backend.domain.notification.repository.NotificationRepository;
import com.graphy.backend.global.error.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;

import static com.graphy.backend.global.error.ErrorCode.SEND_EMAIL_FAIL;

@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class NotificationService {
private final NotificationRepository notificationRepository;
private final MemberService memberService;
private final MailingService mailingService;

@Transactional
public void addNotification(NotificationDto dto, Long memberId) {
Member member = memberService.findMemberById(memberId);
dto.setMember(member);

notificationRepository.save(dto.toEntity());
Notification entity = notificationRepository.save(dto.toEntity());
try {
mailingService.sendNotificationEmail(entity);
} catch (MessagingException e) {
throw new BusinessException(SEND_EMAIL_FAIL);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(30);
executor.setQueueCapacity(50);
executor.setThreadNamePrefix("LSH-ASYNC-");
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("ASYNC-");
executor.initialize();
return executor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.graphy.backend.global.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

@Configuration
public class JavaMailConfig {

@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
@Value("${spring.mail.host}")
private String host;

@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

javaMailSender.setProtocol("smtp");
javaMailSender.setHost(host);
javaMailSender.setUsername(username);
javaMailSender.setPassword(password);
javaMailSender.setJavaMailProperties(getMailProperties());

return javaMailSender;
}

private Properties getMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public enum ErrorCode {
REQUEST_TOO_MUCH_TOKENS(HttpStatus.BAD_REQUEST, "AI001", "GPT에 보내야 할 요청 길이 제한 초과"),

// Message
MESSAGE_NOT_EXIST(HttpStatus.NOT_FOUND, "MSG001", "존재하지 않는 메세지");
MESSAGE_NOT_EXIST(HttpStatus.NOT_FOUND, "MSG001", "존재하지 않는 메세지"),

// Notification
SEND_EMAIL_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "EM001", "이메일 전송 실패");

private final HttpStatus status;
private final String errorCode;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 655f715

Please sign in to comment.