diff --git a/src/main/java/com/sopterm/makeawish/common/message/slack/SlackErrorMessage.java b/src/main/java/com/sopterm/makeawish/common/message/slack/SlackErrorMessage.java new file mode 100644 index 0000000..9136039 --- /dev/null +++ b/src/main/java/com/sopterm/makeawish/common/message/slack/SlackErrorMessage.java @@ -0,0 +1,12 @@ +package com.sopterm.makeawish.common.message.slack; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum SlackErrorMessage { + POST_REQUEST_ERROR("슬랙 요청이 실패했습니다 :"); + + private final String message; +} diff --git a/src/main/java/com/sopterm/makeawish/common/message/slack/SlackField.java b/src/main/java/com/sopterm/makeawish/common/message/slack/SlackField.java new file mode 100644 index 0000000..8883fd6 --- /dev/null +++ b/src/main/java/com/sopterm/makeawish/common/message/slack/SlackField.java @@ -0,0 +1,12 @@ +package com.sopterm.makeawish.common.message.slack; + +public class SlackField { + public static String TYPE = "type"; + public static String TEXT = "text"; + public static String SECTION = "section"; + public static String FIELDS = "fields"; + public static String BLOCKS = "blocks"; + public static String MARKDOWN = "mrkdwn"; + public static String USER_NAME = "*이름:*"; + public static String USER_WISH_PERIOD = "*생일 주간:*"; +} diff --git a/src/main/java/com/sopterm/makeawish/common/message/slack/SlackSuccessMessage.java b/src/main/java/com/sopterm/makeawish/common/message/slack/SlackSuccessMessage.java new file mode 100644 index 0000000..978ddfb --- /dev/null +++ b/src/main/java/com/sopterm/makeawish/common/message/slack/SlackSuccessMessage.java @@ -0,0 +1,13 @@ +package com.sopterm.makeawish.common.message.slack; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum SlackSuccessMessage { + SUCCESS_CREATE_WISH("새로운 소원이 생성되었어요!") + , SUCCESS_CREAT_USER("새로운 유저가 가입했어요!"); + + private final String message; +} diff --git a/src/main/java/com/sopterm/makeawish/external/SlackWishClient.java b/src/main/java/com/sopterm/makeawish/external/SlackWishClient.java new file mode 100644 index 0000000..423136c --- /dev/null +++ b/src/main/java/com/sopterm/makeawish/external/SlackWishClient.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 = "slackWishClient", url = "${slack.wish-url}") +public interface SlackWishClient { + @PostMapping(value = "", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + void postWishMessage(@RequestBody String request); +} diff --git a/src/main/java/com/sopterm/makeawish/service/WishService.java b/src/main/java/com/sopterm/makeawish/service/WishService.java index dbaf8a9..f6d90c8 100644 --- a/src/main/java/com/sopterm/makeawish/service/WishService.java +++ b/src/main/java/com/sopterm/makeawish/service/WishService.java @@ -1,8 +1,13 @@ package com.sopterm.makeawish.service; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sopterm.makeawish.common.message.slack.SlackErrorMessage; +import com.sopterm.makeawish.common.message.slack.SlackSuccessMessage; import com.sopterm.makeawish.domain.user.User; import com.sopterm.makeawish.domain.wish.Wish; import com.sopterm.makeawish.dto.wish.*; +import com.sopterm.makeawish.external.SlackWishClient; import com.sopterm.makeawish.repository.PresentRepository; import com.sopterm.makeawish.repository.UserRepository; import com.sopterm.makeawish.repository.wish.WishRepository; @@ -10,7 +15,9 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.val; +import org.apache.commons.lang3.StringUtils; import org.jsoup.Jsoup; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,6 +27,7 @@ import static com.sopterm.makeawish.common.Util.convertToDate; import static com.sopterm.makeawish.common.message.ErrorMessage.*; +import static com.sopterm.makeawish.common.message.slack.SlackField.*; import static com.sopterm.makeawish.domain.wish.WishStatus.*; import static java.util.Objects.nonNull; @@ -29,9 +37,13 @@ @Transactional(readOnly = true) public class WishService { + @Value("${spring.profiles.active}") + private String activeProfile; private final WishRepository wishRepository; private final UserRepository userRepository; private final PresentRepository presentRepository; + private final SlackWishClient slackWishClient; + private final ObjectMapper jsonMapper = new ObjectMapper(); private final int EXPIRY_DAY = 7; @@ -45,6 +57,12 @@ public Long createWish(Long userId, WishRequestDTO requestDTO) { val to = convertToDate(requestDTO.endDate()); validateWishDate(wisher, from, to); val wish = requestDTO.toEntity(wisher); + try { + val wishSlackRequest = createSlackWishRequest(wish); + slackWishClient.postWishMessage(wishSlackRequest.toString()); + } catch (RuntimeException e) { + log.error(SlackErrorMessage.POST_REQUEST_ERROR.getMessage() + e.getMessage()); + } return wishRepository.save(wish).getId(); } @@ -165,4 +183,34 @@ private Wish getUserMainWish(User user) { .findMainWish(user, 0) .orElseThrow(() -> new EntityNotFoundException(NO_WISH.getMessage())); } + + private JsonNode createSlackWishRequest(Wish wish) { + val rootNode = jsonMapper.createObjectNode(); + rootNode.put(TEXT, SlackSuccessMessage.SUCCESS_CREATE_WISH.getMessage()); + val blocks = jsonMapper.createArrayNode(); + + val textField = jsonMapper.createObjectNode(); + textField.put(TYPE, SECTION); + textField.set(TEXT, createTextFieldNode(SlackSuccessMessage.SUCCESS_CREATE_WISH.getMessage())); + + val contentNode = jsonMapper.createObjectNode(); + contentNode.put(TYPE, SECTION); + val fields = jsonMapper.createArrayNode(); + fields.add(createTextFieldNode(USER_NAME + StringUtils.LF + wish.getWisher().getNickname())); + fields.add(createTextFieldNode(USER_WISH_PERIOD + StringUtils.LF + wish.getStartAt() + " ~ " + wish.getEndAt())); + 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, MARKDOWN); + textField.put(TEXT, text); + return textField; + } + } \ No newline at end of file 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 5c6e958..b4f6c44 100644 --- a/src/main/java/com/sopterm/makeawish/service/social/KakaoLoginService.java +++ b/src/main/java/com/sopterm/makeawish/service/social/KakaoLoginService.java @@ -81,7 +81,7 @@ private JsonNode createSlackRequest(String nickname, String email, LocalDateTime contentNode.put("type", "section"); val fields = jsonMapper.createArrayNode(); fields.add(createTextFieldNode("*이름:*\n" + nickname)); - fields.add(createTextFieldNode("*이메알:*\n"+ email)); + fields.add(createTextFieldNode("*이메일:*\n"+ email)); fields.add(createTextFieldNode("*가입 시간:*\n" + createdAt)); fields.add(createTextFieldNode("*소셜:*\n" + socialType)); contentNode.set("fields", fields);