'use server'; import { db } from '@/lib/db'; import { detectedObjects, youtubeVideos, youtubeChannels } from '@/lib/db/schema'; import { eq } from 'drizzle-orm'; import { auth } from '@/lib/auth'; import { headers } from 'next/headers'; import { revalidatePath } from 'next/cache'; import * as Sentry from '@sentry/nextjs'; import { DEMO_USER_ID } from '../utils/demo'; export async function deleteDetection(detectionId: string) { try { const session = await auth.api.getSession({ headers: await headers(), }); if (!session?.user) { return { success: false, error: 'Unauthorized' }; } // Verify the detection exists and find its owner const data = await db .select({ creatorId: youtubeChannels.creatorId, }) .from(detectedObjects) .innerJoin(youtubeVideos, eq(detectedObjects.videoId, youtubeVideos.id)) .innerJoin(youtubeChannels, eq(youtubeVideos.channelId, youtubeChannels.id)) .where(eq(detectedObjects.id, detectionId)) .limit(1) .then(res => res[0]); if (!data) { return { success: false, error: 'Detection not found' }; } // Verify ownership OR if it's a demo video (allow testing) const isOwner = data.creatorId === session.user.id; const isDemo = data.creatorId === DEMO_USER_ID; if (!isOwner && !isDemo) { return { success: false, error: 'Unauthorized' }; } // Delete the detection (marketplace_matches will cascade delete) await db.delete(detectedObjects).where(eq(detectedObjects.id, detectionId)); revalidatePath('/dashboard/moderation'); return { success: true }; } catch (error) { Sentry.captureException(error, { tags: { action: 'delete-detection', detectionId }, }); return { success: false, error: 'Failed to delete product' }; } }