Skip to content

Commit

Permalink
[feat] 새로운 회원가입 시 discord 알림 전송 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JeonHaeseung committed Jul 18, 2024
1 parent 6aca646 commit ba7ba8c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/main/java/Ness/Backend/domain/member/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import Ness.Backend.domain.schedule.ScheduleService;
import Ness.Backend.domain.schedule.dto.request.PostScheduleDto;
import Ness.Backend.global.time.GlobalTime;
import Ness.Backend.infra.discord.DiscordAlertSender;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
Expand All @@ -27,6 +28,7 @@ public class MemberService {
private final ScheduleService scheduleService;
private final ChatService chatService;
private final GlobalTime globalTime;
private final DiscordAlertSender discordAlertSender;

public void deleteMember(Member member) {
profileRepository.delete(member.getProfile());
Expand Down Expand Up @@ -127,6 +129,9 @@ public void createMember(String email, String password, String picture, String n
chatService.createNewChat("NESS에 오신 것을 환영해요! 간단하게 채팅을 테스트해볼까요?\n" +
"\"내일 5시 디지털 경진대회 미팅 추가\"라고 채팅을 보내보세요.", ChatType.AI, 1, member);

// 알림 보내기
discordAlertSender.sendDiscordInfoAlarm(member.getId(), profile.getName());

return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected ResponseEntity<ErrorResponse> handleMethodNotSupportedException(HttpRe
@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorResponse> handleException(Exception exception, HttpServletRequest httpServletRequest) {
log.error("handleException", exception);
discordAlertSender.sendDiscordAlarm(exception, httpServletRequest);
discordAlertSender.sendDiscordErrorAlarm(exception, httpServletRequest);
final ContentCachingRequestWrapper contentCachingRequestWrapper = new ContentCachingRequestWrapper(httpServletRequest);
return new ResponseEntity<>(ErrorResponse.onFailure(ErrorCode._INTERNAL_SERVER_ERROR), null, INTERNAL_SERVER_ERROR);
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/Ness/Backend/infra/discord/DiscordAlertSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.hibernate.boot.registry.selector.spi.DialectSelector;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DiscordAlertSender {
private final DiscordClientApi discordClientApi;
private final DiscordErrorClientApi discordErrorClientApi;
private final DiscordInfoClientApi discordInfoClientApi;
private final DiscordMessageGenerator discordMessageGenerator;

public void sendDiscordAlarm(Exception exception, HttpServletRequest httpServletRequest) {
discordClientApi.sendAlarm(discordMessageGenerator.createMessage(exception, httpServletRequest));
public void sendDiscordErrorAlarm(Exception exception, HttpServletRequest httpServletRequest) {
discordErrorClientApi.sendAlarm(discordMessageGenerator.createErrorMessage(exception, httpServletRequest));
}

public void sendDiscordInfoAlarm(Long memberId, String name) {
discordInfoClientApi.sendAlarm(discordMessageGenerator.createInfoMessage(memberId, name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Ness.Backend.infra.discord;

import Ness.Backend.infra.discord.dto.DiscordMessageDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(
name = "discordErrorClientApi",
url = "${discord.webhook.error}")
public interface DiscordErrorClientApi {
@PostMapping()
void sendAlarm(@RequestBody DiscordMessageDto discordMessageDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(
name = "discordClientApi",
url = "${discord.webhook.url}")
public interface DiscordClientApi {
name = "discordInfoClientApi",
url = "${discord.webhook.info}")
public interface DiscordInfoClientApi {
@PostMapping()
void sendAlarm(@RequestBody DiscordMessageDto discordMessageDto);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Ness.Backend.infra.discord;

import Ness.Backend.global.time.GlobalTime;
import Ness.Backend.infra.discord.dto.DiscordEmbedDto;
import Ness.Backend.infra.discord.dto.DiscordMessageDto;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -17,18 +18,19 @@
@Component
@RequiredArgsConstructor
public class DiscordMessageGenerator {
private final GlobalTime globalTime;

@Value("${spring.profiles.active}")
private String activeProfile;

/* 메세지 생성 */
public DiscordMessageDto createMessage(Exception exception, HttpServletRequest httpServletRequest) {
/* 에러 메세지 생성 */
public DiscordMessageDto createErrorMessage(Exception exception, HttpServletRequest httpServletRequest) {
return DiscordMessageDto.builder()
.content("## 🚨 서버 에러 발생 🚨")
.embeds(List.of(DiscordEmbedDto.builder()
.title("ℹ️ 에러 정보")
.description("### 🕖 에러 발생 시간\n"
+ ZonedDateTime.now(ZoneId.of("Asia/Seoul")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH시 mm분 ss초(서울 시간)"))
+ globalTime.getToday().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH시 mm분 ss초(서울 시간)"))
+ "\n"
+ "### 🔗 요청 엔드포인트\n"
+ httpServletRequest.getRequestURI()
Expand All @@ -42,9 +44,9 @@ public DiscordMessageDto createMessage(Exception exception, HttpServletRequest h
+ "### 📜 에러 로그\n"
+ "```\n"
+ getStackTrace(exception).substring(0, 1000)
+ "\n```")
.build()
)
+ "\n```"
).build()
)
).build();
}

Expand All @@ -59,4 +61,21 @@ private String getStackTrace(Exception exception) {
private String getRemoteIp(HttpServletRequest httpServletRequest){
return httpServletRequest.getRemoteAddr();
}

/* 인포 메세지 생성 */
public DiscordMessageDto createInfoMessage(Long memberId, String name) {
return DiscordMessageDto.builder()
.content("##🚀 새로운 유저 가입 🚀")
.embeds(List.of(DiscordEmbedDto.builder()
.title("ℹ️ 유저 정보")
.description("### 🕖 유저 가입 시간\n"
+ globalTime.getToday().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH시 mm분 ss초(서울 시간)"))
+ "\n"
+ "### 🖥️ 유저 정보\n"
+ memberId + "번 DB 아이디를 가지신 " + name + "님이 방금 회원가입하셨어요!"
+ "\n"
).build()
)
).build();
}
}

0 comments on commit ba7ba8c

Please sign in to comment.