Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 회원가입 이벤트 디스코드 웹훅 기능 구현 #120

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.doorip.exception.InvalidValueException;
import org.doorip.exception.UnauthorizedException;
import org.doorip.message.ErrorMessage;
import org.doorip.message.EventMessage;
import org.doorip.openfeign.apple.AppleOAuthProvider;
import org.doorip.openfeign.discord.DiscordMessageProvider;
import org.doorip.openfeign.kakao.KakaoOAuthProvider;
import org.doorip.user.domain.*;
import org.doorip.user.dto.request.ResultUpdateRequest;
Expand Down Expand Up @@ -43,6 +45,7 @@ public class UserService {
private final JwtValidator jwtValidator;
private final AppleOAuthProvider appleOAuthProvider;
private final KakaoOAuthProvider kakaoOAuthProvider;
private final DiscordMessageProvider discordMessageProvider;

@Transactional(readOnly = true)
public void splash(Long userId) {
Expand All @@ -67,6 +70,7 @@ public UserSignUpResponse signUp(String token, UserSignUpRequest request) {
User savedUser = saveUser(request, platformId, enumPlatform);
Token issueToken = jwtProvider.issueToken(savedUser.getId());
updateRefreshToken(issueToken.refreshToken(), savedUser);
discordMessageProvider.sendMessage(EventMessage.SIGN_UP_EVENT);
return UserSignUpResponse.of(issueToken, savedUser.getId());
}

Expand Down Expand Up @@ -102,7 +106,6 @@ public void updateResult(Long userId, ResultUpdateRequest request) {
String stringResult = oneTypeResult(mappedIndex.subList(0, 3), "S", "A")
+ oneTypeResult(mappedIndex.subList(3, 6), "R", "E")
+ oneTypeResult(mappedIndex.subList(6, 9), "P", "I");

Result result = getEnumResultFromStringResult(stringResult);
findUser.updateResult(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum ErrorMessage {
INVALID_DATE_TYPE(HttpStatus.BAD_REQUEST, "e4004", "유효하지 않은 날짜 타입입니다."),
INVALID_RESULT_TYPE(HttpStatus.BAD_REQUEST, "e4005", "유효하지 않은 성향 입력 값입니다."),
INVALID_PARTICIPANT_COUNT(HttpStatus.BAD_REQUEST, "e4006", "여행에 입장할 수 있는 최대 인원은 6명입니다."),
INVALID_DISCORD_MESSAGE(HttpStatus.BAD_REQUEST, "e4007", "디스코드 메시지 전달에 실패했습니다."),

/**
* 401 Unauthorized
Expand Down
13 changes: 13 additions & 0 deletions doorip-common/src/main/java/org/doorip/message/EventMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.doorip.message;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum EventMessage {
SIGN_UP_EVENT("doorip 서비스에 회원가입 이벤트가 발생했습니다. 🎉");

private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.doorip.openfeign.discord;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = "${discord.name}", url = "${discord.webhook-url}")
public interface DiscordFeignClient {
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
void sendMessage(@RequestBody DiscordMessage discordMessage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.doorip.openfeign.discord;

public record DiscordMessage(
String content
) {
public static DiscordMessage createDiscordMessage(String message) {
return new DiscordMessage(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.doorip.openfeign.discord;

import feign.FeignException;
import lombok.RequiredArgsConstructor;
import org.doorip.exception.InvalidValueException;
import org.doorip.message.ErrorMessage;
import org.doorip.message.EventMessage;
import org.springframework.stereotype.Component;

import static org.doorip.openfeign.discord.DiscordMessage.createDiscordMessage;

@RequiredArgsConstructor
@Component
public class DiscordMessageProvider {
private final DiscordFeignClient discordFeignClient;

public void sendMessage(EventMessage eventMessage) {
DiscordMessage discordMessage = createDiscordMessage(eventMessage.getMessage());
sendMessageToDiscord(discordMessage);
}

private void sendMessageToDiscord(DiscordMessage discordMessage) {
try {
discordFeignClient.sendMessage(discordMessage);
} catch (FeignException e) {
throw new InvalidValueException(ErrorMessage.INVALID_DISCORD_MESSAGE);
}
}
}
Loading