diff --git a/build.gradle b/build.gradle index 5c636f85..8cbcff2b 100644 --- a/build.gradle +++ b/build.gradle @@ -27,12 +27,14 @@ repositories { ext { set('snippetsDir', file("build/generated-snippets")) + set('springCloudVersion', "2022.0.3") } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-validation' + implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.0.3' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' diff --git a/src/main/java/com/sopterm/makeawish/config/FeignConfig.java b/src/main/java/com/sopterm/makeawish/config/FeignConfig.java new file mode 100644 index 00000000..50ee0e17 --- /dev/null +++ b/src/main/java/com/sopterm/makeawish/config/FeignConfig.java @@ -0,0 +1,9 @@ +package com.sopterm.makeawish.config; + +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Configuration; + +@EnableFeignClients(basePackages = "com.sopterm.makeawish.external") +@Configuration +public class FeignConfig { +} diff --git a/src/main/java/com/sopterm/makeawish/external/SlackClient.java b/src/main/java/com/sopterm/makeawish/external/SlackClient.java new file mode 100644 index 00000000..b67d27bd --- /dev/null +++ b/src/main/java/com/sopterm/makeawish/external/SlackClient.java @@ -0,0 +1,12 @@ +package com.sopterm.makeawish.external; + +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(value = "slackClient", url = "${slack.url}") +public interface SlackClient { + @PostMapping(value = "", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + void postMessage(@RequestBody String request); +} diff --git a/src/main/java/com/sopterm/makeawish/service/social/KakaoLoginService.java b/src/main/java/com/sopterm/makeawish/service/social/KakaoLoginService.java index b209372e..5c6e9586 100644 --- a/src/main/java/com/sopterm/makeawish/service/social/KakaoLoginService.java +++ b/src/main/java/com/sopterm/makeawish/service/social/KakaoLoginService.java @@ -1,27 +1,39 @@ package com.sopterm.makeawish.service.social; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.JsonParser; import com.sopterm.makeawish.domain.user.InternalTokenManager; import com.sopterm.makeawish.domain.user.KakaoTokenManager; import com.sopterm.makeawish.domain.user.User; import com.sopterm.makeawish.dto.auth.AuthSignInRequestDTO; import com.sopterm.makeawish.dto.auth.AuthSignInResponseDTO; +import com.sopterm.makeawish.external.SlackClient; import com.sopterm.makeawish.repository.UserRepository; import com.sopterm.makeawish.service.SocialLoginService; import lombok.*; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; +import java.util.Objects; + import static com.sopterm.makeawish.common.message.ErrorMessage.*; +@Slf4j @Service @RequiredArgsConstructor public class KakaoLoginService implements SocialLoginService { + @Value("${spring.profiles.active}") + private String activeProfile; private final UserRepository userRepository; private final InternalTokenManager tokenManager; private final KakaoTokenManager kakaoTokenManager; - + private final SlackClient slackClient; + private final ObjectMapper jsonMapper = new ObjectMapper(); @Override public AuthSignInResponseDTO socialLogin(String code, String redirectUri) { String kakaoAccessToken = null; @@ -44,6 +56,47 @@ private User issueAccessToken(AuthSignInRequestDTO request) { .orElseGet(() -> signup(request)); } private User signup(AuthSignInRequestDTO request) { + + try { + if (Objects.equals(activeProfile, "dev")) { + val slackRequest = createSlackRequest(request.nickname(), request.email(), request.createdAt(), String.valueOf(request.socialType())); + slackClient.postMessage(slackRequest.toString()); + } + } catch (RuntimeException ex) { + log.error("슬랙 요청이 실패했습니다 : " + ex.getMessage()); + } return userRepository.save(new User(request)); } + + private JsonNode createSlackRequest(String nickname, String email, LocalDateTime createdAt, String socialType) { + val rootNode = jsonMapper.createObjectNode(); + rootNode.put("text", "새로운 유저가 가입했어요!"); + val blocks = jsonMapper.createArrayNode(); + + val textField = jsonMapper.createObjectNode(); + textField.put("type", "section"); + textField.set("text", createTextFieldNode("새로운 유저가 가입했어요!")); + + val contentNode = jsonMapper.createObjectNode(); + contentNode.put("type", "section"); + val fields = jsonMapper.createArrayNode(); + fields.add(createTextFieldNode("*이름:*\n" + nickname)); + fields.add(createTextFieldNode("*이메알:*\n"+ email)); + fields.add(createTextFieldNode("*가입 시간:*\n" + createdAt)); + fields.add(createTextFieldNode("*소셜:*\n" + socialType)); + contentNode.set("fields", fields); + + blocks.add(textField); + blocks.add(contentNode); + rootNode.set("blocks", blocks); + return rootNode; + } + + private JsonNode createTextFieldNode (String text) { + val textField = jsonMapper.createObjectNode(); + textField.put("type", "mrkdwn"); + textField.put("text", text); + return textField; + } + }