'use server' import { createClient } from '@/utils/supabase/server' import { createAdminClient } from '@/utils/supabase/admin' import { revalidatePath } from 'next/cache' export async function addEmployee(formData: FormData) { const supabase = await createClient() // 1. Get Form Data const firstName = formData.get('first_name') as string const lastName = formData.get('last_name') as string const email = formData.get('email') as string const companyId = formData.get('company_id') as string const roleId = formData.get('role_id') as string const photo = formData.get('photo') as File if (!firstName || !lastName || !companyId || !roleId) { return { error: 'Ad, Soyad, Şirket ve Rol seçimi zorunludur.' } } // 2. Handle Photo Upload let photoUrl = null if (photo && photo.size > 0) { const fileExt = photo.name.split('.').pop() const fileName = `${Math.random()}.${fileExt}` const filePath = `${fileName}` const { error: uploadError, data } = await supabase.storage .from('employee-photos') .upload(filePath, photo) if (uploadError) { console.error('Photo upload error:', uploadError) } else { const { data: { publicUrl } } = supabase.storage .from('employee-photos') .getPublicUrl(filePath) photoUrl = publicUrl } } // 3. Insert into Employees table const { error: employeeError } = await supabase .from('employees') .insert([{ first_name: firstName, last_name: lastName, email: email || null, company_id: companyId, role_id: roleId, photo_url: photoUrl, department_id: formData.get('department_id') || null, section_id: formData.get('section_id') || null, employment_type_id: formData.get('employment_type_id') || null, job_title_id: formData.get('job_title_id') || null, tc_no: formData.get('tc_no'), birth_date: formData.get('birth_date') || null, birth_place: formData.get('birth_place'), gender: formData.get('gender'), address: formData.get('address'), phone1: formData.get('phone1'), phone2: formData.get('phone2'), has_driving_license: formData.get('has_driving_license') === 'on', military_status: formData.get('military_status'), start_date: formData.get('start_date') || null, leave_date: formData.get('leave_date') || null, status: 'active' }]) if (employeeError) { return { error: 'Personel eklenirken hata: ' + employeeError.message } } revalidatePath('/employees') return { success: true } } export async function deleteEmployee(id: string) { const supabase = await createClient() const { error } = await supabase .from('employees') .delete() .eq('id', id) if (error) { return { error: 'Personel silinirken hata oluştu: ' + error.message } } revalidatePath('/employees') return { success: true } } export async function updateEmployee(id: string, formData: FormData) { const supabase = await createClient() const photo = formData.get('photo') as File // 1. Handle Photo Upload if new photo provided let photoUrl = formData.get('existing_photo_url') as string if (photo && photo.size > 0) { const fileExt = photo.name.split('.').pop() const fileName = `${Math.random()}.${fileExt}` const filePath = `${fileName}` const { error: uploadError } = await supabase.storage .from('employee-photos') .upload(filePath, photo) if (!uploadError) { const { data: { publicUrl } } = supabase.storage .from('employee-photos') .getPublicUrl(filePath) photoUrl = publicUrl } } const { error } = await supabase .from('employees') .update({ first_name: formData.get('first_name'), last_name: formData.get('last_name'), email: formData.get('email'), company_id: formData.get('company_id'), role_id: formData.get('role_id'), photo_url: photoUrl, department_id: formData.get('department_id') || null, section_id: formData.get('section_id') || null, employment_type_id: formData.get('employment_type_id') || null, job_title_id: formData.get('job_title_id') || null, tc_no: formData.get('tc_no'), birth_date: formData.get('birth_date') || null, birth_place: formData.get('birth_place'), gender: formData.get('gender'), address: formData.get('address'), phone1: formData.get('phone1'), phone2: formData.get('phone2'), has_driving_license: formData.get('has_driving_license') === 'on', military_status: formData.get('military_status'), start_date: formData.get('start_date') || null, leave_date: formData.get('leave_date') || null, status: formData.get('leave_date') ? 'inactive' : formData.get('status') }) .eq('id', id) if (error) { return { error: 'Personel güncellenirken hata oluştu: ' + error.message } } revalidatePath('/employees') return { success: true } } export async function createUserForEmployee(formData: FormData) { const supabaseAdmin = createAdminClient() const supabase = await createClient() const employeeId = formData.get('employee_id') as string const password = formData.get('password') as string if (!employeeId || !password) { return { error: 'Personel ve şifre zorunludur.' } } // 1. Get employee email const { data: employee, error: fetchError } = await supabase .from('employees') .select('email, first_name, last_name') .eq('id', employeeId) .single() if (fetchError || !employee || !employee.email) { return { error: 'Personel bilgisi veya e-posta adresi bulunamadı.' } } // 2. Create user in Auth const { data: authData, error: authError } = await supabaseAdmin.auth.admin.createUser({ email: employee.email, password: password, email_confirm: true, user_metadata: { first_name: employee.first_name, last_name: employee.last_name } }) if (authError) { return { error: 'Kullanıcı oluşturulurken hata (Auth): ' + authError.message } } // 3. Link Auth User to Employee const { error: updateError } = await supabase .from('employees') .update({ user_id: authData.user.id }) .eq('id', employeeId) if (updateError) { return { error: 'Kullanıcı personele bağlanırken hata: ' + updateError.message } } revalidatePath('/employees') return { success: true } }