89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
'use server'
|
|
|
|
import { createClient } from "@/lib/supabase-server"
|
|
import { Customer, CustomerInsert, CustomerUpdate } from "@/types/customer"
|
|
import { revalidatePath } from "next/cache"
|
|
|
|
// Get all customers
|
|
export async function getCustomers() {
|
|
const supabase = createClient()
|
|
const { data, error } = await supabase
|
|
.from('customers')
|
|
.select('*')
|
|
.order('created_at', { ascending: false })
|
|
|
|
if (error) {
|
|
console.error('Error fetching customers:', error)
|
|
return { success: false, error: error.message }
|
|
}
|
|
|
|
return { success: true, data: data as Customer[] }
|
|
}
|
|
|
|
// Get customer by ID
|
|
export async function getCustomerById(id: number) {
|
|
const supabase = createClient()
|
|
const { data, error } = await supabase
|
|
.from('customers')
|
|
.select('*')
|
|
.eq('id', id)
|
|
.single()
|
|
|
|
if (error) {
|
|
return { success: false, error: error.message }
|
|
}
|
|
|
|
return { success: true, data: data as Customer }
|
|
}
|
|
|
|
// Add new customer
|
|
export async function addCustomer(customer: CustomerInsert) {
|
|
const supabase = createClient()
|
|
const { data, error } = await supabase
|
|
.from('customers')
|
|
.insert(customer)
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
return { success: false, error: error.message }
|
|
}
|
|
|
|
revalidatePath('/dashboard/customers')
|
|
return { success: true, data: data as Customer }
|
|
}
|
|
|
|
// Update existing customer
|
|
export async function updateCustomer(id: number, customer: CustomerUpdate) {
|
|
const supabase = createClient()
|
|
const { data, error } = await supabase
|
|
.from('customers')
|
|
.update({ ...customer, updated_at: new Date().toISOString() })
|
|
.eq('id', id)
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
return { success: false, error: error.message }
|
|
}
|
|
|
|
revalidatePath('/dashboard/customers')
|
|
return { success: true, data: data as Customer }
|
|
}
|
|
|
|
// Delete customer
|
|
export async function deleteCustomer(id: number) {
|
|
const supabase = createClient()
|
|
const { error } = await supabase
|
|
.from('customers')
|
|
.delete()
|
|
.eq('id', id)
|
|
|
|
if (error) {
|
|
return { success: false, error: error.message }
|
|
}
|
|
|
|
revalidatePath('/dashboard/customers')
|
|
return { success: true }
|
|
}
|