'use server'; import { and, eq } from 'drizzle-orm'; import { revalidatePath } from 'next/cache'; import { db } from '@/db'; import { testimonios } from '@/db/schema'; import { getCurrentTenantId as getTenantId } from '@/lib/auth/current-user'; type TestimonioEstado = (typeof testimonios.estado.enumValues)[number]; async function setEstado(testimonioId: string, estado: TestimonioEstado) { const tenantId = await getTenantId(); const [updated] = await db .update(testimonios) .set({ estado }) .where(and(eq(testimonios.id, testimonioId), eq(testimonios.tenantId, tenantId))) .returning({ id: testimonios.id }); if (!updated) throw new Error('Opinión no encontrada.'); revalidatePath('/panel/opiniones'); } export async function publicarTestimonio(testimonioId: string) { await setEstado(testimonioId, 'publicado'); } export async function ocultarTestimonio(testimonioId: string) { await setEstado(testimonioId, 'oculto'); } export async function eliminarTestimonio(testimonioId: string) { const tenantId = await getTenantId(); await db .delete(testimonios) .where(and(eq(testimonios.id, testimonioId), eq(testimonios.tenantId, tenantId))); revalidatePath('/panel/opiniones'); }