OBP 수수료 출력 연동시 에러 메세지 개선

This commit is contained in:
Rinjae
2025-12-22 18:00:16 +09:00
2 changed files with 179 additions and 90 deletions
@@ -39,6 +39,8 @@ public class CommissionController {
private final ExternalService externalService;
private final PortalPropertyService portalPropertyService;
private final ObjectMapper objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@GetMapping("/manage")
public String commissionManagePage(Model model) {
@@ -53,59 +55,20 @@ public class CommissionController {
@PathVariable("month") String month,
Model model) {
CommissionDTO commissionDTO = null;
CommissionSearch search = new CommissionSearch();
search.setPartnerCode(SecurityUtil.getUserOrg().getOrgCode());
search.setYear(year);
search.setMonth(month);
if (!StringUtils.isEmpty(search.getPartnerCode())) {
try {
String obmUrl = portalPropertyService.getPortalPropertiesAsMap("Portal").getOrDefault("obp.obm.url", "");
String url = obmUrl + Constants.COMMISSION_PRINT_URL;
String result = externalService.getResponseHttpPost(url, toJson(search));
if (StringUtils.isEmpty(result)) {
model.addAttribute("error", "외부 서버 연결에 실패하였습니다.");
return "apps/commission/commissionPrint";
}
HashMap<String, Object> returnObj = new ObjectMapper().readValue(result, HashMap.class);
CommissionResult result = fetchCommissionData(search, Constants.COMMISSION_PRINT_URL);
if (!StringUtils.isEmpty(returnObj.get("error"))) {
HashMap<String, Object> error = (HashMap) returnObj.get("error");
model.addAttribute("error", error.get("message").toString());
return "apps/commission/commissionPrint";
}
if (StringUtils.pathEquals(search.getPartnerCode(), "000002-01")) {
log.debug("Found Together : " + search.getPartnerCode());
search.setPartnerCode("000002-02");
String togetherResult = externalService.getResponseHttpPost(url, toJson(search));
if (StringUtils.isEmpty(togetherResult)) {
model.addAttribute("error", "외부 서버 연결에 실패하였습니다.");
return "apps/commission/commissionPrint";
}
HashMap togetherLendingReturnObj = new ObjectMapper().readValue(togetherResult, HashMap.class);
log.debug("togetherLendingReturnObj : " + togetherLendingReturnObj.toString());
if (!StringUtils.isEmpty(togetherLendingReturnObj.get("error"))) {
HashMap error = (HashMap) togetherLendingReturnObj.get("error");
model.addAttribute("error", error.get("message").toString());
return "apps/commission/commissionPrint";
}
HashMap<String, Object> mergeMap = mergeCommissionData(returnObj, togetherLendingReturnObj);
result = new ObjectMapper().writeValueAsString(mergeMap);
}
commissionDTO = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(result, CommissionDTO.class);
} catch (Exception e) {
log.warn(e.getMessage(), e);
model.addAttribute("error", "수수료 조회에 실패하였습니다.");
return "apps/commission/commissionPrint";
}
if (result.hasError()) {
model.addAttribute("error", result.getError());
return "apps/commission/commissionPrint";
}
// Model에 데이터 바인딩
model.addAttribute("commission", commissionDTO);
model.addAttribute("commission", result.getData());
model.addAttribute("organization", SecurityUtil.getUserOrg().getOrgName());
model.addAttribute("year", year);
model.addAttribute("month", month);
@@ -116,51 +79,116 @@ public class CommissionController {
// ==================== API Endpoints ====================
@PostMapping()
public ResponseEntity<?> list(@RequestBody CommissionSearch search) {
CommissionDTO commissionDTO = null;
@PostMapping("/print/check")
public ResponseEntity<?> checkPrintData(@RequestBody CommissionSearch search) {
search.setPartnerCode(SecurityUtil.getUserOrg().getOrgCode());
if (!StringUtils.isEmpty(search.getPartnerCode())) {
try {
String obmUrl = portalPropertyService.getPortalPropertiesAsMap("Portal").getOrDefault("obp.obm.url", "");
String url = obmUrl + Constants.COMMISSION_URL;
String result = externalService.getResponseHttpPost(url, toJson(search));
if (StringUtils.isEmpty(result)) {
return ErrorUtil.create("fail.commission.serverError", "외부 서버 연결에 실패하였습니다.");
}
HashMap<String, Object> returnObj = new ObjectMapper().readValue(result, HashMap.class);
if (!StringUtils.isEmpty(returnObj.get("error"))) {
HashMap<String, Object> error = (HashMap) returnObj.get("error");
return ErrorUtil.create("fail.commission.serverError", error.get("message").toString());
}
if (StringUtils.pathEquals(search.getPartnerCode(), "000002-01")) {
log.debug("Found Together : " + search.getPartnerCode());
search.setPartnerCode("000002-02");
String togetherResult = externalService.getResponseHttpPost(url, toJson(search));
if (StringUtils.isEmpty(togetherResult)) {
return ErrorUtil.create("fail.commission.serverError", "외부 서버 연결에 실패하였습니다.");
}
HashMap<String, Object> togetherLendingReturnObj = new ObjectMapper().readValue(togetherResult, HashMap.class);
log.debug("togetherLendingReturnObj : " + togetherLendingReturnObj.toString());
if (!StringUtils.isEmpty(togetherLendingReturnObj.get("error"))) {
HashMap<String, Object> error = (HashMap) togetherLendingReturnObj.get("error");
return ErrorUtil.create("fail.commission.serverError", error.get("message").toString());
}
HashMap<String, Object> mergeMap = mergeCommissionData(returnObj, togetherLendingReturnObj);
result = new ObjectMapper().writeValueAsString(mergeMap);
}
commissionDTO = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(result, CommissionDTO.class);
} catch (Exception e) {
log.warn(e.getMessage(), e);
return ErrorUtil.create("fail.commission.list", "수수료 조회에 실패하였습니다.");
}
return ResponseEntity.ok().body(commissionDTO);
if (StringUtils.isEmpty(search.getPartnerCode())) {
return ErrorUtil.create("fail.commission.needPartnerCode");
}
return ErrorUtil.create("fail.commission.needPartnerCode");
CommissionResult result = fetchCommissionData(search, Constants.COMMISSION_PRINT_URL);
if (result.hasError()) {
return ErrorUtil.create("fail.commission.print", result.getError());
}
return ResponseEntity.ok().body(result.getData());
}
@PostMapping()
public ResponseEntity<?> list(@RequestBody CommissionSearch search) {
search.setPartnerCode(SecurityUtil.getUserOrg().getOrgCode());
if (StringUtils.isEmpty(search.getPartnerCode())) {
return ErrorUtil.create("fail.commission.needPartnerCode");
}
CommissionResult result = fetchCommissionData(search, Constants.COMMISSION_URL);
if (result.hasError()) {
return ErrorUtil.create("fail.commission.list", result.getError());
}
return ResponseEntity.ok().body(result.getData());
}
// ==================== Private Methods ====================
/**
* 외부 API를 호출하여 수수료 데이터를 조회합니다.
*
* @param search 검색 조건
* @param apiPath API 경로 (Constants.COMMISSION_URL 또는 Constants.COMMISSION_PRINT_URL)
* @return 조회 결과
*/
private CommissionResult fetchCommissionData(CommissionSearch search, String apiPath) {
if (StringUtils.isEmpty(search.getPartnerCode())) {
return CommissionResult.error("파트너 코드가 없습니다.");
}
try {
String obmUrl = portalPropertyService.getPortalPropertiesAsMap("Portal").getOrDefault("obp.obm.url", "");
String url = obmUrl + apiPath;
// 첫 번째 API 호출
String result = externalService.getResponseHttpPost(url, toJson(search));
if (StringUtils.isEmpty(result)) {
return CommissionResult.error("외부 서버 연결에 실패하였습니다.");
}
HashMap<String, Object> returnObj = objectMapper.readValue(result, HashMap.class);
// 에러 응답 체크
String errorMessage = extractErrorMessage(returnObj);
if (errorMessage != null) {
return CommissionResult.error(errorMessage);
}
// Together 관련 추가 처리 (000002-01)
if (StringUtils.pathEquals(search.getPartnerCode(), "000002-01")) {
log.debug("Found Together : " + search.getPartnerCode());
CommissionSearch togetherSearch = new CommissionSearch();
togetherSearch.setPartnerCode("000002-02");
togetherSearch.setYear(search.getYear());
togetherSearch.setMonth(search.getMonth());
String togetherResult = externalService.getResponseHttpPost(url, toJson(togetherSearch));
if (StringUtils.isEmpty(togetherResult)) {
return CommissionResult.error("외부 서버 연결에 실패하였습니다.");
}
HashMap<String, Object> togetherReturnObj = objectMapper.readValue(togetherResult, HashMap.class);
log.debug("togetherLendingReturnObj : " + togetherReturnObj.toString());
String togetherErrorMessage = extractErrorMessage(togetherReturnObj);
if (togetherErrorMessage != null) {
return CommissionResult.error(togetherErrorMessage);
}
HashMap<String, Object> mergeMap = mergeCommissionData(returnObj, togetherReturnObj);
result = objectMapper.writeValueAsString(mergeMap);
}
CommissionDTO commissionDTO = objectMapper.readValue(result, CommissionDTO.class);
return CommissionResult.success(commissionDTO);
} catch (Exception e) {
log.warn(e.getMessage(), e);
return CommissionResult.error("수수료 조회에 실패하였습니다.");
}
}
/**
* 응답에서 에러 메시지를 추출합니다.
*/
private String extractErrorMessage(HashMap<String, Object> response) {
if (!StringUtils.isEmpty(response.get("error"))) {
HashMap<String, Object> error = (HashMap) response.get("error");
return error.get("message").toString();
}
return null;
}
private String toJson(CommissionSearch search) {
@@ -169,7 +197,7 @@ public class CommissionController {
jsonMap.put("partnerCode", search.getPartnerCode());
jsonMap.put("targetOnMonth", search.getYear() + search.getMonth());
return new ObjectMapper().writeValueAsString(jsonMap);
return objectMapper.writeValueAsString(jsonMap);
} catch (Exception e) {
log.warn("Failed to serialize CommissionSearch to JSON", e);
return "{}";
@@ -252,4 +280,38 @@ public class CommissionController {
return mergeMap;
}
// ==================== Inner Classes ====================
/**
* 수수료 조회 결과를 담는 내부 클래스
*/
private static class CommissionResult {
private CommissionDTO data;
private String error;
static CommissionResult success(CommissionDTO data) {
CommissionResult result = new CommissionResult();
result.data = data;
return result;
}
static CommissionResult error(String message) {
CommissionResult result = new CommissionResult();
result.error = message;
return result;
}
boolean hasError() {
return error != null;
}
CommissionDTO getData() {
return data;
}
String getError() {
return error;
}
}
}
@@ -431,9 +431,36 @@
return;
}
// 별도 창으로 청구서 출력 페이지 열기
const printUrl = '/commission/print/' + year + '/' + month;
window.open(printUrl, '_blank', 'width=800,height=900,scrollbars=yes,resizable=yes');
// 먼저 청구서 출력용 API로 데이터 조회하여 에러 여부 확인
fetch('/commission/print/check', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-XSRF-TOKEN': /*[[${_csrf.token}]]*/ ''
},
body: JSON.stringify({
year: year,
month: month
})
})
.then(response => {
return response.json().then(data => {
if (!response.ok) {
const errorMessage = data.description || data.message || '데이터 조회에 실패했습니다.';
throw new Error(errorMessage);
}
return data;
});
})
.then(data => {
// 에러가 없으면 청구서 출력 페이지 열기
const printUrl = '/commission/print/' + year + '/' + month;
window.open(printUrl, '_blank', 'width=800,height=900,scrollbars=yes,resizable=yes');
})
.catch(error => {
console.error('Error:', error);
customPopups.showAlert(error.message || '청구서 조회 중 오류가 발생했습니다.');
});
}
</script>
</th:block>