diff --git a/app/Http/Controllers/Api/V1/CommonController.php b/app/Http/Controllers/Api/V1/CommonController.php index c71873c..93fc6a4 100644 --- a/app/Http/Controllers/Api/V1/CommonController.php +++ b/app/Http/Controllers/Api/V1/CommonController.php @@ -3,17 +3,17 @@ namespace App\Http\Controllers\Api\V1; use App\Helpers\ApiResponse; +use App\Models\Products\CommonCode; +use App\Models\Scopes\TenantScope; use Illuminate\Http\Request; -use Illuminate\Support\Facades\DB; class CommonController { public static function getComeCode() { return ApiResponse::handle(function () { - return DB::table('common_codes') + return CommonCode::query() ->select(['code_group', 'code', 'name', 'description', 'is_active']) - ->where('tenant_id', app('tenant_id')) ->get(); }, '공통코드'); } @@ -36,19 +36,18 @@ public function index(Request $request, string $group) return ApiResponse::handle(function () use ($group) { $tenantId = app('tenant_id'); - // 테넌트 전용 데이터가 있으면 테넌트만, 없으면 글로벌 폴백 - $tenantCount = DB::table('common_codes') + // BelongsToTenant 스코프 해제 (글로벌 폴백 로직 직접 처리) + $base = CommonCode::withoutGlobalScope(TenantScope::class) ->where('code_group', $group) - ->where('is_active', true) - ->where('tenant_id', $tenantId) - ->count(); + ->where('is_active', true); - return DB::table('common_codes') + // 테넌트 전용 데이터가 있으면 테넌트만, 없으면 글로벌 폴백 + $hasTenantData = (clone $base)->where('tenant_id', $tenantId)->exists(); + + return (clone $base) ->select(['id', 'code', 'name', 'description', 'sort_order', 'attributes']) - ->where('code_group', $group) - ->where('is_active', true) - ->where(function ($query) use ($tenantId, $tenantCount) { - if ($tenantCount > 0) { + ->where(function ($query) use ($tenantId, $hasTenantData) { + if ($hasTenantData) { $query->where('tenant_id', $tenantId); } else { $query->whereNull('tenant_id');