"use server"; import { createAdminClient } from "@/lib/supabase/server"; import { auth } from "@clerk/nextjs/server"; async function getOrganisationId(): Promise { const { userId } = await auth(); if (!userId) throw new Error("Unauthenticated"); const supabase = await createAdminClient(); const { data: member, error } = await supabase .from("organisation_members") .select("organisation_id") .eq("user_id", userId) .single(); if (error || !member) throw new Error("No organisation found"); return member.organisation_id; } export interface Customer { id: string; name: string; email: string | null; phone: string | null; company: string | null; address: string | null; notes: string | null; created_at: string; } export async function fetchCustomers(search?: string): Promise { const orgId = await getOrganisationId(); const supabase = await createAdminClient(); let query = supabase .from("customers") .select("*") .eq("organisation_id", orgId) .order("name", { ascending: true }); if (search) { query = query.ilike("name", `%${search}%`); } const { data, error } = await query; if (error) throw error; return (data || []) as Customer[]; } export async function createCustomer(input: { name: string; email?: string; phone?: string; company?: string; address?: string; notes?: string; }): Promise<{ success: boolean; customerId?: string; error?: string }> { const orgId = await getOrganisationId(); const supabase = await createAdminClient(); const { data, error } = await supabase .from("customers") .insert({ organisation_id: orgId, name: input.name, email: input.email || null, phone: input.phone || null, company: input.company || null, address: input.address || null, notes: input.notes || null, }) .select("id") .single(); if (error) return { success: false, error: error.message }; return { success: true, customerId: data.id }; } export async function updateCustomer( id: string, input: { name?: string; email?: string; phone?: string; company?: string; address?: string; notes?: string; } ): Promise<{ success: boolean; error?: string }> { const orgId = await getOrganisationId(); const supabase = await createAdminClient(); const { error } = await supabase .from("customers") .update(input) .eq("id", id) .eq("organisation_id", orgId); if (error) return { success: false, error: error.message }; return { success: true }; } export async function deleteCustomer(id: string): Promise<{ success: boolean; error?: string }> { const { userId } = await auth(); if (!userId) return { success: false, error: "Unauthenticated" }; const orgId = await getOrganisationId(); const supabase = await createAdminClient(); // Check role — only owners/admins can delete const { data: member } = await supabase .from("organisation_members") .select("role") .eq("user_id", userId) .eq("organisation_id", orgId) .single(); if (member?.role !== "owner" && member?.role !== "admin") { return { success: false, error: "Only owners and admins can delete customers" }; } const { error } = await supabase .from("customers") .delete() .eq("id", id) .eq("organisation_id", orgId); if (error) return { success: false, error: error.message }; return { success: true }; } export async function getUserRole(): Promise { const { userId } = await auth(); if (!userId) return "employee"; const orgId = await getOrganisationId(); const supabase = await createAdminClient(); const { data } = await supabase .from("organisation_members") .select("role") .eq("organisation_id", orgId) .eq("user_id", userId) .single(); return data?.role || "employee"; } export async function getCurrentUserEmail(): Promise { const { userId } = await auth(); if (!userId) return null; const supabase = await createAdminClient(); const { data } = await supabase .from("profiles") .select("email") .eq("id", userId) .single(); return data?.email || null; }