Files
sam-manage/app/Http/Controllers/Credit/CreditController.php

245 lines
7.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Credit;
use App\Http\Controllers\Controller;
use App\Models\Coocon\CooconConfig;
use App\Services\Coocon\CooconService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;
/**
* 신용평가 컨트롤러
*/
class CreditController extends Controller
{
/**
* 신용평가 조회 페이지
* HTMX 요청 전체 페이지 리로드 (스크립트 로딩을 위해)
*/
public function inquiry(Request $request): View|Response
{
if ($request->header('HX-Request')) {
return response('', 200)->header('HX-Redirect', route('credit.inquiry.index'));
}
$service = new CooconService();
$hasConfig = $service->hasConfig();
return view('credit.inquiry.index', [
'hasConfig' => $hasConfig,
'apiTypes' => CooconService::API_NAMES,
]);
}
/**
* 신용정보 조회 API
*/
public function search(Request $request): JsonResponse
{
$request->validate([
'company_key' => 'required|string|max:20',
'api_type' => 'nullable|string|in:OA12,OA13,OA14,OA15,OA16,OA17,all',
]);
$companyKey = $request->input('company_key');
$apiType = $request->input('api_type', 'all');
$service = new CooconService();
if (!$service->hasConfig()) {
return response()->json([
'success' => false,
'error' => '쿠콘 API 설정이 없습니다. 설정을 먼저 등록해주세요.',
], 400);
}
$result = match ($apiType) {
'OA12' => ['summary' => $service->getCreditSummary($companyKey)],
'OA13' => ['shortTermOverdue' => $service->getShortTermOverdueInfo($companyKey)],
'OA14' => ['negativeInfoKCI' => $service->getNegativeInfoKCI($companyKey)],
'OA15' => ['negativeInfoCB' => $service->getNegativeInfoCB($companyKey)],
'OA16' => ['suspensionInfo' => $service->getSuspensionInfo($companyKey)],
'OA17' => ['workoutInfo' => $service->getWorkoutInfo($companyKey)],
default => $service->getAllCreditInfo($companyKey),
};
return response()->json([
'success' => true,
'data' => $result,
'company_key' => $companyKey,
'api_type' => $apiType,
]);
}
/**
* 설정 관리 페이지
*/
public function settings(Request $request): View|Response
{
if ($request->header('HX-Request')) {
return response('', 200)->header('HX-Redirect', route('credit.settings.index'));
}
$configs = CooconConfig::orderBy('environment')
->orderBy('is_active', 'desc')
->orderBy('created_at', 'desc')
->get();
return view('credit.settings.index', [
'configs' => $configs,
]);
}
/**
* 설정 생성
*/
public function createConfig(): View
{
return view('credit.settings.create');
}
/**
* 설정 저장
*/
public function storeConfig(Request $request): JsonResponse
{
$validated = $request->validate([
'name' => 'required|string|max:100',
'environment' => 'required|in:test,production',
'api_key' => 'required|string|max:100',
'base_url' => 'required|url|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean',
]);
// 같은 환경에서 활성화된 설정이 이미 있으면 비활성화
if ($validated['is_active'] ?? false) {
CooconConfig::where('environment', $validated['environment'])
->where('is_active', true)
->update(['is_active' => false]);
}
$config = CooconConfig::create($validated);
return response()->json([
'success' => true,
'message' => '설정이 저장되었습니다.',
'data' => $config,
]);
}
/**
* 설정 수정
*/
public function editConfig(int $id): View
{
$config = CooconConfig::findOrFail($id);
return view('credit.settings.edit', [
'config' => $config,
]);
}
/**
* 설정 업데이트
*/
public function updateConfig(Request $request, int $id): JsonResponse
{
$config = CooconConfig::findOrFail($id);
$validated = $request->validate([
'name' => 'required|string|max:100',
'environment' => 'required|in:test,production',
'api_key' => 'required|string|max:100',
'base_url' => 'required|url|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean',
]);
// 같은 환경에서 활성화된 설정이 이미 있으면 비활성화 (자기 자신 제외)
if ($validated['is_active'] ?? false) {
CooconConfig::where('environment', $validated['environment'])
->where('is_active', true)
->where('id', '!=', $id)
->update(['is_active' => false]);
}
$config->update($validated);
return response()->json([
'success' => true,
'message' => '설정이 수정되었습니다.',
'data' => $config,
]);
}
/**
* 설정 삭제
*/
public function deleteConfig(int $id): JsonResponse
{
$config = CooconConfig::findOrFail($id);
$config->delete();
return response()->json([
'success' => true,
'message' => '설정이 삭제되었습니다.',
]);
}
/**
* 설정 활성화/비활성화 토글
*/
public function toggleConfig(int $id): JsonResponse
{
$config = CooconConfig::findOrFail($id);
if (!$config->is_active) {
// 같은 환경에서 활성화된 설정이 이미 있으면 비활성화
CooconConfig::where('environment', $config->environment)
->where('is_active', true)
->update(['is_active' => false]);
}
$config->update(['is_active' => !$config->is_active]);
return response()->json([
'success' => true,
'message' => $config->is_active ? '설정이 활성화되었습니다.' : '설정이 비활성화되었습니다.',
'is_active' => $config->is_active,
]);
}
/**
* API 연결 테스트
*/
public function testConnection(Request $request): JsonResponse
{
$request->validate([
'company_key' => 'required|string|max:20',
]);
$companyKey = $request->input('company_key');
$service = new CooconService();
if (!$service->hasConfig()) {
return response()->json([
'success' => false,
'error' => '쿠콘 API 설정이 없습니다.',
], 400);
}
// 신용요약정보 API로 테스트
$result = $service->getCreditSummary($companyKey);
return response()->json([
'success' => $result['success'],
'message' => $result['success'] ? 'API 연결 테스트 성공' : 'API 연결 테스트 실패',
'result' => $result,
]);
}
}