From dd0f79b947928819b6d8a0025cb5cac515857790 Mon Sep 17 00:00:00 2001 From: kent Date: Fri, 26 Dec 2025 15:00:57 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=B6=80=EC=84=9C=20=ED=8A=B8=EB=A6=AC?= =?UTF-8?q?=20API=20=ED=85=8C=EB=84=8C=ED=8A=B8=20=ED=95=84=ED=84=B0?= =?UTF-8?q?=EB=A7=81=20=EB=B0=8F=20=EC=82=AC=EC=9B=90=20=EC=97=B0=EB=8F=99?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - DepartmentUser: Model → Pivot 상속 변경 (fromRawAttributes 에러 수정) - Department: BelongsToTenant 트레이트 추가 (테넌트 필터링) - Department.employees(): tenant_user_profiles 기반 사원 조회 관계 추가 - DepartmentService.tree(): department_user 대신 employees 사용 - users 형태로 변환하여 프론트엔드 호환성 유지 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app/Models/Tenants/Department.php | 14 +++++++- app/Models/Tenants/Pivots/DepartmentUser.php | 4 +-- app/Services/DepartmentService.php | 38 ++++++++++++++++++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/Models/Tenants/Department.php b/app/Models/Tenants/Department.php index 13a8191..6e3ecba 100644 --- a/app/Models/Tenants/Department.php +++ b/app/Models/Tenants/Department.php @@ -5,16 +5,18 @@ use App\Models\Members\User; use App\Models\Permissions\PermissionOverride; use App\Models\Tenants\Pivots\DepartmentUser; +use App\Traits\BelongsToTenant; use App\Traits\ModelTrait; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; use Spatie\Permission\Traits\HasRoles; class Department extends Model { - use HasRoles, ModelTrait, SoftDeletes; // 부서도 권한/역할을 가짐 + use BelongsToTenant, HasRoles, ModelTrait, SoftDeletes; // 부서도 권한/역할을 가짐 protected $table = 'departments'; @@ -50,6 +52,7 @@ public function users() { return $this->belongsToMany(User::class, 'department_user') ->using(DepartmentUser::class) + ->wherePivotNull('deleted_at') ->withTimestamps() ->withPivot(['tenant_id', 'is_primary', 'joined_at', 'left_at']); } @@ -65,4 +68,13 @@ public function departmentUsers() { return $this->hasMany(DepartmentUser::class, 'department_id'); } + + /** 부서 소속 사원 (tenant_user_profiles 기반) */ + public function employees(): HasMany + { + // Department가 이미 BelongsToTenant로 필터링되므로 + // department_id 연결만으로 테넌트 격리됨 + return $this->hasMany(TenantUserProfile::class, 'department_id') + ->where('employee_status', 'active'); + } } diff --git a/app/Models/Tenants/Pivots/DepartmentUser.php b/app/Models/Tenants/Pivots/DepartmentUser.php index f872a3e..94b0189 100644 --- a/app/Models/Tenants/Pivots/DepartmentUser.php +++ b/app/Models/Tenants/Pivots/DepartmentUser.php @@ -7,13 +7,13 @@ use App\Models\Tenants\Department; use App\Traits\BelongsToTenant; use App\Traits\ModelTrait; -use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Database\Eloquent\SoftDeletes; /** * @mixin IdeHelperDepartmentUser */ -class DepartmentUser extends Model +class DepartmentUser extends Pivot { use BelongsToTenant, ModelTrait, SoftDeletes; diff --git a/app/Services/DepartmentService.php b/app/Services/DepartmentService.php index fbe26ef..920a550 100644 --- a/app/Services/DepartmentService.php +++ b/app/Services/DepartmentService.php @@ -84,11 +84,19 @@ public function tree(array $params = []): array $this->loadChildrenRecursive($q, $withUsers); }]); + // employees 관계 사용 (tenant_user_profiles 기반) if ($withUsers) { - $query->with(['users:id,name,email']); + $query->with(['employees.user:id,name,email']); } - return $query->get()->toArray(); + $result = $query->get(); + + // users 형태로 변환 (프론트엔드 호환성) + if ($withUsers) { + $result = $this->transformEmployeesToUsers($result); + } + + return $result->toArray(); } /** 재귀적으로 자식 부서 로드 */ @@ -99,11 +107,35 @@ private function loadChildrenRecursive($query, bool $withUsers): void $this->loadChildrenRecursive($q, $withUsers); }]); + // employees 관계 사용 (tenant_user_profiles 기반) if ($withUsers) { - $query->with(['users:id,name,email']); + $query->with(['employees.user:id,name,email']); } } + /** employees를 users 형태로 변환 (재귀) */ + private function transformEmployeesToUsers($departments) + { + return $departments->map(function ($dept) { + // employees → users 변환 + if ($dept->relationLoaded('employees')) { + $users = $dept->employees->map(function ($profile) { + return $profile->user; + })->filter(); // null 제거 + + $dept->setRelation('users', $users); + $dept->unsetRelation('employees'); + } + + // 자식 부서도 재귀 처리 + if ($dept->relationLoaded('children') && $dept->children->count() > 0) { + $dept->setRelation('children', $this->transformEmployeesToUsers($dept->children)); + } + + return $dept; + }); + } + /** 생성 */ public function store(array $params) {