fix(API): 공통코드 조회 DB::table → CommonCode 모델 전환

- DB::table() 직접 쿼리 → CommonCode 모델 사용으로 변경
- SoftDeletes 자동 적용되어 삭제된 레코드 제외
- getComeCode()도 모델 전환 (TenantScope 자동 적용)
- index()는 TenantScope 해제 후 테넌트/글로벌 폴백 직접 처리

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-31 00:10:36 +09:00
parent 9bd585bdf3
commit e5a293ab12

View File

@@ -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');