Files
sam-api/app/Services/TenantBootstrap/Steps/MenusStep.php
hskwon b8e96be56c refactor: 회원가입 메뉴 복사 로직을 global_menus 테이블 기반으로 변경
- MenuBootstrapService: menus → global_menus 테이블에서 조회
- MenusStep: 신규 테넌트 부트스트랩 시 global_menus 사용
- GlobalMenuTemplateSeeder: GlobalMenu 모델 사용으로 변경
2025-12-02 22:55:10 +09:00

76 lines
2.5 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;
}
// Check if global_menus table exists
if (! DB::getSchemaBuilder()->hasTable('global_menus')) {
return;
}
// Get all global menus from global_menus table 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('global_menus')
->whereNull('deleted_at')
->where('is_active', true)
->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 with global_menu_id reference
$newId = DB::table('menus')->insertGetId([
'tenant_id' => $tenantId,
'parent_id' => $newParentId,
'global_menu_id' => $menu->id, // global_menus 테이블의 ID 참조
'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_customized' => false, // 원본 상태
'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;
}
}
}