feat: 공정 단계(ProcessStep) CRUD API 구현

- process_steps 테이블 마이그레이션 생성 (step_code, sort_order, boolean 플래그 등)
- ProcessStep 모델 생성 (child entity 패턴, HasFactory만 사용)
- ProcessStepService: CRUD + reorder + STP-001 자동채번
- ProcessStepController: DI + ApiResponse::handle 패턴
- FormRequest 3개: Store, Update, Reorder
- Process 모델에 steps() HasMany 관계 추가
- ProcessService eager-load에 steps 추가 (5곳)
- Nested routes: /processes/{processId}/steps

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-03 18:59:12 +09:00
parent 529c587023
commit 3d20c6979d
10 changed files with 441 additions and 5 deletions

View File

@@ -0,0 +1,84 @@
<?php
namespace App\Http\Controllers\V1;
use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\ProcessStep\ReorderProcessStepRequest;
use App\Http\Requests\V1\ProcessStep\StoreProcessStepRequest;
use App\Http\Requests\V1\ProcessStep\UpdateProcessStepRequest;
use App\Services\ProcessStepService;
use Illuminate\Http\JsonResponse;
class ProcessStepController extends Controller
{
public function __construct(
private readonly ProcessStepService $processStepService
) {}
/**
* 공정 단계 목록 조회
*/
public function index(int $processId): JsonResponse
{
return ApiResponse::handle(
fn () => $this->processStepService->index($processId),
'message.fetched'
);
}
/**
* 공정 단계 상세 조회
*/
public function show(int $processId, int $stepId): JsonResponse
{
return ApiResponse::handle(
fn () => $this->processStepService->show($processId, $stepId),
'message.fetched'
);
}
/**
* 공정 단계 생성
*/
public function store(StoreProcessStepRequest $request, int $processId): JsonResponse
{
return ApiResponse::handle(
fn () => $this->processStepService->store($processId, $request->validated()),
'message.created'
);
}
/**
* 공정 단계 수정
*/
public function update(UpdateProcessStepRequest $request, int $processId, int $stepId): JsonResponse
{
return ApiResponse::handle(
fn () => $this->processStepService->update($processId, $stepId, $request->validated()),
'message.updated'
);
}
/**
* 공정 단계 삭제
*/
public function destroy(int $processId, int $stepId): JsonResponse
{
return ApiResponse::handle(
fn () => $this->processStepService->destroy($processId, $stepId),
'message.deleted'
);
}
/**
* 공정 단계 순서 변경
*/
public function reorder(ReorderProcessStepRequest $request, int $processId): JsonResponse
{
return ApiResponse::handle(
fn () => $this->processStepService->reorder($processId, $request->validated('items')),
'message.reordered'
);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests\V1\ProcessStep;
use Illuminate\Foundation\Http\FormRequest;
class ReorderProcessStepRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'items' => ['required', 'array', 'min:1'],
'items.*.id' => ['required', 'integer'],
'items.*.sort_order' => ['required', 'integer', 'min:0'],
];
}
public function attributes(): array
{
return [
'items' => '정렬 항목',
'items.*.id' => '단계 ID',
'items.*.sort_order' => '정렬순서',
];
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Http\Requests\V1\ProcessStep;
use Illuminate\Foundation\Http\FormRequest;
class StoreProcessStepRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'step_name' => ['required', 'string', 'max:100'],
'is_required' => ['nullable', 'boolean'],
'needs_approval' => ['nullable', 'boolean'],
'needs_inspection' => ['nullable', 'boolean'],
'is_active' => ['nullable', 'boolean'],
'connection_type' => ['nullable', 'string', 'max:20'],
'connection_target' => ['nullable', 'string', 'max:255'],
'completion_type' => ['nullable', 'string', 'max:30'],
];
}
public function attributes(): array
{
return [
'step_name' => '단계명',
'is_required' => '필수여부',
'needs_approval' => '승인필요여부',
'needs_inspection' => '검사필요여부',
'is_active' => '사용여부',
'connection_type' => '연결유형',
'connection_target' => '연결대상',
'completion_type' => '완료유형',
];
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Http\Requests\V1\ProcessStep;
use Illuminate\Foundation\Http\FormRequest;
class UpdateProcessStepRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'step_name' => ['sometimes', 'required', 'string', 'max:100'],
'is_required' => ['nullable', 'boolean'],
'needs_approval' => ['nullable', 'boolean'],
'needs_inspection' => ['nullable', 'boolean'],
'is_active' => ['nullable', 'boolean'],
'connection_type' => ['nullable', 'string', 'max:20'],
'connection_target' => ['nullable', 'string', 'max:255'],
'completion_type' => ['nullable', 'string', 'max:30'],
];
}
public function attributes(): array
{
return [
'step_name' => '단계명',
'is_required' => '필수여부',
'needs_approval' => '승인필요여부',
'needs_inspection' => '검사필요여부',
'is_active' => '사용여부',
'connection_type' => '연결유형',
'connection_target' => '연결대상',
'completion_type' => '완료유형',
];
}
}

View File

