-
Notifications
You must be signed in to change notification settings - Fork 1
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
[ADD] 소원 생성 시 슬랙 연동 #158
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sopterm/makeawish/common/message/slack/SlackErrorMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sopterm/makeawish/common/message/slack/SlackField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "*생일 주간:*"; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/sopterm/makeawish/common/message/slack/SlackSuccessMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/main/java/com/sopterm/makeawish/external/SlackWishClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
@@ -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())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드가 훨씬 깔끔해졌네요 ,,,, 정말 짱입니다 |
||
|
||
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
진짜 넘넘 깔꼼해요~!~!