fix: [hr] 사원 등록 시 users 테이블 NOT NULL 제약 오류 수정

- email 미입력 시 임시 이메일 생성 (NOT NULL 제약 대응)
- user_id 자동 생성 및 중복 방지 로직 추가
- role 필드 'ops' 기본값 설정
- Hash::make 사용으로 통일 (기존 패턴 준수)
This commit is contained in:
김보곤
2026-02-26 17:22:50 +09:00
parent 8d0dee2bb2
commit e8d4803590

View File

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