Skip to content

Commit

Permalink
feat: 디스코드 알림 훅 구현 (#259)
Browse files Browse the repository at this point in the history
* remove: 파일 경로 이동

* remove: 구글 시트 기능 및 피드백 기능 삭제

* add: 디스코드 관련 설정 추가

* feat: 회고 생성 시 디스코드 알림 기능 구현

* feat: 회원가입 시 디스코드 알림 기능 구현

* feat: 스페이스 생성 시 디스코드 알림 기능 구현

* feat(DiscordAppender): 디스코드 메세지 만드는 클래스 구현

* fix: 회원가입 시 알맞은 디스코드 메서드 호출하도록 수정
  • Loading branch information
mikekks authored Nov 17, 2024
1 parent 21a9487 commit 332ebe0
Show file tree
Hide file tree
Showing 34 changed files with 323 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.layer.common.annotation.MemberId;
import org.layer.domain.auth.controller.dto.*;
import org.layer.domain.auth.service.AuthService;
import org.layer.domain.jwt.service.JwtService;
import org.layer.domain.member.entity.SocialType;
import org.layer.oauth.service.GoogleService;
import org.layer.oauth.service.KakaoService;
Expand All @@ -24,7 +23,6 @@ public class AuthController implements AuthApi {
private final AuthService authService;
private final GoogleService googleService;
private final KakaoService kakaoService;
private final JwtService jwtService;

private static final String SOCIAL_TOKEN_NAME = "X-AUTH-TOKEN";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
import org.layer.common.exception.BaseCustomException;
import org.layer.domain.auth.controller.dto.*;
import org.layer.domain.auth.service.dto.ReissueTokenServiceResponse;
import org.layer.domain.external.google.enums.SheetType;
import org.layer.domain.external.google.service.GoogleApiService;
import org.layer.domain.common.time.Time;
import org.layer.domain.jwt.JwtToken;
import org.layer.domain.jwt.exception.AuthExceptionType;
import org.layer.domain.jwt.service.JwtService;
import org.layer.domain.member.entity.Member;
import org.layer.domain.member.entity.SocialType;
import org.layer.domain.member.service.MemberService;
import org.layer.external.discord.event.SignUpEvent;
import org.layer.oauth.dto.service.MemberInfoServiceResponse;
import org.layer.oauth.service.GoogleService;
import org.layer.oauth.service.KakaoService;
import org.layer.oauth.service.apple.AppleService;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.IntStream;


@Slf4j
@RequiredArgsConstructor
Expand All @@ -33,8 +32,8 @@ public class AuthService {
private final JwtService jwtService;
private final MemberService memberService;


private final GoogleApiService googleApiService;
private final ApplicationEventPublisher eventPublisher;
private final Time time;

//== 로그인 ==//
@Transactional
Expand All @@ -57,12 +56,21 @@ public SignUpResponse signUp(final String socialAccessToken, final SignUpRequest

// DB에 회원 저장
Member member = memberService.saveMember(signUpRequest, memberInfo);
publishCreateRetrospectEvent(member);

// 토큰 발급
JwtToken jwtToken = jwtService.issueToken(member.getId(), member.getMemberRole());
return SignUpResponse.of(member, jwtToken);
}

public void publishCreateRetrospectEvent(final Member member) {
eventPublisher.publishEvent(SignUpEvent.of(
member.getName(),
member.getId(),
time.now()
));
}

//== 로그아웃 ==//
@Transactional
public void signOut(final Long memberId) {
Expand All @@ -76,23 +84,6 @@ public void signOut(final Long memberId) {
@Transactional
public void withdraw(final Long memberId, WithdrawMemberRequest withdrawMemberRequest) {

// 구글시트 적재
var foundMemberFeedback = memberService.findFeedback(memberId);
if (foundMemberFeedback.isPresent()) {
/*
* 체크박스 배열을 boolean[]로써 길이 3을 고정으로 한다.
* 0번 인덱스가 true -> +1
* 1번 인덱스가 true -> +2
* 3번 인덱스가 true -> +4
*/
var score = IntStream.range(0, 3)
.filter(i -> Boolean.TRUE.equals(withdrawMemberRequest.booleans()[i]))
.map(i -> (int) Math.pow(2, i))
.sum();
googleApiService.writeFeedback(SheetType.WITHDRAW, foundMemberFeedback.get(), score, withdrawMemberRequest.description());
}


// soft delete
memberService.withdrawMember(memberId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.layer.common.annotation.MemberId;
import org.layer.domain.external.controller.dto.ExternalRequest;
import org.layer.domain.external.controller.dto.ExternalResponse;
import org.layer.domain.external.ncp.dto.NcpResponse;
import org.layer.domain.external.ncp.service.NcpService;
import org.layer.external.ncp.dto.NcpResponse;
import org.layer.external.ncp.service.NcpService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.layer.domain.external.controller.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import org.layer.domain.external.ncp.enums.ImageDomain;
import org.layer.external.ncp.enums.ImageDomain;

public class ExternalRequest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import org.layer.domain.external.ncp.exception.ExternalException;
import org.layer.external.ncp.exception.ExternalException;

import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.layer.common.annotation.MemberId;
import org.layer.domain.member.controller.dto.CreateFeedbackRequest;
import org.layer.domain.member.controller.dto.GetMemberAnalyzesResponse;

import org.layer.domain.member.controller.dto.UpdateMemberInfoRequest;
Expand All @@ -16,10 +15,6 @@ public interface MemberApi {
@Operation(summary = "회원 정보(이름, 프로필 사진) 수정", method = "POST", description = "회원의 이름과 프로필 사진(url)을 수정합니다.")
ResponseEntity<UpdateMemberInfoResponse> updateMemberInfo(@MemberId Long memberId, @Valid UpdateMemberInfoRequest updateMemberInfoRequest);

@Operation(summary = "서비스 사용에 대한 피드백 남기기", method = "POST")
ResponseEntity<Void> createFeedback(@MemberId Long memberId, @Valid CreateFeedbackRequest createFeedbackRequest);


@Operation(summary = "내 회고 분석 조회", method = "GET")
ResponseEntity<GetMemberAnalyzesResponse> getMyAnalyzes(@MemberId Long memberId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ public ResponseEntity<UpdateMemberInfoResponse> updateMemberInfo(@MemberId Long

return new ResponseEntity<>(response, HttpStatus.OK);
}

@Override
@PostMapping("/feedback")
public ResponseEntity<Void> createFeedback(@MemberId Long memberId, @Valid @RequestBody CreateFeedbackRequest createFeedbackRequest) {
memberService.createFeedback(memberId, createFeedbackRequest);
return null;
}
@Override
@GetMapping("/analyze")
public ResponseEntity<GetMemberAnalyzesResponse> getMyAnalyzes(@MemberId Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.layer.domain.analyze.repository.AnalyzeRepository;
import org.layer.domain.auth.controller.dto.SignUpRequest;
import org.layer.domain.common.time.Time;
import org.layer.domain.external.google.enums.SheetType;
import org.layer.domain.external.google.service.GoogleApiService;
import org.layer.domain.jwt.SecurityUtil;
import org.layer.domain.member.controller.dto.*;
import org.layer.domain.member.entity.Member;
Expand Down Expand Up @@ -46,8 +44,6 @@ public class MemberService {
private final RetrospectRepository retrospectRepository;
private final AnalyzeRepository analyzeRepository;

private final GoogleApiService googleApiService;

private final SecurityUtil securityUtil;

private final Time time;
Expand All @@ -74,15 +70,6 @@ public void checkIsNewMember(String socialId, SocialType socialType) {
}
}

public void createFeedback(Long memberId, CreateFeedbackRequest createFeedbackRequest) {
var foundMemberFeedback = findFeedback(memberId);
if (foundMemberFeedback.isEmpty()) {
return;
}
googleApiService.writeFeedback(SheetType.FEEDBACK, foundMemberFeedback.get(),
createFeedbackRequest.satisfaction(), createFeedbackRequest.description());
}

@Transactional
public Member saveMember(SignUpRequest signUpRequest, MemberInfoServiceResponse memberInfo) {
Member member = Member.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.layer.domain.retrospect.service;

import static org.layer.common.exception.RetrospectExceptionType.*;

import lombok.RequiredArgsConstructor;

import org.layer.domain.analyze.service.AnalyzeService;
import org.layer.domain.answer.entity.Answers;
import org.layer.domain.answer.repository.AnswerRepository;
import org.layer.domain.common.time.Time;
Expand All @@ -30,6 +27,8 @@
import org.layer.domain.space.repository.MemberSpaceRelationRepository;
import org.layer.domain.space.repository.SpaceRepository;
import org.layer.external.ai.service.AIAnalyzeService;
import org.layer.external.discord.event.CreateRetrospectEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -48,6 +47,8 @@ public class RetrospectService {
private final FormRepository formRepository;
private final SpaceRepository spaceRepository;

private final ApplicationEventPublisher eventPublisher;

private final AIAnalyzeService aiAnalyzeService;

private final Time time;
Expand All @@ -64,6 +65,8 @@ public Long createRetrospect(RetrospectCreateRequest request, Long spaceId, Long
List<Question> questions = getQuestions(request.questions(), savedRetrospect.getId(), null);
questionRepository.saveAll(questions);

publishCreateRetrospectEvent(retrospect, memberId);

Space space = spaceRepository.findByIdOrThrow(spaceId);

// 새로운 폼 생성(수정)인지 확인
Expand All @@ -87,7 +90,6 @@ public Long createRetrospect(RetrospectCreateRequest request, Long spaceId, Long

// 스페이스 최근 폼 수정
space.updateRecentFormId(savedForm.getId(), memberId);

return savedRetrospect.getId();
}

Expand All @@ -102,6 +104,14 @@ private Retrospect getRetrospect(RetrospectCreateRequest request, Long spaceId)
.build();
}

public void publishCreateRetrospectEvent(final Retrospect retrospect, final Long memberId) {
eventPublisher.publishEvent(CreateRetrospectEvent.of(
retrospect.getTitle(),
memberId,
time.now()
));
}

public RetrospectListGetResponse getRetrospects(Long spaceId, Long memberId) {
// 해당 스페이스 팀원인지 검증
Team team = new Team(memberSpaceRelationRepository.findAllBySpaceId(spaceId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public record CreateSpaceRequest(
@NotNull
SpaceCategory category,
@Schema(description = "진행중인 프로젝트 유형")

@NotNull
List<SpaceField> fieldList,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import lombok.extern.slf4j.Slf4j;
import org.layer.common.dto.Meta;
import org.layer.domain.actionItem.repository.ActionItemRepository;
import org.layer.domain.external.ncp.service.NcpService;
import org.layer.domain.common.time.Time;
import org.layer.external.discord.event.CreateSpaceEvent;
import org.layer.external.ncp.service.NcpService;
import org.layer.domain.retrospect.repository.RetrospectRepository;
import org.layer.domain.space.controller.dto.SpaceRequest;
import org.layer.domain.space.controller.dto.SpaceResponse;
Expand All @@ -16,6 +18,7 @@
import org.layer.domain.space.exception.SpaceException;
import org.layer.domain.space.repository.MemberSpaceRelationRepository;
import org.layer.domain.space.repository.SpaceRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -38,6 +41,10 @@ public class SpaceService {
private final ActionItemRepository actionItemRepository;
private final RetrospectRepository retrospectRepository;

private final ApplicationEventPublisher eventPublisher;

private final Time time;

public SpaceResponse.SpacePage getSpaceListFromMemberId(Long memberId, SpaceRequest.GetSpaceRequest getSpaceRequest) {

var spacePages = spaceRepository.findAllSpacesByMemberIdAndCategoryAndCursor(memberId, getSpaceRequest.cursorId(), getSpaceRequest.category(), getSpaceRequest.pageSize());
Expand Down Expand Up @@ -65,9 +72,18 @@ public Long createSpace(Long memberId, SpaceRequest.CreateSpaceRequest createSpa

memberSpaceRelationRepository.save(memberSpaceRelation);

publishCreateSpaceEvent(newSpace, memberId);
return newSpace.getId();
}

public void publishCreateSpaceEvent(final Space space, final Long memberId) {
eventPublisher.publishEvent(CreateSpaceEvent.of(
space.getName(),
memberId,
time.now()
));
}

@Transactional
public void updateSpace(Long memberId, SpaceRequest.UpdateSpaceRequest updateSpaceRequest) {
spaceRepository.findByIdAndJoinedMemberId(updateSpaceRequest.id(), memberId).orElseThrow(() -> new SpaceException(NOT_FOUND_SPACE));
Expand Down
8 changes: 7 additions & 1 deletion layer-api/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ admin:
password: ${ADMIN_PASSWORD}

greeting:
message: this is dev
message: this is dev

discord:
webhook:
retrospect-url: ${DISCORD_RETROSPECT_URL}
space-url: ${DISCORD_SPACE_URL}
member-url: ${DISCORD_MEMBER_URL}
8 changes: 7 additions & 1 deletion layer-api/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ admin:
password: ${ADMIN_PASSWORD}

greeting:
message: this is local
message: this is local

discord:
webhook:
retrospect-url: ${DISCORD_RETROSPECT_URL}
space-url: ${DISCORD_SPACE_URL}
member-url: ${DISCORD_MEMBER_URL}
8 changes: 7 additions & 1 deletion layer-api/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ admin:
password: ${ADMIN_PASSWORD}

greeting:
message: this is prod
message: this is prod

discord:
webhook:
retrospect-url: ${DISCORD_RETROSPECT_URL}
space-url: ${DISCORD_SPACE_URL}
member-url: ${DISCORD_MEMBER_URL}
Loading

0 comments on commit 332ebe0

Please sign in to comment.