Feat: Implement audit logging for Customer actions
This commit is contained in:
51
src/app/dashboard/customers/[id]/actions.ts
Normal file
51
src/app/dashboard/customers/[id]/actions.ts
Normal file
@@ -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')
|
||||
}
|
||||
@@ -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')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user