Skip to content

Commit

Permalink
Merge pull request #107 from Team-Daengnyang/fix/#105
Browse files Browse the repository at this point in the history
[FIX] 거래 내역 조회 수정
  • Loading branch information
csb9427 authored Aug 28, 2024
2 parents 1054c17 + 894d8d5 commit c7e9aae
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.fav.daengnyang.domain.targetDetail.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fav.daengnyang.domain.target.service.dto.response.TargetDetailResponse;
import com.fav.daengnyang.domain.targetDetail.service.dto.request.TransactionDetailRequest;
import com.fav.daengnyang.domain.targetDetail.service.dto.response.TargetDetailSummaryResponse;
import com.fav.daengnyang.domain.targetDetail.service.TargetDetailService;
import com.fav.daengnyang.domain.targetDetail.service.dto.response.TransactionDetailResponse;
import com.fav.daengnyang.global.auth.dto.MemberPrincipal;
import com.fav.daengnyang.global.web.dto.response.SuccessResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/target-detail")
Expand All @@ -19,9 +26,14 @@ public class TargetDetailController {
private final TargetDetailService targetDetailService;

@GetMapping("")
public ResponseEntity<SuccessResponse<TargetDetailSummaryResponse>> getTargetDetailSummary(
@RequestParam Long accountId, @RequestParam String accountNo) throws JsonProcessingException {
TargetDetailSummaryResponse summary = targetDetailService.getTargetDetailSummary(accountId, accountNo);
return ResponseEntity.ok(SuccessResponse.ok(summary));
public ResponseEntity<SuccessResponse<List<TransactionDetailResponse>>> getTransactionDetails(
@AuthenticationPrincipal MemberPrincipal memberPrincipal,
@RequestParam String accountNo,
@RequestParam String startDate,
@RequestParam String endDate) throws JsonProcessingException {
TransactionDetailRequest request = new TransactionDetailRequest(startDate, endDate, accountNo);

List<TransactionDetailResponse> responses = targetDetailService.getTransactionDetails(accountNo, memberPrincipal.getUserKey(), request);
return ResponseEntity.ok(SuccessResponse.ok(responses));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fav.daengnyang.domain.targetDetail.repository.TargetDetailRepository;
import com.fav.daengnyang.domain.targetDetail.service.dto.response.TargetDetailSummaryResponse;
import com.fav.daengnyang.domain.targetDetail.service.dto.request.TransactionDetailRequest;
import com.fav.daengnyang.domain.targetDetail.service.dto.response.TransactionDetailResponse;
import com.fav.daengnyang.global.web.dto.response.TransactionUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
Expand All @@ -15,120 +16,96 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TargetDetailService {

private final TargetDetailRepository targetDetailRepository;
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper;

@Value("${api.key}")
private String apiKey;

// Account 요약 정보 가져오기 메서드
public TargetDetailSummaryResponse getTargetDetailSummary(Long accountId, String accountNo) throws JsonProcessingException {
Integer totalAmount = targetDetailRepository.findTotalAmountByAccountId(accountId);
Long transactionCount = targetDetailRepository.countTransactionsByAccountId(accountId);

// 금융 API 호출 예시
Map<String, Object> balanceResponse = callInquireAccountBalanceApi(accountNo);
Map<String, Object> historyResponse = callInquireTransactionHistoryApi(accountNo);

return TargetDetailSummaryResponse.builder()
.totalAmount(totalAmount)
.transactionCount(transactionCount)
.apiBalance(balanceResponse.toString())
.apiHistory(historyResponse.toString())
.build();
}

// 금융 API 호출 (계좌 잔액 조회)
private Map<String, Object> callInquireAccountBalanceApi(String accountNo) throws JsonProcessingException {
// Header 및 Body 생성
Map<String, Object> body = createBalanceRequest(accountNo);
// 거래 내역 리스트 반환 메서드
public List<TransactionDetailResponse> getTransactionDetails(String accountNo, String userKey, TransactionDetailRequest request) throws JsonProcessingException {
List<TransactionDetailResponse> transactionDetails = new ArrayList<>();

// HttpHeaders 설정
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// HttpEntity 생성
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(body, headers);

// 외부 API 호출
String url = "https://finopenapi.ssafy.io/ssafy/api/v1/edu/demandDeposit/inquireDemandDepositAccountBalance";
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
// 금융 API 호출 및 리스트 추출
extractTransactionDetails(accountNo, userKey, request, transactionDetails);

// 응답 처리
if (response.getStatusCode().is2xxSuccessful()) {
return objectMapper.readValue(response.getBody(), Map.class);
} else {
throw new RuntimeException("API 호출 오류: " + response.getBody());
}
return transactionDetails;
}

// 금융 API 호출 (거래 내역 조회)
private Map<String, Object> callInquireTransactionHistoryApi(String accountNo) throws JsonProcessingException {
// 금융 API 호출 거래 내역 리스트 추출
private void extractTransactionDetails(String accountNo, String userKey, TransactionDetailRequest request, List<TransactionDetailResponse> transactionDetails) throws JsonProcessingException {
// Header 및 Body 생성
Map<String, Object> body = createTransactionHistoryRequest(accountNo);
Map<String, Object> requestBody = createRequestBody(accountNo, userKey, request);

// HttpHeaders 설정
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// HttpEntity 생성
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(body, headers);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);

// 외부 API 호출
String url = "https://finopenapi.ssafy.io/ssafy/api/v1/edu/demandDeposit/inquireTransactionHistory";
String url = "/edu/demandDeposit/inquireTransactionHistoryList";
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

// 응답 처리
if (response.getStatusCode().is2xxSuccessful()) {
return objectMapper.readValue(response.getBody(), Map.class);
Map<String, Object> responseMap = objectMapper.readValue(response.getBody(), Map.class);
Map<String, Object> rec = (Map<String, Object>) responseMap.get("REC");
List<Map<String, Object>> transactionList = (List<Map<String, Object>>) rec.get("list");

// 각 거래 항목의 세부 정보를 리스트에 추가
for (Map<String, Object> transaction : transactionList) {
TransactionDetailResponse detail = TransactionDetailResponse.builder()
.transactionUniqueNo(Long.valueOf((String) transaction.get("transactionUniqueNo")))
.transactionDate((String) transaction.get("transactionDate"))
.transactionTime((String) transaction.get("transactionTime"))
.transactionBalance(Long.valueOf(transaction.get("transactionBalance").toString()))
.transactionAfterBalance(Long.valueOf(transaction.get("transactionAfterBalance").toString()))
.transactionSummary((String) transaction.get("transactionSummary"))
.build();
transactionDetails.add(detail);
}
} else {
throw new RuntimeException("API 호출 오류: " + response.getBody());
}
}

// 잔액 조회 요청 생성
private Map<String, Object> createBalanceRequest(String accountNo) {
return Map.of(
"Header", createHeader("inquireDemandDepositAccountBalance"),
"accountNo", accountNo
);
}

// 거래 내역 조회 요청 생성
private Map<String, Object> createTransactionHistoryRequest(String accountNo) {
return Map.of(
"Header", createHeader("inquireTransactionHistory"),
"accountNo", accountNo,
"startDate", "20240101",
"endDate", "20240331",
"transactionType", "A",
"orderByType", "ASC"
);
}

// 공통 Header 생성
private Map<String, String> createHeader(String apiName) {
// 공통 Header 및 Body 생성
private Map<String, Object> createRequestBody(String accountNo, String userKey, TransactionDetailRequest request) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmmss");

Map<String, String> header = new HashMap<>();
header.put("apiName", apiName);
header.put("apiName", "inquireTransactionHistoryList");
header.put("transmissionDate", now.format(dateFormatter));
header.put("transmissionTime", now.format(timeFormatter));
header.put("institutionCode", "00100");
header.put("fintechAppNo", "001");
header.put("apiServiceCode", "inquireTransactionHistoryList");
header.put("institutionTransactionUniqueNo", TransactionUtil.generateUniqueTransactionNo());
header.put("apiKey", apiKey);
header.put("userKey", "user_key_value");

return header;
header.put("userKey", userKey);

// Body 생성
Map<String, Object> body = new HashMap<>();
body.put("Header", header);
body.put("accountNo", accountNo);
body.put("startDate", request.getStartDate());
body.put("endDate", request.getEndDate());
body.put("transactionType", "A");
body.put("orderByType", "ASC");

return body;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.fav.daengnyang.domain.targetDetail.service.dto.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TransactionDetailRequest {
private String startDate;
private String endDate;
private String accountNo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.fav.daengnyang.domain.targetDetail.service.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TransactionDetailResponse {
private Long transactionUniqueNo;
private String transactionDate;
private String transactionTime;
private Long transactionBalance;
private Long transactionAfterBalance;
private String transactionSummary;
}

0 comments on commit c7e9aae

Please sign in to comment.