diff --git a/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioJuniorMessage.java b/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioJuniorMessage.java index 03978daa..1d3b46c1 100644 --- a/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioJuniorMessage.java +++ b/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioJuniorMessage.java @@ -1,74 +1,35 @@ package com.postgraduate.global.bizppurio.usecase; -import com.fasterxml.jackson.databind.ObjectMapper; import com.postgraduate.domain.senior.domain.entity.Senior; import com.postgraduate.domain.user.domain.entity.User; -import com.postgraduate.global.bizppurio.dto.req.CommonRequest; -import com.postgraduate.global.bizppurio.dto.res.MessageResponse; import com.postgraduate.global.bizppurio.mapper.BizppurioMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import org.springframework.web.reactive.function.client.WebClient; - -import static org.springframework.http.MediaType.APPLICATION_JSON; @RequiredArgsConstructor @Component @Slf4j public class BizppurioJuniorMessage { - private final WebClient webClient; - private final BizppurioAuth bizppurioAuth; - private final ObjectMapper objectMapper; private final BizppurioMapper mapper; - - @Value("${bizppurio.message}") - private String messageUrl; + private final BizppurioSend bizppurioSend; public void mentoringApply(User user) { - CommonRequest commonRequest = mapper.mapToJuniorApplyMessage(user); - sendMessage(commonRequest); + bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToJuniorApplyMessage(user)); } public void mentoringAccept(User user, Senior senior, String time) { - String chatLink = senior.getProfile().getChatLink(); - CommonRequest commonRequest = mapper.mapToJuniorAcceptMessage(user, chatLink, time); - sendMessage(commonRequest); + bizppurioSend.sendMessageWithExceptionHandling(() -> { + String chatLink = senior.getProfile().getChatLink(); + return mapper.mapToJuniorAcceptMessage(user, chatLink, time); + }); } public void mentoringRefuse(User user) { - CommonRequest commonRequest = mapper.mapToJuniorRefuseMessage(user); - sendMessage(commonRequest); + bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToJuniorRefuseMessage(user)); } public void mentoringFinish(User user) { - CommonRequest commonRequest = mapper.mapToJuniorFinish(user); - sendMessage(commonRequest); - } - - private void sendMessage(CommonRequest commonRequest) { - try { - String accessToken = bizppurioAuth.getAuth(); - String request = objectMapper.writeValueAsString(commonRequest); - webClient.post() - .uri(messageUrl) - .headers(h -> h.setContentType(APPLICATION_JSON)) - .headers(h -> h.setBearerAuth(accessToken)) - .bodyValue(request) - .retrieve() - .bodyToMono(MessageResponse.class) - .subscribe(this::check); - } catch (Exception ex) { - log.error("알림톡 전송 예외 발생"); - } - } - - private void check(MessageResponse response) { - if (response.code() != 1000) { - log.error("전송실패 errorCode : {} errorMessage : {}", response.code(), response.description()); - return; - } - log.info("알림톡 전송에 성공하였습니다."); + bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToJuniorFinish(user)); } } diff --git a/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioSend.java b/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioSend.java new file mode 100644 index 00000000..3c981791 --- /dev/null +++ b/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioSend.java @@ -0,0 +1,52 @@ +package com.postgraduate.global.bizppurio.usecase; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.postgraduate.global.bizppurio.dto.req.CommonRequest; +import com.postgraduate.global.bizppurio.dto.res.MessageResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +import java.util.function.Supplier; + +import static org.springframework.http.MediaType.APPLICATION_JSON; + +@RequiredArgsConstructor +@Slf4j +@Component +public class BizppurioSend { + private final BizppurioAuth bizppurioAuth; + private final ObjectMapper objectMapper; + private final WebClient webClient; + + @Value("${bizppurio.message}") + private String messageUrl; + + protected void sendMessageWithExceptionHandling(Supplier messageSupplier) { + try { + CommonRequest commonRequest = messageSupplier.get(); + String accessToken = bizppurioAuth.getAuth(); + String request = objectMapper.writeValueAsString(commonRequest); + webClient.post() + .uri(messageUrl) + .headers(h -> h.setContentType(APPLICATION_JSON)) + .headers(h -> h.setBearerAuth(accessToken)) + .bodyValue(request) + .retrieve() + .bodyToMono(MessageResponse.class) + .subscribe(this::check); + } catch (Exception ex) { + log.error("알림톡 전송 예외 발생: {}", ex.getMessage()); + } + } + + private void check(MessageResponse response) { + if (response.code() != 1000) { + log.error("전송실패 errorCode : {} errorMessage : {}", response.code(), response.description()); + return; + } + log.info("알림톡 전송에 성공하였습니다."); + } +} diff --git a/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioSeniorMessage.java b/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioSeniorMessage.java index 49eb346c..742e9c87 100644 --- a/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioSeniorMessage.java +++ b/src/main/java/com/postgraduate/global/bizppurio/usecase/BizppurioSeniorMessage.java @@ -1,90 +1,44 @@ package com.postgraduate.global.bizppurio.usecase; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import com.postgraduate.domain.senior.domain.entity.Senior; import com.postgraduate.domain.user.domain.entity.User; -import com.postgraduate.global.bizppurio.dto.req.CommonRequest; -import com.postgraduate.global.bizppurio.dto.res.MessageResponse; import com.postgraduate.global.bizppurio.mapper.BizppurioMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.reactive.function.client.WebClient; - -import static com.postgraduate.global.bizppurio.mapper.BizppurioMapper.*; -import static org.springframework.http.MediaType.APPLICATION_JSON; @RequiredArgsConstructor @Component @Slf4j public class BizppurioSeniorMessage { - private final WebClient webClient; private final BizppurioMapper mapper; - private final BizppurioAuth bizppurioAuth; - private final ObjectMapper objectMapper; - - @Value("${bizppurio.message}") - private String messageUrl; + private final BizppurioSend bizppurioSend; public void signUp(User user) { - CommonRequest commonRequest = mapper.mapToSeniorSignUpMessage(user); - sendMessage(commonRequest); + bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToSeniorSignUpMessage(user)); } public void mentoringApply(User user) { - CommonRequest commonRequest = mapper.mapToSeniorApplyMessage(user); - sendMessage(commonRequest); + bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToSeniorApplyMessage(user)); } public void mentoringAccept(Senior senior, String time) { - User user = senior.getUser(); - String chatLink = senior.getProfile().getChatLink(); - CommonRequest commonRequest = mapper.mapToSeniorAcceptMessage(user, chatLink, time); - sendMessage(commonRequest); + bizppurioSend.sendMessageWithExceptionHandling(() -> { + User user = senior.getUser(); + String chatLink = senior.getProfile().getChatLink(); + return mapper.mapToSeniorAcceptMessage(user, chatLink, time); + }); } public void certificationApprove(User user) { - CommonRequest commonRequest = mapper.mapToCertificationApprove(user); - sendMessage(commonRequest); + bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToCertificationApprove(user)); } public void certificationDenied(User user) { - CommonRequest commonRequest = mapper.mapToCertificationDenied(user); - sendMessage(commonRequest); + bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToCertificationDenied(user)); } public void mentoringFinish(User user) { - CommonRequest commonRequest = mapper.mapToSeniorFinish(user); - sendMessage(commonRequest); - } - - private void sendMessage(CommonRequest commonRequest) { - try { - String accessToken = bizppurioAuth.getAuth(); - String request = objectMapper.writeValueAsString(commonRequest); - webClient.post() - .uri(messageUrl) - .headers(h -> h.setContentType(APPLICATION_JSON)) - .headers(h -> h.setBearerAuth(accessToken)) - .bodyValue(request) - .retrieve() - .bodyToMono(MessageResponse.class) - .subscribe(this::check); - } catch (Exception ex) { - log.error("알림톡 전송 예외 발생"); - log.error("{}", ex.getMessage()); - } - } - - private void check(MessageResponse response) { - if (response.code() != 1000) { - log.error("전송실패 errorCode : {} errorMessage : {}", response.code(), response.description()); - return; - } - log.info("알림톡 전송에 성공하였습니다."); + bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToSeniorFinish(user)); } }