Skip to content

Commit

Permalink
Merge pull request #58 from ssu-student-union/feat/55-onboarding
Browse files Browse the repository at this point in the history
[feat] 온보딩 실패 시 문의 메일 api 추가
  • Loading branch information
chahyunsoo authored Aug 16, 2024
2 parents 7d38d02 + 42dd4da commit b9f5138
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ dependencies {
//swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'

//mail
implementation 'org.springframework.boot:spring-boot-starter-mail'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ussum.homepage.application.user.service.OnBoardingService;
import ussum.homepage.application.user.service.dto.request.OnBoardingEmailRequest;
import ussum.homepage.application.user.service.dto.request.OnBoardingRequest;
import ussum.homepage.application.user.service.dto.request.UserRequest;
import ussum.homepage.application.user.service.dto.response.UserResponse;
Expand All @@ -19,9 +20,15 @@ public class OnBoardingController {

@PostMapping("/academy-information")
public ResponseEntity<ApiResponse<?>> createUserOnBoarding(@UserId Long userId,
@RequestBody OnBoardingRequest request){
@RequestBody OnBoardingRequest request) {
onBoardingService.saveUserOnBoarding(userId, request);
return ApiResponse.success(null);
}

@PostMapping("/mail")
public ResponseEntity<ApiResponse<?>> sendEmail(@RequestBody OnBoardingEmailRequest onBoardingEmailRequest) {
onBoardingService.sendEmail(onBoardingEmailRequest);
return ApiResponse.success(null);
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package ussum.homepage.application.user.service;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ussum.homepage.application.user.service.dto.request.OnBoardingEmailRequest;
import ussum.homepage.application.user.service.dto.request.OnBoardingRequest;
import ussum.homepage.domain.csv_user.StudentCsv;
import ussum.homepage.domain.csv_user.service.StudentCsvReader;
import ussum.homepage.domain.member.Member;
import ussum.homepage.domain.member.service.MemberAppender;
import ussum.homepage.domain.user.User;
import ussum.homepage.domain.user.exception.OnBoardingMessagingException;
import ussum.homepage.domain.user.service.UserModifier;
import ussum.homepage.domain.user.service.UserReader;
import ussum.homepage.global.error.exception.GeneralException;

import static ussum.homepage.global.error.status.ErrorStatus.ONBOARDING_FAIL_MAIL_ERROR;
import static ussum.homepage.global.error.status.ErrorStatus.USER_NOT_FOUND;

@Service
Expand All @@ -21,9 +29,13 @@
public class OnBoardingService {
private final UserReader userReader;
private final StudentCsvReader studentCsvReader;
private final JavaMailSender javaMailSender;
private final MemberAppender memberAppender;
private final UserModifier userModifier;

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

@Transactional
public void saveUserOnBoarding(Long userId, OnBoardingRequest request){
User user = userReader.getUserWithId(userId);
Expand All @@ -36,4 +48,19 @@ public void saveUserOnBoarding(Long userId, OnBoardingRequest request){
request.getMemberCode(), request.getMajorCode(),
userId, null));
}

public void sendEmail(OnBoardingEmailRequest onBoardingEmailRequest) {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
mimeMessageHelper.setTo(SENDER_EMAIL_ADDRESS);
mimeMessageHelper.setFrom(onBoardingEmailRequest.email());
mimeMessageHelper.setSubject(onBoardingEmailRequest.toString(onBoardingEmailRequest));
mimeMessageHelper.setText(onBoardingEmailRequest.content());

javaMailSender.send(mimeMessage);
} catch (MessagingException | OnBoardingMessagingException onBoardingMessagingException) {
throw new OnBoardingMessagingException(ONBOARDING_FAIL_MAIL_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ussum.homepage.application.user.service.dto.request;


public record OnBoardingEmailRequest(
String name,
Long studentId,
String email,
String content
) {
public String toString(OnBoardingEmailRequest onBoardingEmailRequest) {
return onBoardingEmailRequest.studentId + "학번과 "
+ onBoardingEmailRequest.email + " 라는 이메일을 가진 "
+ onBoardingEmailRequest.name + "님이 "
+ "메일을 보냈습니다.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ussum.homepage.domain.user.exception;

import ussum.homepage.global.error.code.BaseErrorCode;
import ussum.homepage.global.error.exception.GeneralException;

public class OnBoardingMessagingException extends GeneralException {
private BaseErrorCode baseErrorCode;
public OnBoardingMessagingException(BaseErrorCode baseErrorCode) {
super(baseErrorCode);
}
}
49 changes: 49 additions & 0 deletions src/main/java/ussum/homepage/global/config/MailConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ussum.homepage.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 MailConfig {
@Value("${spring.mail.host}")
private String MAIL_HOST;

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

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

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

// SMTP 서버 주소를 'smtp.gmail.com'으로 변경
javaMailSender.setHost(MAIL_HOST);
javaMailSender.setUsername(MAIL_ADDRESS); // 사용자 이메일 주소
javaMailSender.setPassword(MAIL_PASSWORD); // 앱 비밀번호 또는 사용자 비밀번호

// TLS를 사용하는 587 포트 설정
javaMailSender.setPort(587);
javaMailSender.setJavaMailProperties(getMailProperties());

return javaMailSender;
}

private Properties getMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true"); // TLS 활성화
properties.setProperty("mail.debug", "true");

// 'smtp.gmail.com'으로 변경
properties.setProperty("mail.smtp.ssl.trust", "smtp.gmail.com");
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public enum ErrorStatus implements BaseErrorCode {

//온보딩에러
INVALID_ONBOARDING_REQUEST(HttpStatus.BAD_REQUEST,"ONBOARDING_001","온보딩 정보가 올바르지 않습니다."),
ONBOARDING_FAIL_MAIL_ERROR(HttpStatus.BAD_REQUEST, "ONBOARDING_002","메일이 정상적으로 보내지지 않았습니다."),

//S3에러
S3_ERROR(HttpStatus.INTERNAL_SERVER_ERROR,"S3_001","S3에 파일 저장이 실패했습니다."),
Expand Down

0 comments on commit b9f5138

Please sign in to comment.