import { useState } from 'react'; import { supabase } from '@/lib/supabaseClient'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Progress } from '@/components/ui/progress'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { CheckCircle2, XCircle, AlertCircle, Loader2 } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; import { getErrorMessage } from '@/lib/errorHandler'; interface MigrationResult { userId: string; email: string; success: boolean; error?: string; } export function NovuMigrationUtility(): React.JSX.Element { const { toast } = useToast(); const [isRunning, setIsRunning] = useState(false); const [progress, setProgress] = useState(0); const [results, setResults] = useState([]); const [totalUsers, setTotalUsers] = useState(0); const runMigration = async (): Promise => { setIsRunning(true); setResults([]); setProgress(0); try { // Call the server-side migration function const { data: { session } } = await supabase.auth.getSession(); if (!session) { throw new Error('You must be logged in to run the migration'); } const supabaseUrl = import.meta.env.VITE_SUPABASE_URL as string || 'https://api.thrillwiki.com'; const response = await fetch( `${supabaseUrl}/functions/v1/migrate-novu-users`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${session.access_token}`, }, } ); const data = await response.json() as { success: boolean; error?: string; results?: MigrationResult[]; total?: number }; if (!response.ok || !data.success) { throw new Error(data.error || 'Migration failed'); } if (!data.results || data.results.length === 0) { toast({ title: "No users to migrate", description: "All users are already registered with Novu.", }); setIsRunning(false); return; } setTotalUsers(data.total ?? 0); setResults(data.results ?? []); setProgress(100); const successCount = (data.results ?? []).filter((r: MigrationResult) => r.success).length; const failureCount = (data.results ?? []).length - successCount; toast({ title: "Migration completed", description: `Successfully migrated ${successCount} users. ${failureCount} failures.`, }); } catch (error: unknown) { const errorMsg = getErrorMessage(error); toast({ variant: "destructive", title: "Migration failed", description: errorMsg, }); } finally { setIsRunning(false); } }; const successCount = results.filter(r => r.success).length; const failureCount = results.filter(r => !r.success).length; return ( Novu User Migration Register existing users with Novu notification service This utility will register all existing users who don't have a Novu subscriber ID. The process is non-blocking and will continue even if individual registrations fail. {isRunning && totalUsers > 0 && (
Progress {Math.round(progress)}%

Processing {results.length} of {totalUsers} users

)} {results.length > 0 && (
{successCount} succeeded
{failureCount} failed
{results.map((result, idx) => (
{result.email} {result.success ? ( ) : (
{result.error}
)}
))}
)}
); }