Files
sam-api/app/Services/TenantBootstrap/Steps/MenusStep.php
hskwon 48e76432ee feat: 회원가입 + 테넌트 생성 통합 API 추가 (/api/v1/register)
- 사용자 등록 + 테넌트 생성 + 시스템 관리자 권한 자동 부여
- 사업자번호 조건부 검증 (active 테넌트만 unique)
- 글로벌 메뉴 자동 복제 (parent_id 매핑 알고리즘)
- DB 트랜잭션으로 전체 프로세스 원자성 보장

추가:
- RegisterRequest: FormRequest 검증 (conditional unique)
- RegisterService: 9-step 통합 비즈니스 로직
- RegisterController: ApiResponse::handle() 패턴
- RegisterApi: 완전한 Swagger 문서

수정:
- MenusStep: 글로벌 메뉴 복제 로직 구현
- message.php: 'registered' 키 추가
- error.php: 4개 에러 메시지 추가
- routes/api.php: POST /api/v1/register 라우트

SAM API Rules 준수:
- Service-First, FormRequest, i18n, Swagger, DB Transaction
2025-11-06 17:24:42 +09:00

69 lines
2.2 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,
'code' => $menu->code ?? null,
'icon' => $menu->icon ?? null,
'url' => $menu->url ?? null,
'route_name' => $menu->route_name ?? null,
'sort_order' => $menu->sort_order ?? 0,
'is_active' => $menu->is_active ?? 1,
'depth' => $menu->depth ?? 0,
'description' => $menu->description ?? null,
'created_at' => now(),
'updated_at' => now(),
]);
// Store mapping for children menus
$parentIdMap[$menu->id] = $newId;
}
}
}