66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
'use server'
|
||
|
||
import { createClient } from '@/utils/supabase/server'
|
||
import { revalidatePath } from 'next/cache'
|
||
|
||
export async function submitLeaveRequest(formData: FormData) {
|
||
const supabase = await createClient()
|
||
|
||
const { data: { user } } = await supabase.auth.getUser()
|
||
if (!user) return { error: 'Oturum bulunamadı.' }
|
||
|
||
const startDate = formData.get('start_date') as string
|
||
const endDate = formData.get('end_date') as string
|
||
const reason = formData.get('reason') as string
|
||
const companyId = formData.get('company_id') as string // which company they are requesting leave for
|
||
|
||
if (!startDate || !endDate || !companyId || !reason) {
|
||
return { error: 'Tüm alanları doldurunuz.' }
|
||
}
|
||
|
||
// Find their employee record for this specific company
|
||
const { data: employeeData, error: empError } = await supabase
|
||
.from('employees')
|
||
.select('id')
|
||
.eq('user_id', user.id)
|
||
.eq('company_id', companyId)
|
||
.single()
|
||
|
||
if (empError || !employeeData) {
|
||
return { error: 'Seçili şirket için personel kaydınız bulunamadı.' }
|
||
}
|
||
|
||
const { error } = await supabase
|
||
.from('leave_requests')
|
||
.insert([{
|
||
employee_id: employeeData.id,
|
||
start_date: startDate,
|
||
end_date: endDate,
|
||
reason: reason,
|
||
status: 'pending' // default
|
||
}])
|
||
|
||
if (error) {
|
||
return { error: 'İzin talebi oluşturulurken hata: ' + error.message }
|
||
}
|
||
|
||
revalidatePath('/leave-requests')
|
||
return { success: true }
|
||
}
|
||
|
||
export async function updateLeaveStatus(id: string, newStatus: 'approved' | 'rejected') {
|
||
const supabase = await createClient()
|
||
|
||
const { error } = await supabase
|
||
.from('leave_requests')
|
||
.update({ status: newStatus })
|
||
.eq('id', id)
|
||
|
||
if (error) {
|
||
return { error: 'Durum güncellenirken hata oluştu: ' + error.message }
|
||
}
|
||
|
||
revalidatePath('/leave-requests')
|
||
return { success: true }
|
||
}
|