Skip to content

Commit

Permalink
Merge pull request #252 from haedoang/feature/report
Browse files Browse the repository at this point in the history
feat) report
  • Loading branch information
haedoang authored Dec 26, 2023
2 parents cdea96e + 72c7e3d commit 1b12ef6
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 13 deletions.
2 changes: 1 addition & 1 deletion config
6 changes: 5 additions & 1 deletion src/main/java/com/koliving/api/KolivingApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.koliving.api.location.domain.LocationType.GU;

import com.google.common.collect.Sets;
import com.koliving.api.email.EmailService;
import com.koliving.api.email.IEmailService;
import com.koliving.api.email.MailType;
import com.koliving.api.file.domain.ImageFile;
Expand Down Expand Up @@ -74,7 +75,8 @@ CommandLineRunner commandLineRunner(
UserRepository userRepository,
PasswordEncoder encoder,
NotificationRepository notificationRepository,
ReportReasonRepository reportReasonRepository
ReportReasonRepository reportReasonRepository,
IEmailService emailService
) {
return args -> {
initImageFiles(imageFileRepository);
Expand Down Expand Up @@ -197,6 +199,8 @@ private void initLanguages(LanguageRepository languageRepository) {
Language.valueOf("ko", "auth_email_subject", "이메일 인증을 완료하세요"),
Language.valueOf("en", "contact_email_subject", "Someone is interested in your posts."),
Language.valueOf("ko", "contact_email_subject", "누군가 당신의 게시글에 관심이 있어요."),
Language.valueOf("en", "report_email_subject", "Some post has been reported."),
Language.valueOf("ko", "report_email_subject", "게시글 신고가 접수되었습니다."),
Language.valueOf("en", "auth_email_subtitle", "Click the link below to proceed with authentication"),
Language.valueOf("ko", "auth_email_subtitle", "아래 링크를 클릭하셔서 인증을 진행하세요"),
Language.valueOf("en", "contact_email_subtitle", "Possible roommate has shown interest in your room!"),
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/koliving/api/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ private void init() {
"/api-docs/**",
"/swagger-ui/**",
"/swagger-resources/**",
"/api/v1/rooms/search"
"/api/v1/rooms/search",
"/api/v1/locations"
};

AUTHORIZATION_WHITELIST = new String[]{
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/com/koliving/api/email/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.koliving.api.properties.EmailProperties;
import com.koliving.api.properties.FrontProperties;
import com.koliving.api.room.domain.Room;
import com.koliving.api.room.infra.RoomContactEvent;
import com.koliving.api.user.domain.User;
import com.koliving.api.utils.HttpUtils;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void sendRoomContact(String to, String contact, String message, User send

@Async("mailExecutor")
@Override
public void send(MailType type, String to, String link) {
public void sendMailAuth(MailType type, String to, String link) {
try {
Locale currentLocale = httpUtils.getLocaleForLanguage(LocaleContextHolder.getLocale());

Expand Down Expand Up @@ -110,4 +111,39 @@ public void send(MailType type, String to, String link) {
throw new MailSendException("Failed to send email", e);
}
}

@Async("mailExecutor")
@Override
public void sendRoomReport(String roomLink, String reportReason, User reporter) {
try {
Locale currentLocale = httpUtils.getLocaleForLanguage(LocaleContextHolder.getLocale());

Map<String, Object> variables = new HashMap<>();
variables.put("title", "KOLIVING");
variables.put("roomLink", roomLink);
variables.put("userName", reporter.getFullName());
variables.put("userAge", reporter.getAge());
variables.put("userImageProfile", reporter.getImageProfile());
variables.put("userGender", reporter.getGender());
variables.put("userDescription", reporter.getDescription());
variables.put("reason", reportReason);

MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

helper.setSubject(messageSource.getMessage("report_email_subject", null, currentLocale));
helper.setTo(emailProperties.getUsername());
helper.setText(emailTemplateUtil.generateEmail(MailType.REPORT, variables), true);
helper.setFrom(emailProperties.getUsername());
helper.addInline("logo", new ClassPathResource("static/image/logo-black.jpg"));

mailSender.send(mimeMessage);
} catch (MessagingException e) {
log.error("failed to generate email", e);
throw new MailParseException("failed to generate email", e);
} catch (MailException e) {
log.error("failed to send email", e);
throw new MailSendException("Failed to send email", e);
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/koliving/api/email/IEmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

public interface IEmailService {

void send(MailType type, String to, String url);
void sendMailAuth(MailType type, String to, String url);

void sendRoomContact(String to, String contactInfo, String message, User sender, String link);

void sendRoomReport(String link, String reason, User reporter);
}
3 changes: 2 additions & 1 deletion src/main/java/com/koliving/api/email/MailType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

public enum MailType {
AUTH("auth-email-template"),
CONTACT("contact-email-template");
CONTACT("contact-email-template"),
REPORT("report-email-template");

private final String template;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.koliving.api.report.application;

import com.koliving.api.base.ServiceError;
import com.koliving.api.base.exception.KolivingServiceException;
import com.koliving.api.email.EmailService;
import com.koliving.api.properties.FrontProperties;
import com.koliving.api.report.application.dto.ReportReasonResponse;
import com.koliving.api.report.application.dto.ReportRequest;
import com.koliving.api.report.domain.ReportReason;
import com.koliving.api.room.domain.Room;
import com.koliving.api.room.infra.RoomRepository;
import com.koliving.api.user.domain.User;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,6 +24,9 @@
@RequiredArgsConstructor
public class ReportService {
private final ReportReasonRepository reportReasonRepository;
private final RoomRepository roomRepository;
private final EmailService emailService;
private final FrontProperties frontProperties;

public List<ReportReasonResponse> getReasons() {
return reportReasonRepository.findAll()
Expand All @@ -24,7 +35,17 @@ public List<ReportReasonResponse> getReasons() {
.collect(Collectors.toList());
}

public void saveReport(ReportRequest request) {
//TODO 리포트 로직 구현
public void saveReport(ReportRequest request, User user) {
final Room room = roomRepository.findById(request.roomId())
.orElseThrow(() -> new KolivingServiceException(ServiceError.RECORD_NOT_EXIST));

final ReportReason reportReason = reportReasonRepository.findById(request.reportId())
.orElseThrow(() -> new KolivingServiceException(ServiceError.RECORD_NOT_EXIST));

emailService.sendRoomReport(getRoomDetailUrl(room.getId()), reportReason.getName(), user);
}

public String getRoomDetailUrl(Long roomId) {
return String.format("%s/room/%d", frontProperties.getOrigin(), roomId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.koliving.api.report.application.ReportService;
import com.koliving.api.report.application.dto.ReportReasonResponse;
import com.koliving.api.report.application.dto.ReportRequest;
import com.koliving.api.user.domain.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -14,6 +15,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -70,7 +72,8 @@ public ResponseEntity<List<ReportReasonResponse>> getReasons() {
}
)
@PostMapping("/reasons")
public ResponseEntity<Void> report(@RequestBody ReportRequest request) {
public ResponseEntity<Void> report(@RequestBody ReportRequest request, @AuthenticationPrincipal User user) {
reportService.saveReport(request, user);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,4 @@ public void contact(RoomContactRequest request, User user) {
public String getRoomDetailUrl(Long roomId) {
return String.format("%s/room/%d", frontProperties.getOrigin(), roomId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void sendEmail(String email, String token, String linkPathResource) {
String authLinkParams = "?token=" + token + "&email=" + email;
String authLinkUrl = authLinkPath + authLinkParams;

emailService.send(MailType.AUTH, email, authLinkUrl);
emailService.sendMailAuth(MailType.AUTH, email, authLinkUrl);
}

@Override
Expand Down
177 changes: 177 additions & 0 deletions src/main/resources/templates/report-email-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title th:value="${title}"></title>
</head>
<body style="margin: 0; font-family: 'Pretendard', sans-serif">
<table
style="
width: 100%;
border-collapse: collapse;
margin: 0;
font-family: 'Pretendard', sans-serif;
"
>
<tr>
<td
style="
display: block;
padding: 20px 0;
background: #FF8E00;
text-align: center;
"
>
<img
src="https://kr.object.ncloudstorage.com/backend-bucket/images/templates/house.svg"
alt="house"
/>
</td>
</tr>
<tr>
<td style="display: block; padding: 20px; text-align: center">
<div
style="
color: #FF8E00;
text-align: center;
font-size: 18px;
font-weight: 600;
"
>
</div>
<div
style="
width: 50%;
display: block;
padding: 20px;
margin: 0 auto;
border-radius: 4px;
background: #FAFAFA;
"
>
<div
style="
width: 100%;
color: #212121;
text-align: center;
font-size: 18px;
font-weight: 600;
padding-bottom: 10px;
"
>
Reporter’s Profile
</div>
<div style="display: block; margin-bottom: 0px; text-align: center">
<div
style="
width: 50px;
height: 50px;
border-radius: 45px;
overflow: hidden;
background: #BDBDBD;
margin: 0 auto;
"
>
<img
style="width: 100%; height: 100%; object-fit: cover"
th:src="${userImageProfile}"
/>
</div>
<div
style="
color: #424242;
font-size: 14px;
font-style: normal;
font-weight: 600;
padding-top: 10px;
"
th:text="${userName}"
></div>
<div
style="
color: #757575;
font-size: 12px;
font-style: normal;
font-weight: 400;
"
>
<span th:text="${userAge}"></span>
<span>years old | </span>
<span th:text="${userGender}"></span>
</div>
</div>
<hr style="width: 100%; border: 1px solid #eee" />
<p
style="
color: #757575;
font-size: 12px;
font-style: normal;
font-weight: 400;
"
th:text="${userDescription}"
></p>
</div>
<div
style="
width: 50%;
display: block;
padding: 20px;
margin: 0 auto;
border-radius: 4px;
background: rgba(255, 142, 0, 0.1);
"
>
<div
style="
color: #FF8E00;
text-align: center;
font-size: 18px;
font-weight: 600;
width: 100%;
"
>
Report Reason
</div>
<p
style="
color: #424242;
font-family: Pretendard;
font-size: 14px;
font-style: normal;
font-weight: 400;
margin: 0;
"
th:text="${reason}"
></p>
</div>
<a
style="
display: block;
padding: 8px 10px;
margin: 0 auto;
border-radius: 2px;
border: 1px solid #FF8E00;
color: #FF8E00;
text-align: center;
font-size: 14px;
font-weight: 700;
width: 50%;
background-color: white;
text-decoration: none;
"
th:href="${roomLink}"
target="_blank"
>
<img
src="https://kr.object.ncloudstorage.com/backend-bucket/images/templates/house-cute.svg"
alt=""
style="width: 20px; height: auto; margin-right: 4px"
/>
Go to room
</a>
</td>
</tr>
</table>
</body>
</html>
2 changes: 1 addition & 1 deletion src/test/java/com/koliving/api/email/EmailServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void sendEmail_success() throws MailException {
when(messageSource.getMessage(anyString(), any(), any())).thenReturn("test");
when(emailTemplateUtil.generateEmail(any(), any())).thenReturn("test");

emailService.send(MailType.AUTH, "[email protected]", "testLink");
emailService.sendMailAuth(MailType.AUTH, "[email protected]", "testLink");

verify(mailSender, times(1)).send(any(MimeMessage.class));
}
Expand Down
Loading

0 comments on commit 1b12ef6

Please sign in to comment.