Feat: Implement audit logging for Customer actions

This commit is contained in:
2025-12-03 22:53:32 +03:00
parent b8b4b702a2
commit efeff06432
2 changed files with 60 additions and 2 deletions

View 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')
}

View File

@@ -3,6 +3,8 @@
import { createClient } from "@/lib/supabase/server" import { createClient } from "@/lib/supabase/server"
import { revalidatePath } from "next/cache" import { revalidatePath } from "next/cache"
import { logAction } from "@/lib/logger"
export async function createCustomer(data: { export async function createCustomer(data: {
full_name: string; full_name: string;
phone?: string; phone?: string;
@@ -14,7 +16,7 @@ export async function createCustomer(data: {
}) { }) {
const supabase = await createClient() 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, full_name: data.full_name,
phone: data.phone, phone: data.phone,
email: data.email || null, email: data.email || null,
@@ -22,11 +24,16 @@ export async function createCustomer(data: {
district: data.district, district: data.district,
address: data.address, address: data.address,
notes: data.notes, notes: data.notes,
}) }).select().single()
if (error) { if (error) {
throw new Error(error.message) throw new Error(error.message)
} }
await logAction('create_customer', 'customer', newCustomer.id, {
full_name: data.full_name,
phone: data.phone
})
revalidatePath('/dashboard/customers') revalidatePath('/dashboard/customers')
} }