refactor(work-orders): 코드 리뷰 기반 전면 개선
## Critical 수정 - Multi-tenancy: WorkOrderItem, BendingDetail, Issue에 BelongsToTenant 적용 - 감사 로그: 상태변경, 품목수정, 이슈 등록/해결 시 로깅 추가 - 상태 전이 규칙: STATUS_TRANSITIONS + canTransitionTo() 구현 ## High 수정 - 다중 담당자: work_order_assignees 피벗 테이블 및 관계 추가 - 부분 수정: 품목 ID 기반 upsert/delete 로직 구현 ## 변경 파일 - Models: WorkOrder, WorkOrderAssignee(신규), 하위 모델들 - Services: WorkOrderService (assign, update 메서드 개선) - Migrations: tenant_id 추가, assignees 테이블 생성 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* 작업지시 하위 테이블에 tenant_id 컬럼 추가
|
||||
* - work_order_items
|
||||
* - work_order_bending_details
|
||||
* - work_order_issues
|
||||
*
|
||||
* 기존 데이터는 work_orders 테이블의 tenant_id를 참조하여 업데이트
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// 1. work_order_items
|
||||
Schema::table('work_order_items', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('tenant_id')->nullable()->after('id')->comment('테넌트ID');
|
||||
$table->index('tenant_id', 'idx_work_order_items_tenant');
|
||||
});
|
||||
|
||||
// 기존 데이터 업데이트
|
||||
DB::statement('
|
||||
UPDATE work_order_items wi
|
||||
JOIN work_orders wo ON wi.work_order_id = wo.id
|
||||
SET wi.tenant_id = wo.tenant_id
|
||||
');
|
||||
|
||||
// nullable 제거
|
||||
Schema::table('work_order_items', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('tenant_id')->nullable(false)->change();
|
||||
});
|
||||
|
||||
// 2. work_order_bending_details
|
||||
Schema::table('work_order_bending_details', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('tenant_id')->nullable()->after('id')->comment('테넌트ID');
|
||||
$table->index('tenant_id', 'idx_work_order_bending_details_tenant');
|
||||
});
|
||||
|
||||
DB::statement('
|
||||
UPDATE work_order_bending_details wbd
|
||||
JOIN work_orders wo ON wbd.work_order_id = wo.id
|
||||
SET wbd.tenant_id = wo.tenant_id
|
||||
');
|
||||
|
||||
Schema::table('work_order_bending_details', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('tenant_id')->nullable(false)->change();
|
||||
});
|
||||
|
||||
// 3. work_order_issues
|
||||
Schema::table('work_order_issues', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('tenant_id')->nullable()->after('id')->comment('테넌트ID');
|
||||
$table->index('tenant_id', 'idx_work_order_issues_tenant');
|
||||
});
|
||||
|
||||
DB::statement('
|
||||
UPDATE work_order_issues woi
|
||||
JOIN work_orders wo ON woi.work_order_id = wo.id
|
||||
SET woi.tenant_id = wo.tenant_id
|
||||
');
|
||||
|
||||
Schema::table('work_order_issues', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('tenant_id')->nullable(false)->change();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('work_order_items', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_work_order_items_tenant');
|
||||
$table->dropColumn('tenant_id');
|
||||
});
|
||||
|
||||
Schema::table('work_order_bending_details', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_work_order_bending_details_tenant');
|
||||
$table->dropColumn('tenant_id');
|
||||
});
|
||||
|
||||
Schema::table('work_order_issues', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_work_order_issues_tenant');
|
||||
$table->dropColumn('tenant_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* 작업지시 담당자 피벗 테이블 (Work Order Assignees)
|
||||
* - 다중 담당자 지원
|
||||
* - 주 담당자 구분 (is_primary)
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('work_order_assignees', function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->unsignedBigInteger('tenant_id')->comment('테넌트ID');
|
||||
$table->unsignedBigInteger('work_order_id')->comment('작업지시ID');
|
||||
$table->unsignedBigInteger('user_id')->comment('담당자ID');
|
||||
$table->boolean('is_primary')->default(false)->comment('주담당자 여부');
|
||||
$table->timestamps();
|
||||
|
||||
// Indexes
|
||||
$table->unique(['work_order_id', 'user_id'], 'uq_work_order_assignees');
|
||||
$table->index(['tenant_id', 'work_order_id'], 'idx_wo_assignees_tenant_wo');
|
||||
$table->index(['tenant_id', 'user_id'], 'idx_wo_assignees_tenant_user');
|
||||
|
||||
// Foreign keys
|
||||
$table->foreign('work_order_id')
|
||||
->references('id')
|
||||
->on('work_orders')
|
||||
->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('work_order_assignees');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user