diff --git a/src/app/dashboard/customers/[id]/actions.ts b/src/app/dashboard/customers/[id]/actions.ts new file mode 100644 index 0000000..ed7470b --- /dev/null +++ b/src/app/dashboard/customers/[id]/actions.ts @@ -0,0 +1,51 @@ +'use server' + +import { createClient } from "@/lib/supabase/server" +import { revalidatePath } from "next/cache" +import { logAction } from "@/lib/logger" + +export async function deleteCustomer(id: string) { + const supabase = await createClient() + + const { error } = await supabase + .from('customers') + .delete() + .eq('id', id) + + if (error) throw new Error(error.message) + + await logAction('delete_customer', 'customer', id) + revalidatePath('/dashboard/customers') +} + +export async function updateCustomer(id: string, data: { + full_name: string; + phone?: string; + email?: string; + city?: string; + district?: string; + address?: string; + notes?: string +}) { + const supabase = await createClient() + + const { error } = await supabase + .from('customers') + .update({ + full_name: data.full_name, + phone: data.phone, + email: data.email || null, + city: data.city, + district: data.district, + address: data.address, + notes: data.notes, + }) + .eq('id', id) + + if (error) throw new Error(error.message) + + await logAction('update_customer', 'customer', id, { + full_name: data.full_name + }) + revalidatePath('/dashboard/customers') +} diff --git a/src/app/dashboard/customers/new/actions.ts b/src/app/dashboard/customers/new/actions.ts index 51bb0ed..436dfa0 100644 --- a/src/app/dashboard/customers/new/actions.ts +++ b/src/app/dashboard/customers/new/actions.ts @@ -3,6 +3,8 @@ import { createClient } from "@/lib/supabase/server" import { revalidatePath } from "next/cache" +import { logAction } from "@/lib/logger" + export async function createCustomer(data: { full_name: string; phone?: string; @@ -14,7 +16,7 @@ export async function createCustomer(data: { }) { const supabase = await createClient() - const { error } = await supabase.from('customers').insert({ + const { data: newCustomer, error } = await supabase.from('customers').insert({ full_name: data.full_name, phone: data.phone, email: data.email || null, @@ -22,11 +24,16 @@ export async function createCustomer(data: { district: data.district, address: data.address, notes: data.notes, - }) + }).select().single() if (error) { throw new Error(error.message) } + await logAction('create_customer', 'customer', newCustomer.id, { + full_name: data.full_name, + phone: data.phone + }) + revalidatePath('/dashboard/customers') }