@@ -69,6 +69,14 @@ public function items(): BelongsToMany
->orderByPivot('priority');
}
/**
* 공정 단계
*/
public function steps(): HasMany
{
return $this->hasMany(ProcessStep::class)->orderBy('sort_order');
}
/**
* 작업지시들
*/

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ProcessStep extends Model
{
use HasFactory;
protected $fillable = [
'process_id',
'step_code',
'step_name',
'is_required',
'needs_approval',
'needs_inspection',
'is_active',
'sort_order',
'connection_type',
'connection_target',
'completion_type',
];
protected $casts = [
'is_required' => 'boolean',
'needs_approval' => 'boolean',
'needs_inspection' => 'boolean',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
/**
* 공정
*/
public function process(): BelongsTo
{
return $this->belongsTo(Process::class);
}
}

View File

@@ -25,7 +25,7 @@ public function index(array $params)
$query = Process::query()
->where('tenant_id', $tenantId)
->with(['classificationRules', 'processItems.item:id,code,name']);
->with(['classificationRules', 'processItems.item:id,code,name', 'steps']);
// 검색어
if ($q !== '') {
@@ -62,7 +62,7 @@ public function show(int $id)
$tenantId = $this->tenantId();
$process = Process::where('tenant_id', $tenantId)
->with(['classificationRules', 'processItems.item:id,code,name'])
->with(['classificationRules', 'processItems.item:id,code,name', 'steps'])
->find($id);
if (! $process) {
@@ -104,7 +104,7 @@ public function store(array $data)
// 개별 품목 연결
$this->syncProcessItems($process, $itemIds);
return $process->load(['classificationRules', 'processItems.item:id,code,name']);
return $process->load(['classificationRules', 'processItems.item:id,code,name', 'steps']);
});
}
@@ -145,7 +145,7 @@ public function update(int $id, array $data)
$this->syncProcessItems($process, $itemIds);
}
return $process->fresh(['classificationRules', 'processItems.item:id,code,name']);
return $process->fresh(['classificationRules', 'processItems.item:id,code,name', 'steps']);
});
}
@@ -201,7 +201,7 @@ public function toggleActive(int $id)
'updated_by' => $userId,
]);
return $process->fresh(['classificationRules', 'processItems.item:id,code,name']);
return $process->fresh(['classificationRules', 'processItems.item:id,code,name', 'steps']);
}
/**

View File

@@ -0,0 +1,140 @@
<?php
namespace App\Services;
use App\Models\Process;
use App\Models\ProcessStep;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ProcessStepService extends Service
{
/**
* 공정 단계 목록 조회
*/
public function index(int $processId)
{
$process = $this->findProcess($processId);
return $process->steps()
->orderBy('sort_order')
->get();
}
/**
* 공정 단계 상세 조회
*/
public function show(int $processId, int $stepId)
{
$this->findProcess($processId);
$step = ProcessStep::where('process_id', $processId)->find($stepId);
if (! $step) {
throw new NotFoundHttpException(__('error.not_found'));
}
return $step;
}
/**
* 공정 단계 생성
*/
public function store(int $processId, array $data)
{
$process = $this->findProcess($processId);
$data['process_id'] = $process->id;
$data['step_code'] = $this->generateStepCode($process->id);
$data['sort_order'] = ($process->steps()->max('sort_order') ?? 0) + 1;
$data['is_active'] = $data['is_active'] ?? true;
return ProcessStep::create($data);
}
/**
* 공정 단계 수정
*/
public function update(int $processId, int $stepId, array $data)
{
$this->findProcess($processId);
$step = ProcessStep::where('process_id', $processId)->find($stepId);
if (! $step) {
throw new NotFoundHttpException(__('error.not_found'));
}
$step->update($data);
return $step->fresh();
}
/**
* 공정 단계 삭제
*/
public function destroy(int $processId, int $stepId)
{
$this->findProcess($processId);
$step = ProcessStep::where('process_id', $processId)->find($stepId);
if (! $step) {
throw new NotFoundHttpException(__('error.not_found'));
}
$step->delete();
return true;
}
/**
* 공정 단계 순서 변경
*/
public function reorder(int $processId, array $items)
{
$this->findProcess($processId);
return DB::transaction(function () use ($processId, $items) {
foreach ($items as $item) {
ProcessStep::where('process_id', $processId)
->where('id', $item['id'])
->update(['sort_order' => $item['sort_order']]);
}
return ProcessStep::where('process_id', $processId)
->orderBy('sort_order')
->get();
});
}
/**
* 부모 공정 조회 (tenant scope)
*/
private function findProcess(int $processId): Process
{
$tenantId = $this->tenantId();
$process = Process::where('tenant_id', $tenantId)->find($processId);
if (! $process) {
throw new NotFoundHttpException(__('error.not_found'));
}
return $process;
}
/**
* 단계코드 자동 생성 (STP-001, STP-002, ...)
*/
private function generateStepCode(int $processId): string
{
$lastStep = ProcessStep::where('process_id', $processId)
->orderByRaw('CAST(SUBSTRING(step_code, 5) AS UNSIGNED) DESC')
->first();
if ($lastStep && preg_match('/^STP-(\d+)$/', $lastStep->step_code, $matches)) {
$nextNum = (int) $matches[1] + 1;
} else {
$nextNum = 1;
}
return sprintf('STP-%03d', $nextNum);
}
}