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(),