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

[Refactor] #238 - Discord 회원가입 로직 이벤트 Pub-Sub 구조로 리팩토링 #239

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package org.moonshot.keyresult.service;

import static org.moonshot.keyresult.service.validator.KeyResultValidator.*;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.hasChange;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.hasDateChange;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.hasKeyResultTask;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.isKeyResultAchieved;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.validateActiveKRSizeExceeded;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.validateIndex;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.validateIndexUnderMaximum;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.validateKRPeriodWithInObjPeriod;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.validateKeyResultIndex;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.validateKeyResultPeriod;
import static org.moonshot.log.service.validator.LogValidator.validateLogNum;
import static org.moonshot.task.service.validator.TaskValidator.validateTaskIndex;
import static org.moonshot.user.service.validator.UserValidator.validateUserAuthorization;
Expand All @@ -15,7 +24,6 @@
import org.moonshot.common.model.Period;
import org.moonshot.exception.keyresult.KeyResultNotFoundException;
import org.moonshot.exception.keyresult.KeyResultRequiredException;
import org.moonshot.exception.log.LogNotFoundException;
import org.moonshot.exception.objective.ObjectiveNotFoundException;
import org.moonshot.keyresult.dto.request.KeyResultCreateRequestDto;
import org.moonshot.keyresult.dto.request.KeyResultCreateRequestInfoDto;
Expand Down Expand Up @@ -92,7 +100,7 @@ public void createKeyResult(final KeyResultCreateRequestDto request, final Long
.idx(request.idx())
.target(request.target())
.metric(request.metric()).build());
logService.createKRLog(request, keyResult.getId());
logService.createKRLog(request, keyResult.getId());
}

public void deleteKeyResult(final Long keyResultId, final Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.moonshot.discord.DiscordAppender;
import org.moonshot.exception.global.external.discord.ErrorLogAppenderException;
import org.moonshot.discord.SignUpEvent;
import org.moonshot.exception.user.UserNotFoundException;
import org.moonshot.jwt.JwtTokenProvider;
import org.moonshot.jwt.TokenResponse;
Expand All @@ -33,6 +32,7 @@
import org.moonshot.user.model.User;
import org.moonshot.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -60,6 +60,7 @@ public class UserService {

private final UserRepository userRepository;
private final ObjectiveService objectiveService;
private final ApplicationEventPublisher eventPublisher;

private final GoogleAuthApiClient googleAuthApiClient;
private final GoogleApiClient googleApiClient;
Expand Down Expand Up @@ -95,7 +96,7 @@ public SocialLoginResponse googleLogin(final SocialLoginRequest request) throws
.email(userResponse.email())
.build());
user = newUser;
sendDiscordAlert(newUser);
publishSignUpEvent(newUser);
} else {
user = findUser.get();
user.resetDeleteAt();
Expand Down Expand Up @@ -124,7 +125,7 @@ public SocialLoginResponse kakaoLogin(final SocialLoginRequest request) {
.email(null)
.build());
user = newUser;
sendDiscordAlert(newUser);
publishSignUpEvent(newUser);
} else {
user = findUser.get();
user.resetDeleteAt();
Expand Down Expand Up @@ -176,13 +177,14 @@ public UserInfoResponse getMyProfile(final Long userId) {
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void sendDiscordAlert(final User user) {
try {
DiscordAppender discordAppender = new DiscordAppender();
discordAppender.signInAppend(user.getName(), user.getEmail() == null ? "" : user.getEmail(), user.getSocialPlatform().getValue(), LocalDateTime.now(), user.getProfileImage());
} catch (ErrorLogAppenderException e) {
log.error("{}", e.getErrorType().getMessage());
}
public void publishSignUpEvent(final User user) {
eventPublisher.publishEvent(SignUpEvent.of(
user.getName(),
user.getEmail() == null ? "" : user.getEmail(),
user.getSocialPlatform().toString(),
LocalDateTime.now(),
user.getProfileImage()
));
}

public void softDeleteUser(LocalDateTime currentDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import org.moonshot.exception.global.external.discord.ErrorLogAppenderException;
import org.moonshot.util.MDCUtil;
import org.moonshot.util.StringUtil;
import org.springframework.stereotype.Component;

@Slf4j
@Setter
@Component
@RequiredArgsConstructor
public class DiscordAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.moonshot.discord;


import java.time.LocalDateTime;

public record SignUpEvent(String name, String email, String socialPlatform, LocalDateTime createdAt, String imageUrl) {

public static SignUpEvent of(String name, String email, String socialPlatform, LocalDateTime createdAt,
String imageUrl) {
return new SignUpEvent(name, email, socialPlatform, createdAt, imageUrl);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.moonshot.discord;

import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class SignUpEventListener {

private final DiscordAppender discordAppender;

@EventListener
public void handleSignUpEvent(SignUpEvent event) {
discordAppender.signInAppend(
event.name(),
event.email(),
event.socialPlatform(),
event.createdAt(),
event.imageUrl());
}

}
Loading