"use server"; import { createAdminClient } from "@/lib/supabase/server"; import { auth } from "@clerk/nextjs/server"; async function getOrgAndUserId() { const { userId } = await auth(); if (!userId) throw new Error("Unauthenticated"); const supabase = await createAdminClient(); const { data } = await supabase .from("organisation_members") .select("organisation_id") .eq("user_id", userId) .single(); if (!data) throw new Error("No org found"); return { orgId: data.organisation_id, userId }; } export async function fetchAssets(category?: string, status?: string): Promise { const { orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); let q = supabase .from("assets") .select("*, assignee:employees(first_name, last_name)") .eq("organisation_id", orgId); if (category) q = q.eq("category", category); if (status) q = q.eq("status", status); const { data } = await q.order("created_at", { ascending: false }); return data || []; } export async function createAsset(input: { name: string; category: string; serial_number?: string; purchase_date?: string; purchase_value?: number; notes?: string; }): Promise<{ success: boolean; error?: string }> { const { orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { error } = await supabase .from("assets") .insert({ ...input, organisation_id: orgId }); if (error) return { success: false, error: error.message }; return { success: true }; } export async function assignAsset( id: string, employeeId: string ): Promise<{ success: boolean; error?: string }> { const supabase = await createAdminClient(); const { error } = await supabase .from("assets") .update({ assigned_to: employeeId, assigned_at: new Date().toISOString(), status: "assigned", }) .eq("id", id); if (error) return { success: false, error: error.message }; return { success: true }; } export async function returnAsset( id: string ): Promise<{ success: boolean; error?: string }> { const supabase = await createAdminClient(); const { error } = await supabase .from("assets") .update({ assigned_to: null, assigned_at: null, status: "available", }) .eq("id", id); if (error) return { success: false, error: error.message }; return { success: true }; } export async function fetchEmployeeAssets(employeeId: string): Promise { const { orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { data } = await supabase .from("assets") .select("*") .eq("organisation_id", orgId) .eq("assigned_to", employeeId) .order("assigned_at", { ascending: false }); return data || []; } export async function getAssetCounts(): Promise<{ total: number; assigned: number; available: number; maintenance: number; retired: number; }> { const { orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { data } = await supabase .from("assets") .select("status") .eq("organisation_id", orgId); const counts = { total: data?.length || 0, assigned: 0, available: 0, maintenance: 0, retired: 0, }; (data || []).forEach((a: any) => { if (counts[a.status as keyof typeof counts] !== undefined) counts[a.status as keyof typeof counts]++; }); return counts; } export async function fetchActiveEmployees(): Promise { const { orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { data } = await supabase .from("employees") .select("id, first_name, last_name, email") .eq("organisation_id", orgId) .eq("status", "active") .order("first_name"); return data || []; }