From fdc242b076de4c6e789548206148da2a805f67ca Mon Sep 17 00:00:00 2001 From: Kenan KARAER Date: Wed, 3 Dec 2025 22:36:31 +0300 Subject: [PATCH] Fix: Remove duplicated code in reservation actions --- src/app/dashboard/reservations/new/actions.ts | 86 +++++++------------ 1 file changed, 29 insertions(+), 57 deletions(-) diff --git a/src/app/dashboard/reservations/new/actions.ts b/src/app/dashboard/reservations/new/actions.ts index 6750ef0..e320ef8 100644 --- a/src/app/dashboard/reservations/new/actions.ts +++ b/src/app/dashboard/reservations/new/actions.ts @@ -3,6 +3,7 @@ import { createClient } from "@/lib/supabase/server" import { revalidatePath } from "next/cache" import { redirect } from "next/navigation" +import { logAction } from "@/lib/logger" export async function createReservation(data: { hall_id: string @@ -26,62 +27,33 @@ export async function createReservation(data: { if (conflictError) { throw new Error("Müsaitlik kontrolü yapılırken hata oluştu: " + conflictError.message) } - 'use server' - import { createClient } from "@/lib/supabase/server" - import { revalidatePath } from "next/cache" - 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') + 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') +}