Skip to content

Commit

Permalink
Merge pull request #91 from Team-Growthook/feat/#90-sign-up-slack
Browse files Browse the repository at this point in the history
[FEAT]회원 가입 시에 슬랙에 알림 발생 기능 개발
  • Loading branch information
Hong0329 authored Mar 18, 2024
2 parents 8b07ab7 + 4802d07 commit 6965906
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions growthookServer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'

//* slack bot
implementation("com.slack.api:bolt:1.18.0")
implementation("com.slack.api:bolt-servlet:1.18.0")
implementation("com.slack.api:bolt-jetty:1.18.0")

implementation 'com.google.code.gson:gson:2.10.1'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

public interface ActionPlanRepository extends JpaRepository<ActionPlan,Long> {

List<ActionPlan> findAllBySeedCaveMemberIdAndIsFinished(Long memberId, Boolean isFinished);
List<ActionPlan> findAllBySeedCaveMemberIdAndIsFinishedOrderByCreatedAtDesc(Long memberId, Boolean isFinished);

@Query("SELECT ap FROM ActionPlan ap JOIN ap.seed s JOIN s.cave c JOIN c.member m WHERE m.id = :memberId")
List<ActionPlan> findAllByMemberId(Long memberId);
List<ActionPlan> findAllByMemberIdOrderByCreatedAtDesc(Long memberId);
List<ActionPlan> findAllBySeedIdOrderByCreatedAtDesc(Long seedId);

List<ActionPlan> findAllBySeedId(Long seedId);

List<ActionPlan> findAllByMemberId(Long memberId);

Optional<ActionPlan> findActionPlanById(Long actionPlanId);

default ActionPlan findActionPlanByIdOrThrow(Long actionPlanId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void createActionPlan(Long seedId, ActionPlanCreateRequestDto actionPlanC

@Override
public List<ActionPlanGetResponseDto> getActionPlan(Long seedId) {
List<ActionPlan> actionPlans = actionPlanRepository.findAllBySeedId(seedId);
List<ActionPlan> actionPlans = actionPlanRepository.findAllBySeedIdOrderByCreatedAtDesc(seedId);

return actionPlans.stream()
.map(actionPlan -> ActionPlanGetResponseDto.of(actionPlan.getId(), actionPlan.getContent(), actionPlan.getIsScraped(), actionPlan.getIsFinished(),reviewRepository.existsByActionPlan(actionPlan)))
Expand Down Expand Up @@ -97,7 +97,7 @@ public int getActionPlanPercent(Long memberId) {

@Override
public List<DoingActionPlanGetResponseDto> getDoingActionPlan(Long memberId) {
List<ActionPlan> doingActionPlans = actionPlanRepository.findAllBySeedCaveMemberIdAndIsFinished(memberId,false);
List<ActionPlan> doingActionPlans = actionPlanRepository.findAllBySeedCaveMemberIdAndIsFinishedOrderByCreatedAtDesc(memberId,false);

return doingActionPlans.stream()
.map(actionPlan -> DoingActionPlanGetResponseDto.of(actionPlan.getId(), actionPlan.getContent(), actionPlan.getIsScraped(),actionPlan.getSeed().getId(),reviewRepository.existsByActionPlan(actionPlan)))
Expand All @@ -106,7 +106,7 @@ public List<DoingActionPlanGetResponseDto> getDoingActionPlan(Long memberId) {

@Override
public List<FinishedActionPlanGetResponseDto> getFinishedActionPlan(Long memberId) {
List<ActionPlan> finishedActionPlans = actionPlanRepository.findAllBySeedCaveMemberIdAndIsFinished(memberId,true);
List<ActionPlan> finishedActionPlans = actionPlanRepository.findAllBySeedCaveMemberIdAndIsFinishedOrderByCreatedAtDesc(memberId,true);

return finishedActionPlans.stream()
.map(actionPlan -> FinishedActionPlanGetResponseDto.of(actionPlan.getId(), actionPlan.getContent(), actionPlan.getIsScraped(),actionPlan.getSeed().getId(),reviewRepository.existsByActionPlan(actionPlan)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import com.example.growthookserver.external.slack.service.SlackService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
Expand All @@ -31,6 +32,7 @@ public class AuthServiceImpl implements AuthService {
private final KakaoAuthService kakaoAuthService;
private final AppleAuthService appleAuthService;
private final MemberRepository memberRepository;
private final SlackService slackService;

@Override
@Transactional
Expand Down Expand Up @@ -62,6 +64,9 @@ public AuthResponseDto socialLogin(AuthRequestDto authRequestDto) throws NoSuchA

memberRepository.save(member);

Long memberCount = memberRepository.count();
slackService.sendSlackMessage(socialData.getNickname(), memberCount, "#gth_signup");

member.updateRefreshToken(refreshToken);
}
else memberRepository.findMemberBySocialIdOrThrow(socialData.getId()).updateRefreshToken(refreshToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.growthookserver.external.slack.service;

import com.slack.api.Slack;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class SlackService {
@Value(value = "${slack.token}")
String slackToken;

public void sendSlackMessage(String userName, Long totalMember, String channel) {
String message = userName +"님은 " + totalMember.toString() + "번째 쑥쑥이에어 곰이 됐어요.";
try {
MethodsClient methods = Slack.getInstance().methods(slackToken);

ChatPostMessageRequest request = ChatPostMessageRequest.builder()
.channel(channel)
.text(message)
.build();
methods.chatPostMessage(request);
} catch (SlackApiException | IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}

0 comments on commit 6965906

Please sign in to comment.