"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 Announcement { id: string; organisation_id: string; author_id: string | null; title: string; body: string; is_pinned: boolean; created_at: string; expires_at: string; author_name: string; } export async function fetchAnnouncements(): Promise { const orgId = await getOrganisationId(); const supabase = await createAdminClient(); const { data, error } = await supabase .from("announcements") .select("*") .eq("organisation_id", orgId) .gt("expires_at", new Date().toISOString()) .order("is_pinned", { ascending: false }) .order("created_at", { ascending: false }); if (error) throw error; return (data || []).map((a: any) => ({ id: a.id, organisation_id: a.organisation_id, author_id: a.author_id, title: a.title, body: a.body, is_pinned: a.is_pinned, created_at: a.created_at, expires_at: a.expires_at, author_name: a.author_name || "Unknown", })); } export async function createAnnouncement( title: string, body: string, isPinned: boolean ): Promise<{ success: boolean; announcementId?: string; error?: string }> { const { userId } = await auth(); if (!userId) return { success: false, error: "Unauthenticated" }; const orgId = await getOrganisationId(); const supabase = await createAdminClient(); // Get user's display name from Clerk let authorName = "Unknown"; try { const { clerkClient } = await import("@clerk/nextjs/server"); const clerk = await clerkClient(); const user = await clerk.users.getUser(userId); authorName = user.fullName || user.primaryEmailAddress?.emailAddress || "Unknown"; // Upsert profile so other tables can use it too await supabase.from("profiles").upsert( { id: userId, full_name: authorName, email: user.primaryEmailAddress?.emailAddress || null }, { onConflict: "id" } ); } catch { // Fall back to profile name if available const { data: profile } = await supabase.from("profiles").select("full_name").eq("id", userId).single(); authorName = profile?.full_name || "Unknown"; } const { data, error } = await supabase .from("announcements") .insert({ organisation_id: orgId, author_id: userId, title, body, is_pinned: isPinned, author_name: authorName, }) .select("id") .single(); if (error) return { success: false, error: error.message }; // Broadcast notification to all org members const supabase2 = await createAdminClient(); await supabase2.from("notification_events").insert({ organisation_id: orgId, user_id: null, // broadcast to all type: "announcement", title: `📢 ${title}`, body: body.slice(0, 200), }); return { success: true, announcementId: data.id }; } export async function updateAnnouncement( id: string, title: string, body: string, isPinned: boolean ): Promise<{ success: boolean; error?: string }> { const orgId = await getOrganisationId(); const supabase = await createAdminClient(); const { error } = await supabase .from("announcements") .update({ title, body, is_pinned: isPinned }) .eq("id", id) .eq("organisation_id", orgId); if (error) return { success: false, error: error.message }; return { success: true }; } export async function deleteAnnouncement(id: string): Promise<{ success: boolean; error?: string }> { const orgId = await getOrganisationId(); const supabase = await createAdminClient(); const { error } = await supabase .from("announcements") .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"; }