"use server"; import { createAdminClient } from "@/lib/supabase/server"; import { auth } from "@clerk/nextjs/server"; async function getOrgAndUserId(): Promise<{ orgId: string; userId: string }> { const { userId } = await auth(); if (!userId) throw new Error("Unauthenticated"); const supabase = await createAdminClient(); const { data } = await supabase .from("organisation_members") .select("organisation_id, role") .eq("user_id", userId) .single(); if (!data) throw new Error("No org found"); return { orgId: data.organisation_id, userId }; } // ── Projects ────────────────────────────────────────────────── export interface ProjectRow { id: string; name: string; description: string | null; status: string; created_at: string; created_by: string; organisation_id: string; tasks?: { count: number }[]; creator?: { full_name: string } | null; } export async function fetchProjects(): Promise { const { orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { data } = await supabase .from("projects") .select("*, tasks:tasks(count), creator:profiles(full_name)") .eq("organisation_id", orgId) .order("created_at", { ascending: false }); return data || []; } export async function createProject( name: string, description?: string ): Promise<{ success: boolean; id?: string; error?: string }> { const { orgId, userId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { data, error } = await supabase .from("projects") .insert({ organisation_id: orgId, name, description: description || null, created_by: userId, status: "active", }) .select("id") .single(); if (error) return { success: false, error: error.message }; return { success: true, id: data.id }; } // ── Tasks ───────────────────────────────────────────────────── export interface TaskRow { id: string; title: string; description: string | null; status: string; priority: string; due_date: string | null; assigned_to: string | null; project_id: string; organisation_id: string; created_by: string; created_at: string; assignee?: { first_name: string; last_name: string } | null; project?: { name: string } | null; } export async function fetchProjectTasks(projectId: string): Promise { const { orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { data } = await supabase .from("tasks") .select("*, assignee:employees(first_name, last_name)") .eq("organisation_id", orgId) .eq("project_id", projectId) .order("created_at", { ascending: false }); return data || []; } export async function createTask(input: { project_id: string; title: string; description?: string; assigned_to?: string; due_date?: string; priority: string; status: string; }): Promise<{ success: boolean; error?: string }> { const { orgId, userId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { error } = await supabase.from("tasks").insert({ organisation_id: orgId, created_by: userId, title: input.title, description: input.description || null, assigned_to: input.assigned_to || null, due_date: input.due_date || null, priority: input.priority, status: input.status, project_id: input.project_id, }); if (error) return { success: false, error: error.message }; return { success: true }; } export async function updateTask( id: string, input: Partial<{ title: string; description: string; assigned_to: string; due_date: string; priority: string; status: string; }> ): Promise<{ success: boolean; error?: string }> { const { orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); const { error } = await supabase .from("tasks") .update(input) .eq("id", id) .eq("organisation_id", orgId); if (error) return { success: false, error: error.message }; return { success: true }; } // ── My Tasks ────────────────────────────────────────────────── export async function fetchMyTasks(): Promise { const { userId, orgId } = await getOrgAndUserId(); const supabase = await createAdminClient(); // Get the employee record for this user const { data: employee } = await supabase .from("employees") .select("id") .eq("organisation_id", orgId) .eq("user_id", userId) .single(); if (!employee?.id) return []; const { data } = await supabase .from("tasks") .select("*, project:projects(name), assignee:employees(first_name, last_name)") .eq("organisation_id", orgId) .eq("assigned_to", employee.id) .order("due_date", { ascending: true, nullsFirst: false }); return data || []; } // ── Helpers ─────────────────────────────────────────────────── export async function fetchEmployeesForAssign(): Promise< { id: string; first_name: string; last_name: string; email: string | null }[] > { 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 || []; }