From e8d48035903d2274fe7161aa0b6e195bd33a1afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EA=B3=A4?= Date: Thu, 26 Feb 2026 17:22:50 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20[hr]=20=EC=82=AC=EC=9B=90=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20=EC=8B=9C=20users=20=ED=85=8C=EC=9D=B4=EB=B8=94=20N?= =?UTF-8?q?OT=20NULL=20=EC=A0=9C=EC=95=BD=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - email 미입력 시 임시 이메일 생성 (NOT NULL 제약 대응) - user_id 자동 생성 및 중복 방지 로직 추가 - role 필드 'ops' 기본값 설정 - Hash::make 사용으로 통일 (기존 패턴 준수) --- app/Services/HR/EmployeeService.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/app/Services/HR/EmployeeService.php b/app/Services/HR/EmployeeService.php index 7661b31a..1eb2798a 100644 --- a/app/Services/HR/EmployeeService.php +++ b/app/Services/HR/EmployeeService.php @@ -8,6 +8,8 @@ use App\Models\User; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Str; class EmployeeService { @@ -91,13 +93,34 @@ public function createEmployee(array $data): Employee $tenantId = session('selected_tenant_id'); return DB::transaction(function () use ($data, $tenantId) { + // user_id 생성: 이메일 있으면 @ 앞부분, 없으면 EMP_랜덤6자 + $userId = ! empty($data['email']) + ? Str::before($data['email'], '@') + : 'EMP_'.strtolower(Str::random(6)); + + // user_id 중복 방지 + while (User::where('user_id', $userId)->exists()) { + $userId = $userId.'_'.Str::random(3); + } + + // 이메일: 미입력 시 임시 이메일 생성 (NOT NULL 제약) + $email = ! empty($data['email']) + ? $data['email'] + : $userId.'@placeholder.local'; + + // email 중복 방지 + while (User::where('email', $email)->exists()) { + $email = $userId.'_'.Str::random(3).'@placeholder.local'; + } + // User 생성 $user = User::create([ - 'user_id' => $data['email'] ?? $data['name'], + 'user_id' => $userId, 'name' => $data['name'], - 'email' => $data['email'] ?? null, + 'email' => $email, 'phone' => $data['phone'] ?? null, - 'password' => bcrypt($data['password'] ?? 'sam1234!'), + 'password' => Hash::make($data['password'] ?? 'sam1234!'), + 'role' => 'ops', 'is_active' => true, 'must_change_password' => true, 'created_by' => auth()->id(),