2025-11-21 14:46:13 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Requests\StoreTenantRequest;
|
|
|
|
|
use App\Http\Requests\UpdateTenantRequest;
|
|
|
|
|
use App\Services\TenantService;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class TenantController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly TenantService $tenantService
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 목록 조회
|
|
|
|
|
*/
|
2025-12-27 20:05:49 +09:00
|
|
|
public function index(Request $request): JsonResponse|\Illuminate\Http\Response
|
2025-11-21 14:46:13 +09:00
|
|
|
{
|
|
|
|
|
$tenants = $this->tenantService->getTenants(
|
|
|
|
|
$request->all(),
|
2025-11-24 21:21:22 +09:00
|
|
|
$request->integer('per_page', 10)
|
2025-11-21 14:46:13 +09:00
|
|
|
);
|
|
|
|
|
|
2025-12-27 20:05:49 +09:00
|
|
|
// HTMX 요청 시 순수 HTML 반환
|
2025-11-21 14:46:13 +09:00
|
|
|
if ($request->header('HX-Request')) {
|
2025-12-27 20:05:49 +09:00
|
|
|
return response(
|
|
|
|
|
view('tenants.partials.table', compact('tenants'))->render(),
|
|
|
|
|
200,
|
|
|
|
|
['Content-Type' => 'text/html']
|
|
|
|
|
);
|
2025-11-21 14:46:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 일반 요청 시 JSON 반환
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => $tenants->items(),
|
|
|
|
|
'meta' => [
|
|
|
|
|
'current_page' => $tenants->currentPage(),
|
|
|
|
|
'last_page' => $tenants->lastPage(),
|
|
|
|
|
'per_page' => $tenants->perPage(),
|
|
|
|
|
'total' => $tenants->total(),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 생성
|
|
|
|
|
*/
|
|
|
|
|
public function store(StoreTenantRequest $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$tenant = $this->tenantService->createTenant($request->validated());
|
|
|
|
|
|
|
|
|
|
// HTMX 요청 시 성공 메시지와 리다이렉트 헤더 반환
|
|
|
|
|
if ($request->header('HX-Request')) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 생성되었습니다.',
|
|
|
|
|
'redirect' => route('tenants.index'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 생성되었습니다.',
|
|
|
|
|
'data' => $tenant,
|
|
|
|
|
], 201);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 특정 테넌트 조회
|
|
|
|
|
*/
|
|
|
|
|
public function show(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$tenant = $this->tenantService->getTenantById($id, true);
|
|
|
|
|
|
2025-11-25 11:06:03 +09:00
|
|
|
if (! $tenant) {
|
2025-11-21 14:46:13 +09:00
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => '테넌트를 찾을 수 없습니다.',
|
|
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HTMX 요청 시 HTML 반환
|
|
|
|
|
if ($request->header('HX-Request')) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'html' => view('tenants.partials.detail', compact('tenant'))->render(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => $tenant,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 수정
|
|
|
|
|
*/
|
|
|
|
|
public function update(UpdateTenantRequest $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$this->tenantService->updateTenant($id, $request->validated());
|
|
|
|
|
|
|
|
|
|
// HTMX 요청 시 성공 메시지와 리다이렉트 헤더 반환
|
|
|
|
|
if ($request->header('HX-Request')) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 수정되었습니다.',
|
|
|
|
|
'redirect' => route('tenants.index'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 수정되었습니다.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 삭제 (Soft Delete)
|
|
|
|
|
*/
|
|
|
|
|
public function destroy(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$this->tenantService->deleteTenant($id);
|
|
|
|
|
|
|
|
|
|
// HTMX 요청 시 테이블 행 제거 트리거
|
|
|
|
|
if ($request->header('HX-Request')) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 삭제되었습니다.',
|
|
|
|
|
'action' => 'remove',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 삭제되었습니다.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 복원
|
|
|
|
|
*/
|
|
|
|
|
public function restore(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$this->tenantService->restoreTenant($id);
|
|
|
|
|
|
|
|
|
|
// HTMX 요청 시 테이블 새로고침 트리거
|
|
|
|
|
if ($request->header('HX-Request')) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 복원되었습니다.',
|
|
|
|
|
'action' => 'refresh',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 복원되었습니다.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 영구 삭제 (슈퍼관리자 전용)
|
|
|
|
|
*/
|
|
|
|
|
public function forceDestroy(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
// 슈퍼관리자 권한 체크
|
2025-11-25 11:06:03 +09:00
|
|
|
if (! auth()->user()?->is_super_admin) {
|
2025-11-21 14:46:13 +09:00
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => '권한이 없습니다.',
|
|
|
|
|
], 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->tenantService->forceDeleteTenant($id);
|
|
|
|
|
|
|
|
|
|
// HTMX 요청 시 테이블 행 제거 트리거
|
|
|
|
|
if ($request->header('HX-Request')) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 영구 삭제되었습니다.',
|
|
|
|
|
'action' => 'remove',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => '테넌트가 영구 삭제되었습니다.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 통계 조회
|
|
|
|
|
*/
|
|
|
|
|
public function stats(Request $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$stats = $this->tenantService->getTenantStats();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => $stats,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2025-11-27 19:11:32 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 모달 정보 조회
|
|
|
|
|
*/
|
|
|
|
|
public function modal(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$tenant = $this->tenantService->getTenantForModal($id);
|
|
|
|
|
|
|
|
|
|
if (! $tenant) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => '테넌트를 찾을 수 없습니다.',
|
|
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$html = view('tenants.partials.modal-info', compact('tenant'))->render();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'html' => $html,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 사용자 탭 데이터
|
|
|
|
|
*/
|
|
|
|
|
public function users(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$users = $this->tenantService->getTenantUsers($id);
|
|
|
|
|
|
|
|
|
|
$html = view('tenants.partials.modal-users', compact('users'))->render();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'html' => $html,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 부서 탭 데이터
|
|
|
|
|
*/
|
|
|
|
|
public function departments(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$departments = $this->tenantService->getTenantDepartments($id);
|
|
|
|
|
|
|
|
|
|
$html = view('tenants.partials.modal-departments', compact('departments'))->render();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'html' => $html,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 역할 탭 데이터
|
|
|
|
|
*/
|
|
|
|
|
public function roles(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$roles = $this->tenantService->getTenantRoles($id);
|
|
|
|
|
|
|
|
|
|
$html = view('tenants.partials.modal-roles', compact('roles'))->render();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'html' => $html,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 메뉴 탭 데이터
|
|
|
|
|
*/
|
|
|
|
|
public function menus(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$menus = $this->tenantService->getTenantMenus($id);
|
|
|
|
|
|
|
|
|
|
$html = view('tenants.partials.modal-menus', compact('menus'))->render();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'html' => $html,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 테넌트 구독 정보 탭 데이터
|
|
|
|
|
*/
|
|
|
|
|
public function subscription(Request $request, int $id): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$tenant = $this->tenantService->getTenantForModal($id);
|
|
|
|
|
|
|
|
|
|
if (! $tenant) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => '테넌트를 찾을 수 없습니다.',
|
|
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subscription = $this->tenantService->getTenantSubscription($id);
|
|
|
|
|
$usage = $this->tenantService->getTenantUsage($id);
|
|
|
|
|
|
|
|
|
|
$html = view('tenants.partials.modal-subscription', compact('tenant', 'subscription', 'usage'))->render();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'html' => $html,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2025-11-25 11:06:03 +09:00
|
|
|
}
|