Files
sam-api/app/Services/TenantBootstrap/Steps/MenusStep.php
hskwon 92c60ff39f fix: 회원가입 메뉴 생성 오류 수정 및 검증 에러 처리 개선
주요 변경사항:
- MenusStep.php: 존재하지 않는 컬럼(code, route_name, depth, description) 제거
- MenusStep.php: 실제 DB 스키마 컬럼(hidden, is_external, external_url) 추가
- RecipeRegistry.php: MenusStep 비활성화 (하이브리드 메뉴 생성 방식 도입)
- Handler.php: ValidationException 처리 개선 (실제 에러 메시지 표시, 422 상태 코드)

기술 세부사항:
- 하이브리드 접근: TenantBootstrapper(데이터) + MenuBootstrapService(메뉴)
- HTTP 상태 코드 표준화: 422 Unprocessable Entity (validation 실패)
- 실제 검증 에러 메시지 반환: errors 객체에 필드별 에러 정보 포함
2025-11-10 09:35:43 +09:00

68 lines
2.1 KiB
PHP

<?php
namespace App\Services\TenantBootstrap\Steps;
use App\Services\TenantBootstrap\Contracts\TenantBootstrapStep;
use Illuminate\Support\Facades\DB;
class MenusStep implements TenantBootstrapStep
{
public function key(): string
{
return 'menus_seed';
}
public function run(int $tenantId): void
{
if (! DB::getSchemaBuilder()->hasTable('menus')) {
return;
}
// Check if tenant already has menus
$exists = DB::table('menus')->where('tenant_id', $tenantId)->exists();
if ($exists) {
return;
}
// Get all global menus ordered by parent_id, sort_order
// Order by: root menus first (parent_id IS NULL), then by parent_id ASC, then sort_order ASC
$globalMenus = DB::table('menus')
->whereNull('tenant_id')
->orderByRaw('parent_id IS NULL DESC, parent_id ASC, sort_order ASC')
->get();
if ($globalMenus->isEmpty()) {
return;
}
$parentIdMap = []; // old_id => new_id mapping
foreach ($globalMenus as $menu) {
// Map parent_id: if parent exists in map, use new parent_id, else null
$newParentId = null;
if ($menu->parent_id !== null && isset($parentIdMap[$menu->parent_id])) {
$newParentId = $parentIdMap[$menu->parent_id];
}
// Insert new menu for tenant
$newId = DB::table('menus')->insertGetId([
'tenant_id' => $tenantId,
'parent_id' => $newParentId,
'name' => $menu->name,
'icon' => $menu->icon ?? null,
'url' => $menu->url ?? null,
'sort_order' => $menu->sort_order ?? 0,
'is_active' => $menu->is_active ?? 1,
'hidden' => $menu->hidden ?? 0,
'is_external' => $menu->is_external ?? 0,
'external_url' => $menu->external_url ?? null,
'created_at' => now(),
'updated_at' => now(),
]);
// Store mapping for children menus
$parentIdMap[$menu->id] = $newId;
}
}
}