Fix: Remove duplicated code in reservation actions
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
import { createClient } from "@/lib/supabase/server"
|
import { createClient } from "@/lib/supabase/server"
|
||||||
import { revalidatePath } from "next/cache"
|
import { revalidatePath } from "next/cache"
|
||||||
import { redirect } from "next/navigation"
|
import { redirect } from "next/navigation"
|
||||||
|
import { logAction } from "@/lib/logger"
|
||||||
|
|
||||||
export async function createReservation(data: {
|
export async function createReservation(data: {
|
||||||
hall_id: string
|
hall_id: string
|
||||||
@@ -26,62 +27,33 @@ export async function createReservation(data: {
|
|||||||
if (conflictError) {
|
if (conflictError) {
|
||||||
throw new Error("Müsaitlik kontrolü yapılırken hata oluştu: " + conflictError.message)
|
throw new Error("Müsaitlik kontrolü yapılırken hata oluştu: " + conflictError.message)
|
||||||
}
|
}
|
||||||
'use server'
|
|
||||||
|
|
||||||
import { createClient } from "@/lib/supabase/server"
|
if (conflicts && conflicts.length > 0) {
|
||||||
import { revalidatePath } from "next/cache"
|
return { error: "Seçilen tarih ve saatte bu salon zaten dolu." }
|
||||||
import { redirect } from "next/navigation"
|
|
||||||
import { logAction } from "@/lib/logger"
|
|
||||||
|
|
||||||
export async function createReservation(data: {
|
|
||||||
hall_id: string
|
|
||||||
customer_id: string
|
|
||||||
package_id?: string
|
|
||||||
start_time: string
|
|
||||||
end_time: string
|
|
||||||
notes?: string
|
|
||||||
}) {
|
|
||||||
const supabase = await createClient()
|
|
||||||
|
|
||||||
// 1. Check for conflicts
|
|
||||||
// We look for any reservation in the same hall that overlaps with the requested time range
|
|
||||||
const { data: conflicts, error: conflictError } = await supabase
|
|
||||||
.from('reservations')
|
|
||||||
.select('id')
|
|
||||||
.eq('hall_id', data.hall_id)
|
|
||||||
.neq('status', 'cancelled') // Ignore cancelled bookings
|
|
||||||
.or(`and(start_time.lte.${data.end_time},end_time.gte.${data.start_time})`)
|
|
||||||
|
|
||||||
if (conflictError) {
|
|
||||||
throw new Error("Müsaitlik kontrolü yapılırken hata oluştu: " + conflictError.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conflicts && conflicts.length > 0) {
|
|
||||||
return { error: "Seçilen tarih ve saatte bu salon zaten dolu." }
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Create Reservation
|
|
||||||
const { data: newReservation, error } = await supabase.from('reservations').insert({
|
|
||||||
hall_id: data.hall_id,
|
|
||||||
customer_id: data.customer_id,
|
|
||||||
package_id: data.package_id || null,
|
|
||||||
start_time: data.start_time,
|
|
||||||
end_time: data.end_time,
|
|
||||||
status: 'pending', // Default to pending, admin must confirm
|
|
||||||
notes: data.notes,
|
|
||||||
}).select().single()
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
return { error: error.message }
|
|
||||||
}
|
|
||||||
|
|
||||||
await logAction('create_reservation', 'reservation', newReservation.id, {
|
|
||||||
hall_id: data.hall_id,
|
|
||||||
customer_id: data.customer_id,
|
|
||||||
start_time: data.start_time
|
|
||||||
})
|
|
||||||
|
|
||||||
revalidatePath('/dashboard/reservations')
|
|
||||||
revalidatePath('/dashboard/calendar')
|
|
||||||
redirect('/dashboard/reservations')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. Create Reservation
|
||||||
|
const { data: newReservation, error } = await supabase.from('reservations').insert({
|
||||||
|
hall_id: data.hall_id,
|
||||||
|
customer_id: data.customer_id,
|
||||||
|
package_id: data.package_id || null,
|
||||||
|
start_time: data.start_time,
|
||||||
|
end_time: data.end_time,
|
||||||
|
status: 'pending', // Default to pending, admin must confirm
|
||||||
|
notes: data.notes,
|
||||||
|
}).select().single()
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return { error: error.message }
|
||||||
|
}
|
||||||
|
|
||||||
|
await logAction('create_reservation', 'reservation', newReservation.id, {
|
||||||
|
hall_id: data.hall_id,
|
||||||
|
customer_id: data.customer_id,
|
||||||
|
start_time: data.start_time
|
||||||
|
})
|
||||||
|
|
||||||
|
revalidatePath('/dashboard/reservations')
|
||||||
|
revalidatePath('/dashboard/calendar')
|
||||||
|
redirect('/dashboard/reservations')
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user