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

[ADD] 소원 생성 시 슬랙 연동 #158

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Changes from 1 commit
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
27 changes: 14 additions & 13 deletions src/main/java/com/sopterm/makeawish/service/WishService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

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.SlackClient;
import com.sopterm.makeawish.external.SlackWishClient;
import com.sopterm.makeawish.repository.PresentRepository;
import com.sopterm.makeawish.repository.UserRepository;
Expand All @@ -23,10 +24,10 @@
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.time.LocalDateTime;
import java.util.Objects;

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 Down Expand Up @@ -60,7 +61,7 @@ public Long createWish(Long userId, WishRequestDTO requestDTO) {
val wishSlackRequest = createSlackWishRequest(wish);
slackWishClient.postWishMessage(wishSlackRequest.toString());
} catch (RuntimeException e) {
log.error("슬랙 요청이 실패했습니다. : " + e.getMessage());
log.error(SlackErrorMessage.POST_REQUEST_ERROR.getMessage() + e.getMessage());
}
return wishRepository.save(wish).getId();
}
Expand Down Expand Up @@ -185,30 +186,30 @@ private Wish getUserMainWish(User user) {

private JsonNode createSlackWishRequest(Wish wish) {
val rootNode = jsonMapper.createObjectNode();
rootNode.put("text", "새로운 소원이 생성되었어요!");
rootNode.put(TEXT, SlackSuccessMessage.SUCCESS_CREATE_WISH.getMessage());
val blocks = jsonMapper.createArrayNode();

val textField = jsonMapper.createObjectNode();
textField.put("type", "section");
textField.set("text", createTextFieldNode("새로운 소원이 생성되었어요!"));
textField.put(TYPE, SECTION);
textField.set(TEXT, createTextFieldNode(SlackSuccessMessage.SUCCESS_CREATE_WISH.getMessage()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드가 훨씬 깔끔해졌네요 ,,,, 정말 짱입니다 !!


val contentNode = jsonMapper.createObjectNode();
contentNode.put("type", "section");
contentNode.put(TYPE, SECTION);
val fields = jsonMapper.createArrayNode();
fields.add(createTextFieldNode("*이름:*"+ StringUtils.LF + wish.getWisher().getNickname()));
fields.add(createTextFieldNode("*생일 주간 기간:*"+ StringUtils.LF + wish.getStartAt() + " ~ " + wish.getEndAt()));
contentNode.set("fields", fields);
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);
rootNode.set(BLOCKS, blocks);
return rootNode;
}

private JsonNode createTextFieldNode (String text) {
val textField = jsonMapper.createObjectNode();
textField.put("type", "mrkdwn");
textField.put("text", text);
textField.put(TYPE, MARKDOWN);
textField.put(TEXT, text);
return textField;
}

Expand Down