Skip to content

Commit

Permalink
Merge pull request #158 from Make-A-Wish-Sopt/jiyoung_#157-slack-alarm
Browse files Browse the repository at this point in the history
[ADD] 소원 생성 시 슬랙 연동
  • Loading branch information
wlwpfh authored Dec 20, 2023
2 parents e3f1de3 + 589b866 commit c18ad49
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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 = "*생일 주간:*";
}
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions src/main/java/com/sopterm/makeawish/external/SlackWishClient.java
Original file line number Diff line number Diff line change
@@ -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);
}
48 changes: 48 additions & 0 deletions src/main/java/com/sopterm/makeawish/service/WishService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
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;
import jakarta.persistence.EntityNotFoundException;
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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c18ad49

Please sign in to comment.